题目:
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

For example,
Given input array nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn’t matter what you leave beyond the new length.

翻译:
给定一个排序好的数组,就地移除重复的元素,来使得每个元素只出现一次,并且返回新的长度。
不要分配额外的空间给另一个数组,你必须完成它在原数组上,并且只使用常数级的内存。

分析:
由于需要移除重复的元素,所以必须有移动元素的操作。当遍历的i指针指向的元素与上一个元素重复时,需要采用一个指针nextEmpty来记录当前这个位置(需要被移除的位置,也就是要把后面的元素复制过来的位置),当遍历到下一个不重复的元素时,再复制到这个位置。

代码:

public class Solution {public int removeDuplicates(int[] nums) {int nextEmpty=1;for(int i=1;i<nums.length;i++){if(nums[i]!=nums[i-1]){nums[nextEmpty]=nums[i];nextEmpty++;}}return nextEmpty;}
}

Leet Code OJ 26. Remove Duplicates from Sorted Array [Difficulty: Easy]相关推荐

  1. Leet Code OJ 83. Remove Duplicates from Sorted List [Difficulty: Easy]

    题目: Given a sorted linked list, delete all duplicates such that each element appear only once. For e ...

  2. 26. Remove Duplicates from Sorted Array【easy】

    26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...

  3. LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] c++

    LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...

  4. LeetCode 26. Remove Duplicates from Sorted Array

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  5. leetcode python3 简单题26. Remove Duplicates from Sorted Array

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二十六题 (1)题目 英文: Given a sorted array nums, ...

  6. Leet Code OJ 4. Median of Two Sorted Arrays [Difficulty: Hard]

    题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

  7. leetCode #26 Remove Duplicates from Sorted Array

    删除相同数字 1 class Solution { 2 public: 3 int removeDuplicates(vector<int>& nums) { 4 int coun ...

  8. Leet Code OJ 102. Binary Tree Level Order Traversal [Difficulty: Easy]

    题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...

  9. Leet Code OJ 104. Maximum Depth of Binary Tree [Difficulty: Easy]

    题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...

最新文章

  1. fasterrcnn深度学习口罩检测
  2. Android Service与Activity的交互
  3. boost::intrusive::sg_set用法的测试程序
  4. WinJS实用开发技巧(5):学习资料推荐
  5. linux操作指令训练,实验二linux 常用命令练习
  6. 043、JVM实战总结:动手实验,自己动手模拟出频繁Young GC的场景
  7. Shell学习笔记一
  8. Hyperledger Fabric 或 Composer的configtx.yaml配置文件解析
  9. IIS7 上部署MVC3,执行错误:Could not load type 'System.ServiceModel.Activation.HttpModule'
  10. android四级菜单,android实现菜单三级树效果
  11. Fortran笔记,全局变量
  12. 中国石油大学-《现代应用文写作》第三阶段在线作业
  13. python爬取谷歌图片_selenium+python 爬取网络图片(1) -- soso、谷歌、好搜
  14. 如何关闭mysql secure_file_priv
  15. Python 网络爬虫及数据可视化
  16. c语言将字符串按空格分割_C语言根据空格分割获取字符串单元
  17. Windows中怎么下载桌面便签小工具 便签小工具简单使用教程
  18. 麦当劳肯德基供应商使用变质过期肉:吃不死人
  19. 深度学习车辆检测实现自动驾驶
  20. 配合清退工作,建议将金融科技纳入监管沙盒

热门文章

  1. 广义Fibonacci数列找循环节
  2. cocos2d-x游戏开发(十)执行单元场景CCScene
  3. 使用Packet.dll和npf.sys实现原始数据包的发送和接收
  4. 曹大带我学 Go(8)—— 一个打点引发的事故
  5. 一个卑微的程序员友链
  6. 【免费报名】与阿里云一同探索视频云的新技术与新场景
  7. 音视频技术开发周刊 81期
  8. 技术人凭什么那么骄傲?谢然:给自己开发工具
  9. 哈希表及哈希冲突解决办法
  10. WebRTC 那些常用的缩略词以及部分知识杂谈