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. CodeForces - 986C AND Graph

    不难想到,x有边连出的一定是 (2^n-1) ^ x 的一个子集,直接连子集复杂度是爆炸的...但是我们可以一个1一个1的消去,最后变成补集的一个子集. 但是必须当且仅当 至少有一个 a 等于 x 的 ...

  2. Ze_Min Tree 主席树

    前言 主席树,也叫可持久化线段树,所以他的本质是颗线段树,而可持久化指的是这颗线段树可以访问过去某个时刻线段树上的信息. 应用 应用的比较多的是查询区间的第k大值(因为其他的数据结构不好做). 实现 ...

  3. Problem K: 数字菱形

    #include<stdio.h> int main() { int n,i,j,k,t,x,q,p; while(scanf("%d",&n)!=EOF) ; ...

  4. 将SeqReader打包成可执行的jar包

    SeqReader是我定义的一个读取SequenceFile文件,并将部分(key,value)打印到控制台窗口的类,其完整代码如下: /** * Created with IntelliJ IDEA ...

  5. [测试技术分享]DNS域传送漏洞测试

    DNS域传送漏洞测试 1.简介: DNS(Domain Name System)也叫域名管理系统,它它建立在一个分布式数据库基础之上,在这个数据库里,保存了IP地址和域名的相互映射关系.正因为DNS的 ...

  6. 数据访问与sql语句的管理(一)

    在开发过程中数据访问是必不可少的.每个框架都会有自己数据访问机制.大家在一般的情况下会为自己的框架配备2套数据访问机制,ORM和DataHelper.当然,根据项目的需要有时候也可能只一种. 其实这2 ...

  7. 请用心练完这16个webpack小例子

    16个demo,webpack+react搭配使用首先教大家2个新技能 1.按照正常github地址情况下,你的github本身不能访问目录. 例如要访问vue-demo下的vueCpu文件夹:htt ...

  8. 用Qemu模拟vexpress-a9 (五) --- u-boot引导kernel,device tree的使用

    环境介绍 Win7 64 + Vmware 11 + ubuntu14.04 32 u-boot 版本:u-boot-2015-04 Linux kernel版本:linux-3.16.y busyb ...

  9. 【java】获取解析资源文件的方法

    关于资源文件的读取,有很多种方法,下面补充了多种方法 1.java.util.ResourceBundle 使用java自带的util包下的ResourceBundle类获取,使用方法最简单 //获取 ...

  10. 消息队列系列(三):.Rabbitmq Trace的使用

       一.什么是Trace        Trace是Rabbitmq用于记录每一次发送的消息,方便使用Rabbitmq的开发者调试.排错.可通过插件形式 提供可视化界面   二.Trace实现概况 ...