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 SDK之 FrameBuffer

    The Vulkan Framebuffer Framebuffers represent a collection of memory attachments that are used by a ...

  2. 042-PHP使用闭包函数递归无限级分类

    <?php //使用闭包函数递归无限级分类 function demo($array){ # 用于存储递归后的队列 $data = []; # 递归函数 $func = function (&a ...

  3. 操作系统类型&操作系统结构&现代操作系统基本特征

    五大类型操作系统 (1). 批处理操作系统 用户脱机使用计算机 用户提交作业之后直到获得结果之前就不再和计算机打交道. 作业提交的方式可以是直接交给计算中心的管理操作员,也可以是通过远程通讯线路提交. ...

  4. maven 依赖报错

    1 maven项目,在Intellij 右侧 Maven projects - Lifecycle - clean , validate, compile, ….,右击clean,选中Run ‘pro ...

  5. 六、CI框架之分配变量

    一.在controllers里面添加 $this->load->vars('m_Str1','我是一个字符串变量'); 二.在View中添加相应代码 界面显示效果如下: 不忘初心,如果您认 ...

  6. 吴裕雄--天生自然Django框架开发笔记:Django Nginx+uwsgi 安装配置

    Django Nginx+uwsgi 安装配置 使用 python manage.py runserver 来运行服务器.这只适用测试环境中使用. 正式发布的服务,需要一个可以稳定而持续的服务器,比如 ...

  7. Maven - Eclipse例子

    版权所有,未经授权,禁止转载 章节 Maven – 简介 Maven – 工作原理 Maven – Repository(存储库) Maven – pom.xml 文件 Maven – 依赖管理 Ma ...

  8. Hive鲜为人知的宝石-Hooks

    本来想祝大家节日快乐,哎,无奈浪尖还在写文章.谴责一下,那些今天不学习的人.对于今天入星球的人,今天调低了一点价格.减少了20大洋.机不可失失不再来.点击阅读原文或者扫底部二维码. hive概述 Hi ...

  9. spring_mybatis :整合

    第一步:导入相关架包(使用maven构建项目) 在pom.xml文件中导入相关依赖 1.Junit测试架包 <dependency> <groupId>junit</gr ...

  10. 中后缀表达式/洛谷P1175 表达式的转换

    P1175 表达式的转换 思路:先用栈转成中缀表达式,再用栈进行计算.要输出过程,因此计算一次输出一次,但是栈没有迭代器,不好用,换成vector(可以pop_back).虽然表达式求值也可以这么做, ...