问题描述:

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 A = [1,1,2],

Your function should return length = 2, and A is now [1,2].

解决原理:

(若输入数组无序,则先对数组进行排序,则相同值相邻)

两个游标len,i

0~len是元素不重复的子数组,即len是目标子数组的最后一个元素的索引

游标i遍历数组

若A[i]!=A[len],则为目标子数组增添一个元素

代码:

 class Solution {
public:
int removeDuplicates(int A[], int n) {
int len = ;
if(n == ) return ;
//sort(A,A+n);
for(int i = ; i < n; i++){
if(A[i] != A[len]){
len++;
A[len] = A[i];
}
}
return len+;
}
};

Q5: Remove Duplicates from Sorted Array的更多相关文章

  1. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  2. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

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

  3. Remove Duplicates From Sorted Array

    Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...

  4. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  5. 26. Remove Duplicates from Sorted Array

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

  6. 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

  7. LeetCode:Remove Duplicates from Sorted Array I II

    LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...

  8. 【26】Remove Duplicates from Sorted Array

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

  9. leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

随机推荐

  1. array_intersect() php筛选两个数组共有的元素

    我们已经讲过如何筛选出连个数组中不共有的元素,今天就来看看php如何筛选出两个数组中共有的元素,例如筛选$array1和$array2共有的元素. 函数名:array_intersect(): 调用方 ...

  2. 学习进度条<第一周>

    所花时间(包括上课):8小时(上课4,编程0.5,写博客1,读书2.5) 代码量:90行 博客量:4篇 了解到的知识点:什么是BUG                 哪怕有几万分之一的概率也要考虑安全 ...

  3. 【Sublime Text 3】插件

    TrailingSpacer 高亮显示多余的空格和Tab HTML-CSS-JS Prettify

  4. 相对URL拼接为绝对URL的过程

    URL有两种方式:绝对的和相对的. 绝对URL中包含有访问资源的所需的全部信息 举一个例子: <HTML> <HEAD><TITLE>Joe's Tools< ...

  5. 跟开涛老师学shiro -- 授权

    授权,也叫访问控制,即在应用中控制谁能访问哪些资源(如访问页面/编辑数据/页面操作等).在授权中需了解的几个关键对象:主体(Subject).资源(Resource).权限(Permission).角 ...

  6. 最短路径--SPFA 算法

    适用范围:给定的图存在负权边,这时类似Dijkstra等算法便没有了用武之地,而Bellman-Ford算法的复杂度又过高,SPFA算法便派上用场了. 我们约定有向加权图G不存在负权回路,即最短路径一 ...

  7. 论XCODE工程里使用的宏定义.

    在XCODE开发过程中,经常会遇到引用头文件,引用库路径的问题,如果不是直接的源码引入,则需要在工程中增加设置.虽然现在有了Pod这类集合管理工具,但有时为了一个很小的lib引入Pod这尊大神还是有点 ...

  8. Matlab优化存储器读写来改善程序性能

    最近用Matlab写程序的时候终于遇到了程序执行效率的问题,于是在Google上面搜索了一篇提高代码性能的文章,简单的概括一下. 文章是通过优化寄存器读写来提高执行速度的,主要体现在三个方面: 在做循 ...

  9. 作业6 NABCD模型分析,产品Backlog

    1.N(Need 需求): 随着生活水平的提高,每个家庭中都会有电脑和移动设备,可以更加快捷方便使用软件.以前孩子练习计算能力需要通做习题卷或老师出题目来进行,但现在只要通过这个四则运算的程序,可以自 ...

  10. XML的解析方式(DOM、SAX、StAX)

    (新)  XML的解析方式(DOM.SAX.StAX) 博客分类: XML   一般来说,解析XML文件存在着两种方式,一种是event-based API,比如说象SAX,XNI. 第二种是tree ...