[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 ...
随机推荐
- Mysql 存储过程造测试数据
1.Mysql 存储过程造测试数据 -- 创建一个用户表 CREATE TABLE `sys_user` ( -- `id` CHAR (32) NOT NULL DEFAULT '' COMMENT ...
- 028-PHP常用数学函数abs和acos和asin
<?php print(abs(-));//打印绝对值 // 从 -1 到1打印acos函数的值 print("<TABLE BORDER=\"1\"> ...
- 166-PHP 文本分割函数str_split(一)
<?php $str='programming'; //定义一个字符串 $arr=str_split($str); //将字符串分割并传入数组 print_r($arr); //输出数组详细信息 ...
- PHP数据库基础(简单的)
经常用到的函数 $link=mysql_connect("数据库地址","用户名","密码");//链接数据库mysql_query(“se ...
- ssh-keygen 签名ca证书
介绍 ssh-keygen命令用于为"ssh" 生成,管理和转换认证秘钥,支持RSA和DSA两种认证秘钥 生成秘钥对 ssh-keygen -b 2048 -C milo -f 2 ...
- getClass兼容ie
if (!document.getElementsByClassName) { document.getElementsByClassName = function(className, elemen ...
- P 1020 月饼
转跳点:
- 下页小希学MVC5+EF6.2 学习记录一
目的:1 学习mvc+ef 2 写下日记也是对自己的督促 第0课 从0开始 ASP.NET MVC开发模式和传统的WebForm开发模式相比,增加了很多"约定". 直接讲这些 & ...
- javascript中new操作符的原理
javascript中的new是一个语法糖,对于学过c++,java 和c#等面向对象语言的人来说,以为js里面是有类和对象的区别的,实现上js并没有类,一切皆对象,比java还来的彻底 new的过程 ...
- 兼容iphonex底部那个
@media only screen and (device-width: 375px) and (device-height: 812px) and (-webkit-device-pixel-ra ...