[Algo] 117. Array Deduplication III

Posted xuanlu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Algo] 117. Array Deduplication III相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

[Algo] 118. Array Deduplication IV

php 函数小技巧

PHP加密与解密

快排_Java

如何从字符串中提取数字?

php二维数组求和