[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程序中常用概念介绍
一.关键字.引用.直接量.变量.长量概念 1.关键字 java内部定义的java语言专用的单词,这些单词具有特殊含义,开发人员在定义自己声明的名称时,应该避开这些专用的单词.这些专用的单词也就称之为j ...
- Elasticsearch 更新文档
章节 Elasticsearch 基本概念 Elasticsearch 安装 Elasticsearch 使用集群 Elasticsearch 健康检查 Elasticsearch 列出索引 Elas ...
- 数据结构必做题参考:实验一T1-20,实验2 T1
实验一T1-10 #include <bits/stdc++.h> using namespace std; ; struct Book { string isbn; string nam ...
- (六--二)scrapy框架之持久化操作
scrapy框架之持久化操作 基于终端指令的持久化存储 基于管道的持久化存储 1 基于终端指令的持久化存储 保证爬虫文件的parse方法中有可迭代类型对象(通常为列表or字典)的返回,该返回值可以通过 ...
- Java进阶之路 - 1.走近Java
Java进阶之路 - 1.走近Java 一.走近Java思维导图
- PHP的操作符与控制结构
一.操作符 操作符是用来对数组和变量进行某种操作运算的符号. 算术操作符 操作符 名称 示例 + 加 $a+$b - 减 $a-$b * 乘 $a*$b / 除 $a/$b % 取余 $a%$b 复 ...
- svn报错:“Previous operation has not finished; run 'cleanup' if it was interrupted“
今天在eclipse上使用SVN:team - 显示资源历史记录 的时候报错. 方法是在本地磁盘项目目录上右键TortoiseSVN - Clean up 我的弹出的界面和下面一样,请勾选Break ...
- python格式字符
- spring+springMVC+mybatis , 项目启动遇坑
github上找的框架组合例子 结合自己的数据库作为新项目开发. 但是项目启动时,tomcat启动失败: 检查不出错误. 于是改换maven引入jetty插件来启动项目, 结果在未改动的任何代码的情况 ...
- swift之保存数据到keychain
访问KeyChain 1.在mac上按下 Command+Space 输入Keychain Access 2.在终端输入security find-generic-password -help 读取配 ...