LeetCode 26: Remove Duplicates from Sorted Array
2015-10-22 22:03
344 查看
Remove Duplicates from Sorted Array
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
1and
2respectively. It doesn’t matter what you leave beyond the new length.
解题思路
class Solution { public: int removeDuplicates(vector<int>& nums) { if (nums.size() < 1) return 0; int index = 1; int size = nums.size(); for (int i = 1; i < size; ++i) { if (nums[i] != nums[i-1]) { nums[index++] = nums[i]; } } return index; } };
相关文章推荐
- js数组去重的方法汇总
- php数组去重的函数代码
- 两个数组去重的JS代码
- javascript数组去重方法汇总
- javascript数组去重方法终极总结
- javascript数组去重的方法汇总
- php数组去重实例及分析
- js 数组去重的四种实用方法
- javascript数组去重的六种方法汇总
- 关于js数组去重的问题小结
- js数组去重的三种常用方法总结
- js算法中的排序、数组去重详细概述
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart