zoukankan      html  css  js  c++  java
  • [Algo] 115. Array Deduplication I

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

    Assumptions

    • The array is not null

    Examples

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

    public class Solution {
      public int[] dedup(int[] array) {
        // Write your solution here.
       // return array;
        if (array == null || array.length <= 1) {
          return array;
        }
        int slow = 0;
        // result include slow
        for (int i = 1; i < array.length; i++) {
          if (array[i] == array[slow]) {
            continue;
          }
          array[++slow] = array[i]; 
        }
        return Arrays.copyOf(array, slow + 1);
      }
    }
    public class Solution {
      public int[] dedup(int[] array) {
        // Write your solution here.
       // return array;
        if (array == null || array.length <= 1) {
          return array;
        }
        int slow = 1;
        for (int i = 1; i < array.length; i++) {
          if (array[i] == array[slow - 1]) {
            continue;
          }
          array[slow++] = array[i]; 
        }
        return Arrays.copyOf(array, slow);
      }
    }
  • 相关阅读:
    [BJOI2006]狼抓兔子
    [HNOI2016]最小公倍数
    hihocoder 1419 重复旋律4
    [NOI2015]品酒大会
    [SDOI2016]生成魔咒
    [ZJOI2009]狼和羊的故事
    BZOJ4361 isn
    [SDOI2009]虔诚的墓主人
    BZOJ 3329 Xorequ
    [ZJOI2013]丽洁体
  • 原文地址:https://www.cnblogs.com/xuanlu/p/12355255.html
Copyright © 2011-2022 走看看