关闭

[LeetCode26] Remove Duplicates from Sorted Array

707人阅读 评论(0) 收藏 举报
分类:

作为LeetCode:80的先导题,不会做80的先看这题。

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.

Subscribe to see which companies asked this question

C++实现:

从头往后依次比较,遇到不同的就把他放到前面,若要得到最后的数组结果 可以使用 nums.resize(index+1).

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        if (nums.size() == 0)
		return 0;
	int index = 0;
	for (int i = 0; i < nums.size(); i++)
	{
		if (nums[index] != nums[i])
			nums[++index] = nums[i];
	}
	return index + 1;
    }
};


0
0
查看评论

LeetCode --- 26. Remove Duplicates from Sorted Array

题目链接:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that each element appear only once and return th...
  • makuiyu
  • makuiyu
  • 2015-02-02 11:52
  • 2186

Remove Duplicates from Sorted Array [python解]

题目要求:Givena sorted array, remove the duplicates in place such that each element appearonly once and return the new length. Do not allocate e...
  • nadizhong4136
  • nadizhong4136
  • 2017-07-24 19:18
  • 261

LeetCode: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 ex...
  • fytain
  • fytain
  • 2013-09-02 20:42
  • 2757

Remove Duplicates from Sorted Array -- LeetCode

原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/  这道题跟Remove Element类似,也是考察数组的基本操作,属于面试中比较简单的题目。做法是维护两个指针,一个保留当前有...
  • linhuanmars
  • linhuanmars
  • 2014-02-27 05:12
  • 10899

LeetCode(26)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 spa...
  • feliciafay
  • feliciafay
  • 2013-12-31 08:37
  • 1243

LeetCode 26 — Remove Duplicates from Sorted Array(C++ Java Python)

题目:http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the duplicates in place such that each element ...
  • dragon_dream
  • dragon_dream
  • 2014-02-23 15:12
  • 3847

26. Remove Duplicates from Sorted Array (python)

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 f...
  • qq_28119401
  • qq_28119401
  • 2016-10-30 15:58
  • 629

Remove Duplicates from Sorted Array II -- LeetCode

原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/  这道题跟Remove Duplicates from Sorted Array比较类似,区别只是这里元素可以重复出现...
  • linhuanmars
  • linhuanmars
  • 2014-04-23 08:39
  • 10680

LeetCode(80) Remove Duplicates From Sorted Array II

题目如下: Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], ...
  • feliciafay
  • feliciafay
  • 2015-01-11 03:55
  • 1295

LeetCode Remove Duplicates from Sorted Array(C语言O(n)解法)

题目: 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 ...
  • ahfytao
  • ahfytao
  • 2015-03-10 15:03
  • 414
    个人资料
    • 访问:18646次
    • 积分:726
    • 等级:
    • 排名:千里之外
    • 原创:52篇
    • 转载:4篇
    • 译文:2篇
    • 评论:1条
    最新评论