Given an unsorted integer array, remove adjacent duplicate elements repeatedly, from left to right. 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. Return the array after deduplication.

Assumptions

  • The given array is not null

Examples

{1, 2, 3, 3, 3, 2, 2} → {1, 2, 2, 2} → {1}, return {1}

Soltuion 1:

public class Solution {
public int[] dedup(int[] array) {
// Write your solution here
LinkedList<Integer> stack = new LinkedList<>();
int i = 0;
while (i < array.length) {
int cur = array[i];
if (!stack.isEmpty() && stack.peekFirst() == cur) {
while (i < array.length && array[i] == cur) {
i += 1;
}
stack.pollFirst();
} else {
stack.offerFirst(cur);
i += 1;
}
}
int[] res = new int[stack.size()];
for (int j = res.length - 1; j >= 0; j--) {
res[j] = stack.pollFirst();
}
return res;
}
}

Soltuion 2:

public class Solution {
public int[] dedup(int[] array) {
// Write your solution here
// incldue end result
int end = -1;
for (int i = 0; i < array.length; i++) {
int cur = array[i];
if (end == -1 || cur != array[end]) {
array[++end] = array[i];
} else {
while (i + 1 < array.length && array[i + 1] == cur) {
i += 1;
}
end -= 1;
}
}
return Arrays.copyOf(array, end + 1);
}
}

[Algo] 118. Array Deduplication IV的更多相关文章

  1. [Algo] 117. Array Deduplication III

    Given a sorted integer array, remove duplicate elements. For each group of elements with the same va ...

  2. [Algo] 115. Array Deduplication I

    Given a sorted integer array, remove duplicate elements. For each group of elements with the same va ...

  3. JavaScript library of crypto standards. 看源码

    crypto-js - npm https://www.npmjs.com/package/crypto-js crypto-js/docs/QuickStartGuide.wiki <wiki ...

  4. IOS懒加载

    1.懒加载基本 懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内存小).所谓懒加载,写的是其get方法. 注意:如果是懒加载的话则一定要注意先判断是否已经有了,如果没有那么再去进行实例化 ...

  5. IOS开发之--UIScrollView pagingEnabled自定义翻页宽度

    用到UIScrollview的翻页效果时,有时需要显示一部分左右的内容,但是UIScrollView的PagingEnabled只能翻过整页,下面几个简单的设置即可实现 技术点: 1. 创建一个继承U ...

  6. python 常用模块 Top200

    名次 模块名称 被使用项目数 1 sys 7858 2 os 6983 3 re 5663 4 time 5268 5 random 3339 6 datetime 3310 7 setuptools ...

  7. Python数据分析之numpy学习

    Python模块中的numpy,这是一个处理数组的强大模块,而该模块也是其他数据分析模块(如pandas和scipy)的核心. 接下面将从这5个方面来介绍numpy模块的内容: 1)数组的创建 2)有 ...

  8. PHP世纪万年历

    <?  //世纪万年历  #这是唯一的设置-请输入php文件的位置  $file="http://192.168.1.168/php/rl/s2m.php";  //#农历每 ...

  9. Numpy系列(三)- 基本运算操作

    Numpy 中数组上的算术运算符使用元素级别.最后的结果使用新的一个数组来返回. import numpy as np a = np.array( [20,30,40,50] ) b = np.ara ...

随机推荐

  1. Vulkan 之 Layers

    Layers 暴露给api层,不像传统图形API集成在驱动里面,开发者根据自己的需要进行开启,最终目的还是为了提高性能. The Loader he loader is the central arb ...

  2. MyBatis parameterType、resultType的数据类型

    parameterType.resultType的数据类型要写全限定类名或者别名. mybatis已经给常用的数据类型起好了别名,参考mybatis.pdf  3.1.1小节: 基本数据类型很少用,一 ...

  3. 一百零七、SAP的OO-ALV之一,新建程序

    一.来带SE38模块,新建一个Z_TIANPAN_20190807_OOALV的本地程序 二.设置一个标题,点击对勾 三.选择保存为本地对象 我们下一篇来写创建屏幕

  4. HDU 4902 Nice boat 多校4 线段树

    给定n个数 第一个操作和普通,区间覆盖性的,把l-r区间的所有值改成固定的val 第二个操作是重点,输入l r x 把l-r区间的所有大于x的数,变成gcd(a[i],x) a[i]即指满足条件的序列 ...

  5. NumPy 数组切片

    章节 Numpy 介绍 Numpy 安装 NumPy ndarray NumPy 数据类型 NumPy 数组创建 NumPy 基于已有数据创建数组 NumPy 基于数值区间创建数组 NumPy 数组切 ...

  6. vue使用Vant UI中的swiper组件及传值

    子组件SwiperBanner <!-- --> <template> <div class="swiper"> <van-swipe : ...

  7. React全局浮窗、对话框

    下面代码是组件源码: import React, {Component} from 'react' import {createPortal} from 'react-dom' import styl ...

  8. mysql数据库管理、常用命令及函数(10.10 第十八天)

    数据库管理: MYSQL 轻量级数据库,适用于中小型企业,性能好,开源的(免费的) MSSQL 微软开发的,需要安装在NT系统中,不支持跨平台,适用于中大型企业 ACCESS 小巧方便,适用于小型企业 ...

  9. POJ 2996:Help Me with the Game

    Help Me with the Game Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64 ...

  10. JAVAEE 和项目开发(第三课:HTTP的请求头和请求方式)

    HTTP 协议之请求格式   请求格式的结构:请求行:请求方式.请求的地址和 HTTP 协议版本 请求头:消息报头,一般用来说明客户端要使用的一些附加信息 空行: 位于请求行和请求数据之间,空行是必须 ...