26. Remove Duplicates from Sorted Array【easy】

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.

解法一:

 class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if (nums.empty() || nums.size() == )
{
return nums.size();
} int i = ;
int j = ;
nums[j++] = nums[i++]; while (i < nums.size() && j < nums.size())
{
if (nums[i] != nums[i - ])
{
nums[j++] = nums[i++];
}
else
{
++i;
}
} return j;
}
};

思路:双指针,注意边界条件的判断

解法二:

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

可读性一般

解法三:

 class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int count = ;
for(int i = ; i < nums.size(); i++){
if(nums[i] == nums[i-]) count++;
else nums[i-count] = nums[i];
}
return nums.size()-count;
}
};

26. Remove Duplicates from Sorted Array【easy】的更多相关文章

  1. 83. Remove Duplicates from Sorted List【easy】

    83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...

  2. Leet Code OJ 26. Remove Duplicates from Sorted Array [Difficulty: Easy]

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

  3. LeetCode:26. Remove Duplicates from Sorted Array(Easy)

    1. 原题链接 https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/ 2. 题目要求 给定一个已 ...

  4. [Leetcode][Python]26: Remove Duplicates from Sorted Array

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 26: Remove Duplicates from Sorted Array ...

  5. LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>

    LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...

  6. leetCode练题——26. Remove Duplicates from Sorted Array

    1.题目 26. Remove Duplicates from Sorted Array--Easy Given a sorted array nums, remove the duplicates  ...

  7. 88. Merge Sorted Array【easy】

    88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...

  8. C# 写 LeetCode easy #26 Remove Duplicates from Sorted Array

    26.Remove Duplicates from Sorted Array Given a sorted array nums, remove the duplicates in-place suc ...

  9. 26. Remove Duplicates from Sorted Array

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

随机推荐

  1. luogu P1418 选点问题

    题目描述 给出n个点,m条边,每个点能控制与其相连的所有的边,要求选出一些点,使得这些点能控制所有的边,并且点数最少.同时,任意一条边不能被两个点控制 输入输出格式 输入格式: 第一行给出两个正整数n ...

  2. luogu P1772 [ZJOI2006]物流运输

    题目描述 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格的管理和跟踪. ...

  3. 【动态规划】【最短路】【spfa】bzoj1207 [HNOI2004]打鼹鼠

    <法一>若打了一只鼹鼠后,还能打另一只,我们可以在它们之间连权值为1的边.于是答案就是 以m为终点的最长路长度+1.建反图,就是单源最长路. MLE TLE 一时爽. #include&l ...

  4. 摄氏度和华氏度之间的额转换 Exercise06_08

    /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:摄氏度和华氏度之间的额转换 * */ public class Exercise06_08 { public static v ...

  5. 【MySQL笔记】: unable to connect to remote host. catalog download has failed.

    安装完MySQL之后,它每天凌晨启动一个Intaller任务,甚是烦人:   这是一个Windows的计划服务,在这里删除即可,开始/附件/系统工具/任务计划程序,把mysql的定时任务计划取消/删除 ...

  6. js数据加载完成的方法

    1.document.onreadystatechange(页面加载完成的方法)配合document.readyState(获取加载状态)使用 document.onreadystatechange= ...

  7. iptables配置

    iptables -F iptables -P INPUT DROP iptables -P FORWARD DROP iptables -A INPUT -m state --state ESTAB ...

  8. linux内核分析笔记----上半部与下半部(下)

    接着上节的来,我们在上节说了软中断和tasklet,那这最后就是工作队列了哦.. 工作队列和前面讨论的其他形式都不相同,它可以把工作推后,交由一个内核线程去执行----该工作总是会在进程上下文执行.这 ...

  9. 使用webpack构建本地服务器

    想不想让你的浏览器监听你的代码的修改,并自动刷新显示修改后的结果,其实Webpack提供一个可选的本地开发服务器,这个本地服务器基于node.js构建,可以实现你想要的这些功能,不过它是一个单独的组件 ...

  10. jstack来分析。当linux出现cpu被java程序消耗过高时

    我们使用jdk自带的jstack来分析.当linux出现cpu被java程序消耗过高时,以下过程说不定可以帮上你的忙: 1.top查找出哪个进程消耗的cpu高 21125 co_ad2    18   ...