https://leetcode.com/problems/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 1 and 2 respectively. It doesn't matter what you leave beyond the new length.

思路:

也是指针思想,利用新array任意元素一定不会比旧array该元素所在对应位置index大的原理(也可以看作栈),通过两个指针表征新旧array。

遍历一遍array,只有当遇到新元素时,压入新array。

AC代码:

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

LeetCode(26)题解:Remove Duplicates from Sorted Array的更多相关文章

  1. 【26】Remove Duplicates from Sorted Array

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

  2. 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  3. 【LeetCode】080. Remove Duplicates from Sorted Array II

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

  4. 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)

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

  5. leetcode笔记:Remove Duplicates from Sorted Array II

    一.题目描写叙述 二.解题技巧 这道题和Remove Duplicates from Sorted Array这道题是相似的.仅仅只是这里同意出现反复的数字而已,能够採用二分搜索的变种算法.仅仅只是增 ...

  6. 【LeetCode算法-26】Remove Duplicates from Sorted Array

    LeetCode第26题 Given a sorted array nums, remove the duplicates in-place such that each element appear ...

  7. leetcode第26题--Remove Duplicates from Sorted Array

    problem: Given a sorted array, remove the duplicates in place such that each element appear only onc ...

  8. LeetCode(26) Remove Duplicates from Sorted Array

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

  9. LeetCode OJ 80. Remove Duplicates from Sorted Array II

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

随机推荐

  1. 在使用Cocos2d-JS 开发过程中需要用到的单体设计模式

    JavaScript 单体模式的一种实现 T.getInstance = (function () { var instance = null; return function () { return ...

  2. XML文件的操作说明

    说明:C#中XmlNode与XmlElement的区别如下:xmlnode类表示xml文档中的单个节点,其命名空间为:System.Xml.XmlNode的三个最主要的子类包括:XmlDocument ...

  3. 九度oj 题目1450:产生冠军

    题目描述: 有一群人,打乒乓球比赛,两两捉对撕杀,每两个人之间最多打一场比赛. 球赛的规则如下: 如果A打败了B,B又打败了C,而A与C之间没有进行过比赛,那么就认定,A一定能打败C. 如果A打败了B ...

  4. sql自动审核工具-inception

    [inception使用规范及说明文档](http://mysql-inception.github.io/inception-document/usage/)[代码仓库](https://githu ...

  5. ASP.NET上传大文件404报错

    报错信息: Failed to load resource: the server responded with a status of 404 (Not Found) 尝试1: 仅修改Web.con ...

  6. 【Android】SharedPreference存储数据

    SharedPreference存储数据 使用SharedPreference保存数据  putString(key,value) 使用SharedPreference读取数据  getString( ...

  7. [SCOI2003]字符串折叠 (区间DP)

    题目描述 折叠的定义如下: 一个字符串可以看成它自身的折叠.记作S = S X(S)是X(X>1)个S连接在一起的串的折叠.记作X(S) = SSSS…S(X个S). 如果A = A’, B = ...

  8. jsp 详解request对象

    request对象 客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求,然后做出响应.它是HttpServletRequest类的实例. 序号 方 法 说 明 1  object ...

  9. MongoDB_java连接MongoDB

    java程序连接单机版的mongodb: 参考:http://www.runoob.com/mongodb/mongodb-java.html https://www.yiibai.com/mongo ...

  10. 并发编程辅助工具-java.util.concurrent

    1. CountDownLatch 类似于计数器的功能,主要用于控制某个任务的执行先后顺序,可以控制某个任务在其他任务(可能是多线程的)执行完 之后,才会去执行. public static void ...