[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 ...
随机推荐
- 2.13 ViewModel 使数据即使在后台被杀死数据也能存活
操作程序如下: 主要是创建一个 SavedStateHandle 来缓存上一次的数据,并通过重新读取上一次存储的数据来实现数据的存活 MyVIewModel: package com.example. ...
- Vue.js(24)之 弹窗组件封装
同事封装了一个弹窗组件,觉得还不错,直接拿来用了: gif图展示: 弹框组件代码: <template> <transition name="confirm-fade&qu ...
- JAVA - 创建SpringBoot项目
JAVA - 创建SpringBoot项目 Spring Boot是由Pivotal团队提供的全新框架,设计目的是用来简化新Spring应用的初始搭建以及开发过程.它主要推崇的是'消灭配置’,实现零配 ...
- 十四、 React路由(react-router4.x): 动态路由、get传值、React中使用url模块
概述 新闻列表 -跳转-> 详情页 时,想把列表对应的id传到详情页里,可用到三种传值方法: 1.动态路由传值 2.get传值 3.localstorage传值 一.动态路由传值 [App.js ...
- SciKit-Learn 使用matplotlib可视化数据
章节 SciKit-Learn 加载数据集 SciKit-Learn 数据集基本信息 SciKit-Learn 使用matplotlib可视化数据 SciKit-Learn 可视化数据:主成分分析(P ...
- 解决Tomcat在idea控制台乱码问题
解决Tomcat乱码问题 打开Tomcat安装目录:apache-tomcat-9.0.14-windows-x64/conf/logging.properties 大概在50多行注释这一句#java ...
- 如何在Ubuntu 18.04上安装和卸载TeamViewer
卸载命令:sudo apt --purge remove teamviewer 安装:https://www.linuxidc.com/Linux/2018-05/152282.htm 如何在Ubun ...
- 用Spring中的ResponseEntity文件批量压缩下载
我看了很多网上的demo,先生成ZIP压缩文件,然后再下载. 我这里是生成ZIP文件流 进行下载.(核心代码没多少,就是一些业务代码) @RequestMapping(value = "/& ...
- [题解] Luogu P4721 【模板】分治 FFT
分治FFT的板子为什么要求逆呢 传送门 这个想法有点\(cdq\)啊,就是考虑分治,在算一段区间的时候,我们把他分成两个一样的区间,然后先做左区间的,算完过后把左区间和\(g\)卷积一下,这样就可以算 ...
- 吴裕雄--天生自然 JAVASCRIPT开发学习:运算符
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...