[Algo] 118. Array Deduplication IV
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的更多相关文章
- [Algo] 117. Array Deduplication III
Given a sorted integer array, remove duplicate elements. For each group of elements with the same va ...
- [Algo] 115. Array Deduplication I
Given a sorted integer array, remove duplicate elements. For each group of elements with the same va ...
- JavaScript library of crypto standards. 看源码
crypto-js - npm https://www.npmjs.com/package/crypto-js crypto-js/docs/QuickStartGuide.wiki <wiki ...
- IOS懒加载
1.懒加载基本 懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内存小).所谓懒加载,写的是其get方法. 注意:如果是懒加载的话则一定要注意先判断是否已经有了,如果没有那么再去进行实例化 ...
- IOS开发之--UIScrollView pagingEnabled自定义翻页宽度
用到UIScrollview的翻页效果时,有时需要显示一部分左右的内容,但是UIScrollView的PagingEnabled只能翻过整页,下面几个简单的设置即可实现 技术点: 1. 创建一个继承U ...
- python 常用模块 Top200
名次 模块名称 被使用项目数 1 sys 7858 2 os 6983 3 re 5663 4 time 5268 5 random 3339 6 datetime 3310 7 setuptools ...
- Python数据分析之numpy学习
Python模块中的numpy,这是一个处理数组的强大模块,而该模块也是其他数据分析模块(如pandas和scipy)的核心. 接下面将从这5个方面来介绍numpy模块的内容: 1)数组的创建 2)有 ...
- PHP世纪万年历
<? //世纪万年历 #这是唯一的设置-请输入php文件的位置 $file="http://192.168.1.168/php/rl/s2m.php"; //#农历每 ...
- Numpy系列(三)- 基本运算操作
Numpy 中数组上的算术运算符使用元素级别.最后的结果使用新的一个数组来返回. import numpy as np a = np.array( [20,30,40,50] ) b = np.ara ...
随机推荐
- Tunning spark
Data Serialization 对spark程序来说,可能会产生的瓶颈包括:cpu,网络带宽,内存 在任何分布式应用中数据序列化都非常重要,数据序列化带来的作用是什么?第一减少内存占用,第二减小 ...
- Impala 笔记
简介 Cloudera公司推出,提供对HDFS.Hbase数据的高性能.低延迟的交互式SQL查询功能. 基于Hive使用内存计算,兼顾数据仓库.具有实时.批处理.多并发等优点 是CDH平台首选的PB级 ...
- Day 20:网络编程(1)
什么是计算机网络? 指的是分布在不同地域的计算机,通过外部设备连接起来,实现资源共享与数据传输的计算机系统. 通信三要素: IP: IP地址 Internet上的每台主机(Host)都有一个唯一的IP ...
- 【剑指Offer】面试题12. 矩阵中的路径
题目 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一格开始,每一步可以在矩阵中向左.右.上.下移动一格.如果一条路径经过了矩阵的某一格,那么该路径 ...
- MySQL 字符集和校验规则工作流程
MySQL 字符集和校验规则工作原理 字符编码相关参数 数据流中的转码过程 校验规则 Tips:字符集和校验规则总是相伴的 一 从简单的建库语句开始 CREATE DATABASE [IF NOT E ...
- react-native-tab-view 导航栏切换插件讲解
首先引入插件 yarn add react-native-tab-view 如果用的原生环境要安装另外几个插件 yarn add react-native-reanimated react-nativ ...
- Spring Boot without the web server
https://stackoverflow.com/questions/26105061/spring-boot-without-the-web-server/28565277 1. spring.m ...
- Mac下使用Hexo搭建个人博客
Hexo介绍 利用原作者的一句话:A fast,simple&powerful blog framework,powered by Node.js Hexo是基于Node.js的博客平台,He ...
- Android自定义View——QQ音乐中圆形旋转碟子
1.在onMeasure中测量整个View的宽和高后,设置宽高 2.获取我们res的图片资源后,在ondraw方法中进行绘制圆形图片 3.通过Handler发送Runnable在主线程中更新UI,达到 ...
- python----linux下简单的排序
1.选择排序:把一个数与余下所有的数排序,最小的排到最前面 [root@besttest liyn_test]# cat test.py #! /usr/bin/python a=[,,,] ,len ...