码迷,mamicode.com
首页 > 其他好文 > 详细

[Algo] 117. Array Deduplication III

时间:2020-02-24 09:17:03      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:val   slow   element   int   arrays   nta   integer   remove   solution   

Given a sorted integer array, remove duplicate elements. For each group of elements with the same value do not keep any of them. Do this in-place, using the left side of the original array and and maintain the relative order of the elements of the array. Return the array after deduplication.

Assumptions

  • The given array is not null

Examples

  • {1, 2, 2, 3, 3, 3} → {1}

public class Solution {
  public int[] dedup(int[] array) {
    // Write your solution here.
    int slow = 0;
    int begin = 0;
    int i = 0;
    while(i < array.length) {
      begin = i;
      while (i < array.length && array[begin] == array[i]) {
        i += 1;
      }
      if (i - begin == 1) {
        array[slow++] = array[begin];
      }
    }
    return Arrays.copyOf(array, slow);
  }
}

 

[Algo] 117. Array Deduplication III

标签:val   slow   element   int   arrays   nta   integer   remove   solution   

原文地址:https://www.cnblogs.com/xuanlu/p/12355346.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!