leetcode — remove-duplicates-from-sorted-array
import java.util.Arrays;
/**
* Source : https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/
*
*
* Given a sorted array, remove the duplicates in place such that each element appear
* only once and return the new length.
*
* Do not allocate extra space for another array, you must do this in place with constant memory.
*
* For example,
* Given input array A = [1,1,2],
*
* Your function should return length = 2, and A is now [1,2].
*/
public class RemoveDuplicates {
/**
*
* 因为是有序的,所以如果有重复的元素一定是相邻的,只要判断相邻的不相等的个数就知道不重复的元素个数
*
* @param num
* @return
*/
public int remove (int[] num) {
int pos = 0;
for (int i = 0; i < num.length - 1; i++) {
if (num[i] != num[i + 1]) {
num[pos] = num[i];
pos++;
}
}
System.out.println(Arrays.toString(num));
// 另外加上最后一个元素
return pos + 1;
}
public static void main(String[] args) {
RemoveDuplicates removeDuplicates = new RemoveDuplicates();
int[] arr = new int[]{1,1,2};
int[] arr1 = new int[]{1,1,1,2};
int[] arr2 = new int[]{1,1,22,22,22,33};
System.out.println(removeDuplicates.remove(arr));
System.out.println(removeDuplicates.remove(arr1));
System.out.println(removeDuplicates.remove(arr2));
}
}
leetcode — remove-duplicates-from-sorted-array的更多相关文章
- LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- [Leetcode] Remove Duplicates From Sorted Array II (C++)
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- [LeetCode]Remove Duplicates from Sorted Array题解
Remove Duplicates from Sorted Array: Given a sorted array, remove the duplicates in place such that ...
- [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array II [27]
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- [leetcode]Remove Duplicates from Sorted Array II @ Python
原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 题意: Follow up for &quo ...
- [LeetCode] Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- LeetCode——Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
随机推荐
- 阿森纳vs托特纳姆热刺
阿森纳vs托特纳姆热刺之间进行的英格兰足球联盟杯比赛时间为2018年12月20日 03:45.本场比赛的亚盘初盘为阿森纳0.0,大小球初盘3.0,角球盘口10.5.在此之前,阿森纳和托特纳姆热刺两队进 ...
- BZOJ 3622
直接算 $a_i>b_i$ 对数恰为 $k$ 的不好算 那么可以先算 $a_i>b_i$ 对数至少 $k$ 的 这个排序后随便dp一下就好 那么再除了一下 用 $f_i$ 表示 $a_i& ...
- TortoiseGit的ssh key和Git的ssh key
情景模拟: 你使用Git+TortoiseGit对项目进行版本控制,本地库(自己电脑建立的.git)与远程库(如GitLab上建立)通信需要使用ssh验证,你用git生成公钥并保存到了Gitlab上, ...
- fiddler 应用
一 pc 端抓取 例:本地调试代码,转换域名,请求网络数据 1:设置代理,以smart header 为例 ip为 127.0.0.1 端口与自己的fillder一致,注意将不代理的地址列表做修改 ...
- PHP通过经纬坐标计算两个地址的距离
<?php /** *求两个已知经纬度之间的距离,单位为米 * *@param lng1,lng2 经度 * *@param lat1,lat2 纬度 * *@return float 距离,单 ...
- shell基础及变量
一 Shell概述 1.Shell的作用——命令解释器,“翻译官” shell作为一个人机接口,用于解释用户输入的命令,将命令解释为Linux内核可以执行的2进制代码,并将执行的结果返回在标准终端上. ...
- 再回首数据结构—数组(Golang实现)
数组为线性数据结构,通常编程语言都有自带了数组数据类型结构,数组存放的是有个相同数据类型的数据集: 为什么称数组为线性数据结构:因为数组在内存中是连续存储的数据结构,数组中每个元素最多只有左右两个方向 ...
- gridlayout代码注释
<div class="wrapper"> //定义一节或者一部分区域,它的css样式对应的css中class选择器的wrapper <div class=&qu ...
- ajax经典面试题
1.什么是AJAX,为什么要使用Ajax(请谈一下你对Ajax的认识) 什么是ajax: AJAX是“Asynchronous JavaScript and XML”的缩写.他是指一种创建交互式网页应 ...
- 使用COOKIE实现登录 VS 使用SESSION实现登录
注:本文使用的代码基于PHP,其他语言逻辑同理. 一:使用COOKIE实现登录验证 使用cookie实现登录的方式,主要通过一些单向的加密信息进行验证.比如admin用户登录了之后,服务端生成一个co ...