[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 ...
随机推荐
- 二、JavaScript之点击按钮改变HTML样式 (CSS)
一.代码如下 二.点击前 三.点击后 <!DOCTYPE html> <html> <meta http-equiv="Content-Type" c ...
- 干货分享:深度解析Supplement Essay写作
今天Hotessay小编给同学们介绍下附加文书的创作思路.因为附加文书基本上都是短essay,所以简洁才是硬道理! 通常,我们可以把美国大学的附加文书分为以下几类: 1.Tell us about y ...
- centos7-虚拟机 主机 互通 静态ip网络设置
由于目前互联网发展的速度之快.用户量之多,很多时候作为服务端单台服务器的硬件配置已经不足以支撑业务.集群.分布式等技术架构变得越来越普及,作为开发人员也有必要掌握相关技能.笔者打算选用virtual ...
- python---函数定义、调用、参数
1.函数定义和调用 下面def test部分的代码块是函数定义:test(2,3)则是函数调用 def test(a,b): print(a) print(b) test(,) 2.必填参数,即函数调 ...
- c++ opencv 数学函数示例
// ConsoleApplication11.cpp : Defines the entry point for the console application. // #include " ...
- Egret Engine 2D - 矢量绘图
绘制矩形 drawRect 绘制矩形边 lineStyle( 10, 0x00ff00 清空绘图 clear 绘制园形 drawCircle 绘制直线 moveTo lineTo 绘制曲线 cur ...
- GitHub 网站上不去/加载慢/加载不全 解决办法
1. 当你打开你的 GitHub 2. F12 进入检查页面,点击 network 3. 找到变红的字段右键复制连接 4. 打开 DNS 查询网站,输入你复制的网址,点击查询 5. 选择国内的 ip ...
- 花了一周整理的,这是价值10W的32个Python项目!
今天为大家整理了32个Python爬虫项目.整理的原因是,爬虫入门简单快速,也非常适合新入门的小伙伴培养信心.所有链接指向GitHub,祝大家玩得愉快~QQSpider ! QQ空间爬虫,包括日志.说 ...
- 数据库连接池C3P0的使用
一.直接使用代码链接(一般企业开发不会用到.大多数用方法二) 1.导入jar 2.新建JDBCUtil import java.io.FileInputStream; import java.io.I ...
- 第7章,c语言控制语句:分支和跳转
7.1 if语句 通用形式:if(expression) statment 7.2 if else语句 通用形式:if(expression) startment else startment2 7. ...