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. I/O输入系统

    I/O输入系统 计算机有两个主要任务:I/O操作与计算处理.在许多情况下,主要任务是I/O操作.而计算处理只是附带的. 操作系统在计算机I/O方面的作用是管理和控制I/O操作和I/O设备. 概述 对与 ...

  2. 九度oj 题目1536:树的最小高度

    题目描述: 给定一棵无向树, 我们选择不同的节点作为根节点时,可以得到不同的高度(即树根节点到叶子节点距离的最大值), 现在求这棵树可能的最低高度. 输入: 输入可能包含多个测试案例. 对于每个测试案 ...

  3. WPF Custom Command And Binding

    using System; using System.Collections.Generic; using System.Windows.Input; namespace WPF.Commands { ...

  4. BZOJ 3779 重组病毒 ——LCT 线段树

    发现操作一很像一个LCT的access的操作. 然后答案就是路径上的虚边的数量. 然后考虑维护每一个点到根节点虚边的数量, 每次断开一条偏爱路径的时候,子树的值全部+1, 连接一条偏爱路径的时候,子树 ...

  5. mongo安装-docker

    拉取官方镜像(可以自己使用dockerFile构建): docker search mongo //查询Docker Hub 上的mongo镜像 docker pull mongo // 拉取官方镜像 ...

  6. Entity Farmework领域建模方式 3种编程方式

    一个业务领域由各个实体和各个相互关联且有格子的属性和行为的实体组成,每个实体都有其状态和验证规则需要维护,Entity Framework (后面简称EF)实体框架设计的出现是为了允许开发人员着重关注 ...

  7. Heavy Transportation(最短路)

    poj 1797 ——Heavy Transportation 思路: 这道题我们可以采用类似于求最短路径的方法,用一种新的“松弛操作”去取代原本的方法. 我们可以记录d[u]为运送货物到点j时最大可 ...

  8. python3.x对python2.x变动

    原文地址:http://rookiedong.iteye.com/blog/1185403 python 2.4 与 python 3.0 的比较 一. print 从语句变为函数 原:     pr ...

  9. Codeforces 375 D Tree and Queries

    Discription You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. ...

  10. Spring基于Setter函数的依赖注入(DI)

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/dependency-injection/spring-setter-based-dependenc ...