LeetCode 26 Remove Duplicates from Sorted Array
来源:互联网 发布:mac用什么五笔 编辑:程序博客网 时间:2024/06/08 09:25
题目:
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.
题意:
给一个已经排好序的数组,要求去除其中重复的元素,并返回新的数组的长度。要求不得申请额外的空间。
我们可以用两个指针,一个遍历数组,另一个用于指向下一个不重复的数,当便利数组的指针读取到当前数和前一个数相同时,跳过操作,继续遍历,当读取到不相同,则更新另一个指针所指向的位置的数,将这个不相同的数存在指向的位置,依次向后枚举判断。
代码如下:
class Solution {public: int removeDuplicates(vector<int>& nums) { int i = 0; if (nums.empty()) return 0; for (int j = 1; j < nums.size(); j ++) { if (nums[i] != nums[j]) { nums[++i] = nums[j]; } } return i + 1; }};
阅读全文
0 0
- LeetCode(26)Remove Duplicates From Sorted Array
- [leetcode 26] Remove Duplicates from Sorted Array
- [Leetcode] 26 - Remove Duplicates from Sorted Array
- leetcode|26|Remove Duplicates from Sorted Array
- leetcode-26 Remove Duplicates from Sorted Array
- leetcode 26 Remove Duplicates from Sorted Array
- LeetCode 26 Remove Duplicates from Sorted Array
- LeetCode 26 Remove Duplicates from Sorted Array
- LeetCode 26 Remove Duplicates from Sorted Array
- leetcode 26 Remove Duplicates from Sorted Array
- LeetCode---(26) Remove Duplicates from Sorted Array
- Leetcode 26 Remove Duplicates from Sorted Array
- LeetCode 26 - Remove Duplicates from Sorted Array
- Leetcode[26]-Remove Duplicates from Sorted Array
- leetcode 26 -- Remove Duplicates from Sorted Array
- LeetCode 26: Remove Duplicates from Sorted Array
- leetcode[26]:Remove Duplicates from Sorted Array
- leetcode 26 Remove Duplicates from Sorted Array
- Tomcat配置成功,但输入http://localhost:8080/不显示界面的解决方法
- 《编程之美》-求数组的子数组之和的最大值错误
- 深入理解uboot 2016
- boost的时间和日期处理1
- Redis 基础入门
- LeetCode 26 Remove Duplicates from Sorted Array
- TI—CC3200【6】通过功放芯片的使能引脚消去POPO声
- Linux 脚本:/etc/init.d/functions 中的主要方法
- 习题 5.8 输出所有的“水仙花数”。
- php 获取父类或者子类名
- Centos Vim C/C++ IDE打造
- Qt5.4下连接Mysql,QSqlDatabase: QMYSQL driver not loaded but available
- 线程的几种状态转换
- hdu--6090--Rikka with Graph