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. [CTSC2017]游戏(Bayes定理,线段树)

    传送门:http://uoj.ac/problem/299 题目良心给了Bayes定理,但对于我这种数学渣来说并没有什么用. 先大概讲下相关数学内容: 1.定义:$P(X)$ 表示事件$X$发生的概率 ...

  2. 【数论】【素数判定】CODEVS 2851 菜菜买气球

    素数判定模板. #include<cstdio> #include<map> using namespace std; ],ans=-,l,r,n,sum[]; bool is ...

  3. 《深入理解Spark-核心思想与源码分析》(一)总体规划和第一章环境准备

    <深入理解Spark 核心思想与源码分析> 耿嘉安著 本书共计486页,计划每天读书20页,计划25天完成. 2018-12-20   1-20页 凡事豫则立,不豫则废:言前定,则不跲:事 ...

  4. 【重装系统】老毛桃U盘工具V2013超级装机版-安装原版Win7/Win8

    老毛桃U盘工具V2013超级装机版-程序下载和运行 老毛桃U盘工具V2013超级装机版-安装原版XP的方法 老毛桃U盘工具V2013超级装机版-安装原版Win7/Win8

  5. SecureCRT的一些问题解决

    按下退格键发送删除命令 设置缓冲 拷贝与粘贴 多标签切换   ctrl + tab . 如果同时按下shift,可以方向切换

  6. VS2010中使用命令行參数

    在Linux下编程习惯了使用命令行參数,故使用VS2010时也尝试了一下. 新建项目,c++编敲代码例如以下: #include<iostream> #include<fstream ...

  7. Render Texture coordinates

    https://docs.unity3d.com/550/Documentation/Manual/SL-PlatformDifferences.html Render Texture coordin ...

  8. Laravel 5系列教程六:表单 Forms

    免费视频教程地址https://laravist.com/series/laravel-5-basic 在开始之前,我们把界面先美化一点点先: 首先到https://github.com/JellyB ...

  9. webmagic 初始化 startRequests

    在spider类中有三个方法可以初始化startRequests.可以对这些地方进行扩展. /** * create a spider with pageProcessor. * * @param p ...

  10. Eclipse+Maven远程部署项目到Tomcat中

    使用maven的自动部署功能可以很方便的将maven工程自动打包并且部署到远程tomcat服务器,省去一些繁琐的操作,节省大量时间. 我使用的tomcat版本是8.5,tomcat7和tomcat8都 ...