[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);
}
}
[Algo] 115. Array Deduplication I的更多相关文章
- [Algo] 118. Array Deduplication IV
Given an unsorted integer array, remove adjacent duplicate elements repeatedly, from left to right. ...
- [Algo] 117. Array Deduplication III
Given a sorted integer array, remove duplicate elements. For each group of elements with the same va ...
- 使用Python一步一步地来进行数据分析总结
原文链接:Step by step approach to perform data analysis using Python译文链接:使用Python一步一步地来进行数据分析--By Michae ...
- 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 ...
- numpy&pandas基础
numpy基础 import numpy as np 定义array In [156]: np.ones(3) Out[156]: array([1., 1., 1.]) In [157]: np.o ...
- 小白眼中的AI之~Numpy基础
周末码一文,明天见矩阵- 其实Numpy之类的单讲特别没意思,但不稍微说下后面说实际应用又不行,所以大家就练练手吧 代码裤子: https://github.com/lotapp/BaseCode ...
- numpy快速入门
numpy快速入门 numpy是python的科学计算的核心库,很多更高层次的库都基于numpy.博主不太喜欢重量级的MATLAB,于是用numpy进行科学计算成为了不二选择. 本文主要参考Scipy ...
- php 阳历转农历优化版
网上转换方法很多例子错误. 测试例子1:输入公历 2010年2月1号测试,对比百度万年历 农历应该为己丑年(2009)腊月(12月)十八. 测试例子2:输入农历1990.11.初十,丑时,公历应该为1 ...
随机推荐
- java流程控制语句要点
java流程控制语句要点 一.java7增强后的switch switch语句后面的控制表达式的数据类型只能是byte.short.char.int四种整数类型,不能是boolean类型,java7以 ...
- tkinter中控件menu的两种组织方法
tkinter中,菜单控件组织方法有两种,使用中常出现混淆,为明晰各个正确用法,特整理撰写此博文.菜单控件的组织实际上是通过一个“母菜单”和“子菜单”构成,“母菜单”一方面与master连接(即与依附 ...
- css mix-blend-mode 颜色滤镜混合模式
CSS3混合模式种类 在CSS3混合模式中,目前仅有16种:normal,multiply,screen,overlay,darken,lighten,color-dodge,color-burn,h ...
- android 开发学习3
DAO:DATA ACCESS OBJECT getApplication()和MainActivity.this 是两种不同的context,也是最常见的两种.第一种中context的生命周期与Ap ...
- 【分布式】流式计算Storm框架
Storm简介: Storm起源Twitter开源的一个类似于Hadoop的实时数据处理框架,不过两则还是有区别的,Hadoop是批量处理数据,而Storm处理的是实时的数据流. Storm应用场景: ...
- java初学小项目-酒店客房管理系统
最近初次接触JAVA,感觉之前学的C语言很有用,跟着视频做了一个小项目-酒店客房管理系统 /* 酒店客房管理系统 */ import java.util.Scanner;//通过键盘来输入命令需要的引 ...
- Linux下C程序的内存映像
2.Linux下C程序的内存映像 2.1. 代码段.只读数据段(1)对应着程序中的代码(函数),代码段在Linux中又叫文本段(.text)(2)只读数据段就是在程序运行期间只能读不能写的数据,con ...
- 【LeetCode】组合总和
[问题]给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制 ...
- DFS+BFS(广度优先搜索弥补深度优先搜索遍历漏洞求合格条件总数)--09--DFS+BFS--蓝桥杯剪邮票
题目描述 如下图, 有12张连在一起的12生肖的邮票.现在你要从中剪下5张来,要求必须是连着的.(仅仅连接一个角不算相连) 比如,下面两张图中,粉红色所示部分就是合格的剪取. 请你计算,一共有多少 ...
- Python Learning Day8
bp4解析库 pip3 install beautifulsoup4 # 安装bs4pip3 install lxml # 下载lxml解析器 html_doc = """ ...