leetcode 26. Remove Duplicates from Sorted Array

原创 2016年06月01日 11:06:59

题目

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.

解1(超时,这么笨的办法,我还想了那么久,(⊙﹏⊙))

 public int removeDuplicates(int[] nums) {

        if(nums.length==0||nums.length==1)
            return nums.length;

        int i=0;
        int j=1;
        int k=nums.length-1;
        while(i<nums.length && j<nums.length && j<k){

            if(nums[i]==nums[j])
            {
                do{
                    moveBack(nums,j,k);
                    k--;
                }while((nums[i]==nums[j])&& j<k);

                moveBack(nums,i,k);
                k--;
            }
            else{
                i++;
                j=i+1;
            }

        }
      return k+1;
    }

    public void moveBack(int[] nums,int j,int k){

        int temp=nums[j];
        nums[j]=nums[k];
        nums[k]=temp;
        String b=Arrays.toString(nums);
        System.out.println("Before:"+b);
        if(k-j>1)
        Arrays.sort(nums, j, k);
         b=Arrays.toString(nums);
        System.out.println("After:"+b);

    }

解2(让元素后面的元素与前面的元素相比)

public class Solution {
    public int removeDuplicates(int[] nums) {

    if(nums.length ==0){
      return 0;
    }
    int count=1;
    for(int i=1; i<nums.length; ++i){
      if(nums[i] != nums[i-1]){ //注意这行代码
        nums[count]=nums[i];
        count++;
      }
    }
    return count;
  }
}

解3( 让前面的元素与后面的元素相比)

public int removeDuplicates(int[] nums) {
    if(nums.length ==0){
      return 0;
    }
    int count=1;
    for(int i=1; i<nums.length; ++i){
      if(nums[count-1] != nums[i]){ //注意这行代码
        nums[count]=nums[i];
        count++;
      }
    }
    return count;
  }

参考链接

1:http://www.tuicool.com/articles/ayeuYf

LeetCode 26 Remove Duplicates from Sorted Array(从已排序数组中移除重复元素)

翻译给定一个已排序的数组,删除重复的元素,这样每个元素只出现一次,并且返回新的数组长度。不允许为另一个数组使用额外的空间,你必须就地以常量空间执行这个操作。例如, 给定输入数组为 [1,1,2]你的函...
  • NoMasp
  • NoMasp
  • 2015年11月13日 22:41
  • 2253

【LeetCode26】【Remove Duplicates from Sorted Array】

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

leetCode26:Remove Duplicates from Sorted Array

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

Leetcode刷题记——26. Remove Duplicates from Sorted Array(删除数组重复元素)

一、题目叙述: Given a sorted array, remove the duplicates in place such that each element appear ...

【leetcode c++】26 Remove Duplicates from Sorted Array

Given a sorted array, remove the duplicatesin place such that each element appear only once and retu...
  • hqq39
  • hqq39
  • 2015年06月20日 18:46
  • 258

leetcode 26:Remove Duplicates from Sorted Array (C)

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

LeetCode_Easy心得:26. Remove Duplicates from Sorted Array(C语言)

26. Remove Duplicates from Sorted Array
  • hhhhhyb
  • hhhhhyb
  • 2017年07月28日 22:03
  • 121

LeetCode笔记:26. Remove Duplicates from Sorted Array

固定内存下删除有序数组中重复的数字

LeetCode 26: Remove Duplicates from Sorted Array

LeedCode 26: Remove Duplicates from Sorted Array的三种解题思路

Leetcode NO.26 Remove Duplicates from Sorted Array

本题题目要求如下: Given a sorted array, remove the duplicates in place such that each element appear only ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode 26. Remove Duplicates from Sorted Array
举报原因:
原因补充:

(最多只允许输入30个字)