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. 三、jsx简化教程

    1)使用 JSX 的好处 1.提供更加语意化且易懂的标签 与html对比 <!--HTML写法--> <form class="messageBox"> & ...

  2. CSU-ACM2020寒假集训比赛2

    A - Messenger Simulator CodeForces - 1288E 两种解法,我选择了第二种 mn很好求,联系过就是1,没联系过就是初始位置 第一种:统计同一个人两次联系之间的出现的 ...

  3. [ACTF2020 新生赛]BackupFile

    0x00 知识点 备份文件 index.php.bak str弱相等被转化为整型 0x01解题 根据提示下载备份文件得到源码 看到==弱相等 且被强制转为整型 http://7d5cccc5-4ecd ...

  4. Ubuntu Vi指令

    Ubuntu在不更新源的情况下是没办法使用Vim指令的只能使用Vi指令 所有我也就记录了下来 vi / vim命令: 插入内容: i: 插入光标前一个字符 I: 插入行首 a: 插入光标后一个字符 A ...

  5. Egret Engine 2D - 显示对象

        alpha:透明度 width:宽度 height:高度 rotation:旋转角度 scaleX:横向缩放 scaleY:纵向缩放 skewX:横向斜切 skewY:纵向斜切 visible ...

  6. Toolbar融入状态栏实现沉浸式遇到的问题

    这里写一个纠结我一下午的问题,目的是写一个toolbar和状态栏相融合的沉浸式的样子,遇到各种各样的问题,还好最后慢慢解决了. 一.首先在活动中将状态栏设为透明 @Override protected ...

  7. DevOps专题|基础Agent部署系统

    随着京东云业务规模.管理机器规模的扩大,各类agent也在逐渐增多,如日志agent.监控agent.控制系统agent等.这对agent的部署.升级.状态维护提出了很高的要求,一旦某个全局agent ...

  8. 201903-1 小中大 Java

    思路: 中位数就是排序后中间的那个数.如果有偶数个数,就是中间两个数的平均值. 注意,这个平均值可能是整数,可能是小数,如果都是一样的处理,如果输出整数是3.0,而不是3,就有问题.所以需要分开处理. ...

  9. mybatis的批量update

    方法有三种:1.通过java代码batch方式,xml文件只需一条update语句.java代码繁琐 2.xml使用foreach,“;”分割多条update语句,要求:jdbc的url需加上allo ...

  10. no.9亿级用户下的新浪微博平台架构读后感

    微博平台的第三代技术体系,使用正交分解法建立模型:在水平方向,采用典型的三级分层模型,即接口层.服务层与资源层:在垂直方向,进一步细分为业务架构.技术架构.监控平台与服务治理平台. 水平分层 (1)接 ...