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

  1. package easy;
  2.  
  3. public class RemoveDuplicatesOfSortedArray {
  4.  
  5. public int removeDuplicates(int[] nums){
  6. //if the nums is empty return length = 0
  7. int length = 0;
  8. if(nums.length>0){
  9. int temp = nums[0];
  10. length=1;
  11. for(int i = 0; i<nums.length;i++){
  12. if(temp != nums[i]){
  13. //找到所有不一样的数
  14. temp = nums[i];
  15. length++;
  16. //将所有不一样的数放在相应的位置
  17. nums[length - 1] = temp;
  18. }
  19. }
  20. }
  21.  
  22. return length;
  23. }
  24.  
  25. public static void main(String[] args){
  26. RemoveDuplicatesOfSortedArray a = new RemoveDuplicatesOfSortedArray();
  27. int[] nums = new int[]{1,1,2};
  28. int length = a.removeDuplicates(nums);
  29. System.out.println(length);
  30. }
  31.  
  32. }

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. iOS中 数据持久化 UI高级_17

    数据持久化的本质就是把数据由内写到本地(硬盘中),在iOS指将数据写到沙盒文件夹下: 沙盒机制:指的就是采用沙盒文件夹的形式管理应用程序的本地文件,而且沙盒文件夹的名字是随机分配的,采用十六进制方法命 ...

  2. sublime test2 快捷键

    快捷键比较全的:http://blog.useasp.net/archive/2013/06/14/sublime-text-2-all-default-Shortcuts-table-on-wind ...

  3. STL算法设计理念 - 函数对象和函数对象当参数和返回值

    函数对象: 重载函数调用操作符的类,其对象常称为函数对象(function object),即它们是行为类似函数的对象.一个类对象,表现出一个函数的特征,就是通过"对象名+(参数列表)&qu ...

  4. Mapreduce 框架解析

    MapReduce过程解析 一.客户端 Map-Reduce的过程首先是由客户端提交一个任务开始的. public static RunningJob runJob(JobConf job) thro ...

  5. Unix/Linux中的grep命令(转)

    本文转载自:如何使用Unix/Linux grep命令——磨刀不误砍柴工系列.该博文条理很清晰. grep简介 grep在一个或多个文件中查找与模式字符串(pattern)匹配的行,并将搜索的结果打印 ...

  6. 2DSprite添加Light照射(Unity3D开发之十六)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/45534245 ...

  7. ERP-非财务人员的财务培训教(四)------公司/部门的成本与费用控制

    一.损益表.资产负责表 二.成本分类 ----成本习性 三.成本核算模式 四.成本控制原则 第四部分 公司/部门的成本与费用控制   一.损益表.资产负责表   项目 Items 产品销售收入 Sal ...

  8. C语言之归并排序

    即将两个都升序(或降序)排列的数据序列合并成一个仍按原序排列的序列. 上代码: #include <stdio.h> #include <stdlib.h> #define m ...

  9. Java进阶(十五)Java中设置session的详细解释

    Java中设置session的详细解释 简单通俗的讲session就是象一个临时的容器,用来存放临时的东西.从你登陆开始就保存在session里,当然你可以自己设置它的有效时间和页面,举个简单的例子: ...

  10. django-debug-tools 使用

    用django开发很快也很容易,但是很多时候我们的经验并不是很足,就会给自己挖下很多坑,不管是性能问题,还是开发语言使用技巧问题都会给应用的稳定带来危害, 开发之后的调试和调优就显得很重要,今天就尝试 ...