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);
      }
    }
  • 相关阅读:
    2017年系统架构设计师论文范文
    在SQL Server 2008中执行透明数据加密(转自IT专家网)
    开发笔记
    [置顶] GO-Gin框架快速指南
    [置顶] JS-逆向爬虫
    [置顶] ES篇
    [置顶] GO
    [置顶] 爬虫入狱指南
    [置顶] websocket
    [置顶] Linux篇
  • 原文地址:https://www.cnblogs.com/xuanlu/p/12355255.html
Copyright © 2011-2022 走看看