description:

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.

thoughts:

first we should juage if the array is empty, if true, we can just return length=0,and do not do anything special about the array.if not, then the length must >=1.we first record the nums[0]  as temp and  legnth = 1, then find the first different number with it,make the temp equal to it, and length++;end make the nums[length -1] = temp.do this operation untill you have scan all the value of the nums,then return length.

my code in java

package easy;

public class RemoveDuplicatesOfSortedArray {

    public int removeDuplicates(int[] nums){
//if the nums is empty return length = 0
int length = 0;
if(nums.length>0){
int temp = nums[0];
length=1;
for(int i = 0; i<nums.length;i++){
if(temp != nums[i]){
//找到所有不一样的数
temp = nums[i];
length++;
//将所有不一样的数放在相应的位置
nums[length - 1] = temp;
}
}
} return length;
} public static void main(String[] args){
RemoveDuplicatesOfSortedArray a = new RemoveDuplicatesOfSortedArray();
int[] nums = new int[]{1,1,2};
int length = a.removeDuplicates(nums);
System.out.println(length);
} }

remove duplicate of the sorted array的更多相关文章

  1. LeetCode 26. Remove Duplicates from Sorted Array (从有序序列里移除重复项)

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

  2. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  3. Remove Duplicates from Sorted Array II leetcode java

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

  4. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

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

  5. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

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

  6. Remove Duplicates from Sorted Array II

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

  7. No.026:Remove Duplicates from Sorted Array

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

  8. LeetCode 26 Remove Duplicates from Sorted Array

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

  9. Remove Duplicates from Sorted Array II [LeetCode]

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

随机推荐

  1. 【leetcode】经典算法题-Counting Bits

    题目描述: 给定一个数字n,统计0-n之间的数字二进制的1的个数,并用数组输出 例子: For num = 5 you should return [0,1,1,2,1,2]. 要求: 算法复杂复o( ...

  2. (NO.00001)iOS游戏SpeedBoy Lite成形记(二十二)

    自己的游戏自己更需要多玩,这样才能首先发现不足的地方.所以本猫到现在已经忍一个地方很久了,就是弹出moneyLayer后每次都要输入数字才能关闭,这多少让人不爽.于是本篇我们就修正这个小小的不便. 首 ...

  3. XBMC源代码分析 2:Addons(皮肤Skin)

    前文已经对XBMC源代码的整体架构进行了分析: XBMC源代码分析 1:整体结构以及编译方法 从这篇文章开始,就要对XBMC源代码进行具体分析了.首先先不分析其C++代码,分析一下和其皮肤相关的代码. ...

  4. Darwin Streaming Server 安装流程

    Darwin StreamingServer 安装流程 Darwin StreamingServer 支持开放源代码和基于标准的实时传输协议/实时流协议(RTP/ RTSP).MPEG-4 和MP3 ...

  5. 《java入门》第一季之类(String类字符串一旦被赋值就没法改变)

    毫无疑问,String类是java里面最重要的类之一.因此它有很多方法需要了解和掌握. 字符串一旦被赋值,值就不能发生改变: package cn.itcast_02; /* * 字符串的特点:一旦被 ...

  6. mysql 备份和恢复的两条命令

    压缩备份: 1.mysqldump -h localhost -u root -p dbname | gzip > dbname.sql.gz 压缩恢复: 1.gunzip < dbnam ...

  7. Android WebKit 内核

    一.WebKit简介 WebKit是一个开源的浏览器网页排版引擎,包含WebCore排版引擎和JSCore引擎.WebCore和JSCore引擎来自于KDE项目的KHTML和KJS开源项目.Andro ...

  8. 图像边缘检测--OpenCV之cvCanny函数

    图像边缘检测--OpenCV之cvCanny函数 分类: C/C++ void cvCanny( const CvArr* image, CvArr* edges, double threshold1 ...

  9. 【UML 建模】UML建模语言入门-视图,事物,关系,通用机制

    . 作者 :万境绝尘  转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/18964835 . 一. UML视图 1. Ration ...

  10. linux下用gtk+写比赛赌博GUI小游戏

    游戏界面全部由gtk的GUI完成,没有使用openGL之类的高端货. 游戏玩法就是8位选手比赛跑步,你可以在赛前赌哪位选手会赢,如果输了cash会被扣除,反之cash会增加. 无聊写了3个选项:小数时 ...