问题描述:

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. js注册验证

    var user = $("user");var userCheck = $("userCheck");var pwd = $("pwd") ...

  2. 面向连接的Socket Server的简单实现(简明易懂)

    一.基本原理 有时候我们需要实现一个公共的模块,需要对多个其他的模块提供服务,最常用的方式就是实现一个Socket Server,接受客户的请求,并返回给客户结果. 这经常涉及到如果管理多个连接及如何 ...

  3. HTML的常用标签

    <!--编写HTML常用的标签 文字版面的编辑 格式标签 文本标签 超链接和锚点 图像和图像地图 --> <!DOCTYPE html PUBLIC "-//W3C//DT ...

  4. XMPP协议的原理介绍

    XMPP(可扩展消息处理现场协议)是基于可扩展标记语言(XML)的协议,它用于即时消息(IM)以及在线现场探测.它在促进服务器之间的准即时操作.这个协议可能最终允许因特网用户向因特网上的其他任何人发送 ...

  5. Android RecyclerView(瀑布流)水平/垂直方向分割线

     Android RecyclerView(瀑布流)水平/垂直方向分割线 Android RecyclerView不像过去的ListView那样随意的设置水平方向的分割线,如果要实现Recycle ...

  6. USB 描述符

    标准的USB设备有5种USB描述符:设备描述符,配置描述符,字符串描述符,接口描述符,端点描述符. // Standard Device Descriptor typedef struct { u8 ...

  7. Google Translate CLI 筆記

    $ trans -shell -brief > Rien ne réussit comme le succès. Nothing succeeds like success. > Was ...

  8. Matlab GUI设计中的一些常用函数

    Matlab GUI常用函数总结 % — 文件的打开.读取和关闭% — 文件的保存% — 创建一个进度条% — 在名为display的axes显示图像,然后关闭% — 把数字转化为时间格式% — ch ...

  9. 求10000以内n的阶乘

    总时间限制:  5000ms 内存限制:  655360kB 描述 求10000以内n的阶乘. 输入 只有一行输入,整数n(0<=n<=10000). 输出 一行,即n!的值. 样例输入 ...

  10. ipconfig /flushdns 解释

    当我们一域名的形式访问过目标网站后,该网站的域名和IP地址对应关系就会自动保存到本地工作站的DNS缓存列表中,如果以后再次访问该域名,浏览器就会先访问DNS缓存列表中的信息.但是,如果被访问网站的域名 ...