LeetCode 26:Remove Duplicates from Sorted Array
来源:互联网 发布:mysql truncate table 编辑:程序博客网 时间:2024/05/01 11:29
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.
例如,给定nums = [1, 1, 2]
你的函数应该返回2,并且前两个元素为1和2。对于新数组长度以外的元素不作要求。
因为是已排好序的数组,因此只需顺序找到比前一个已排好的元素大的元素,然后放到它该放的位置即可。重点还是只扫一遍
class Solution {public: int removeDuplicates(vector<int>& nums) { if(nums.size()==0||nums.size()==1) return nums.size(); int count=1,temp=nums[0]; for(int i=1;i<nums.size();i++) if(nums[i]>temp) { nums[count]=nums[i]; temp=nums[count++]; } return count; }};
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
- 分享一种最简单的Android打渠道包的方法
- LintCode:把排序数组转换为高度最小的二叉搜索树
- iOS GitHub上值得留意的第三方库
- 分布式搜索Elasticsearch——概述
- 设置启动图片
- LeetCode 26:Remove Duplicates from Sorted Array
- iOS之自定义弹出框--AlertView
- 常用的正则表达式
- UIWebView设置字体大小,颜色,字体
- Python单元测试框架之Nose
- alsa的 snd_pcm_readi 和 snd_pcm_writei
- js - 下雪动态效果-setInterval、animate
- Drools 入门例子
- 常用工具类