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);
      }
    }
  • 相关阅读:
    CSS实现雨滴动画效果
    大型网站架构系列:电商网站架构案例
    CSS 不定宽高的垂直水平居中方式总汇
    js中尺寸类样式
    Tiling
    排序二叉树
    算术表达式的转换
    Area
    catch that cow
    R中双表操作学习[转载]
  • 原文地址:https://www.cnblogs.com/xuanlu/p/12355255.html
Copyright © 2011-2022 走看看