去掉有序数组中重复数字 原地 leetcode java (最简单的方法)
1.利用荷兰国旗的思路,每次记住最后一个位置,遇到一个不重复的数,放在它后面,代码很简单。
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 Solution {
public int removeDuplicates(int[] A) {
int len=A.length;
if(len==0) return 0;
int pos=0; for(int i=1;i<len;i++)
{
if(A[i]!=A[pos])
{
pos++;
A[pos]=A[i]; } }
return pos+1; } }
2.只要跟end的前一个比较就行了,思路跟上边,就一点不一样,原创啊
Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?
For example,
Given sorted array A = [1,1,1,2,2,3]
,
Your function should return length = 5
, and A is now [1,1,2,2,3]
.
public class Solution {
public int removeDuplicates(int[] A) {
int len=A.length;
if(len==0) return 0;
if(len==1) return 1;
int end=1;
for(int i=2;i<len;i++)
{
if(A[i]!=A[end-1])
{
end++;
A[end]=A[i]; } } return end+1; }
}
去掉有序数组中重复数字 原地 leetcode java (最简单的方法)的更多相关文章
- LeetCode 26 Remove Duplicates from Sorted Array (移除有序数组中重复数字)
题目链接: https://leetcode.com/problems/remove-duplicates-from-sorted-array/?tab=Description 从有序数组中移除重 ...
- 去掉有序数组中的重复元素 c/c++
去掉有序数组中的重复元素: int RemoveDuplates(int A[], int nCnt) { ; ; , j = ; i < nCnt && j < nCnt ...
- 【LeetCode每天一题】Remove Duplicates from Sorted Array II(移除有序数组中重复的两次以上的数字)
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- 剑指offer:1.找出数组中重复的数(java版)
数组中重复的数:题目:找出数组中重复的数,题目描述:在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任 ...
- 高效率去掉js数组中重复项
Array类型并没有提供去重复的方法,如果要把数组的重复元素干掉,那得自己想办法: function unique(arr) { var result = [], isRepeated; for (v ...
- 求一个数组中重复数字的个数,要求复杂度为O(n)
给出代码 #include <stdio.h> #include <unistd.h> #include <iostream> #include <memor ...
- (Java)怎么去掉字符串数组中重复的值?
String fdbs = "WXB,WXA,FDA,WXB"; String[] str = fdbs.split(","); Set set = new H ...
- (python)剑指Offer:数组中重复的数字
问题描述 在长度为n的数组中,所有的元素都是0到n-1的范围内. 数组中的某些数字是重复的,但不知道有几个重复的数字,也不知道重复了几次,请找出任意重复的数字. 例如,输入长度为7的数组{2,3,1, ...
- [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)
[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...
随机推荐
- PAT_1040 有几个PAT
问题描述: 字符串APPAPT中包含了两个单词“PAT”,其中第一个PAT是第2位(P),第4位(A),第6位(T):第二个PAT是第3位(P),第4位(A),第6位(T). 现给定字符串,问一共可以 ...
- (转)Object-C 中各数据类型转换 NSData转NSString,Byte,UIImage
,NSData 与 NSString NSData --> NSString NSString *aString = [[NSString alloc] initWithData:adata e ...
- Lambda Expression In Java
题记在阅读JDK源码java.util.Collections的时候在UnmodifiableCollection类中看到了这么一段代码: public void forEach(Consumer& ...
- linux环境下安装php扩展
本文只提供源码安装的方法(已安装pcntl为例) 其他方法请参考:http://doc3.workerman.net/appendices/install-extension.html 1.利用php ...
- [转]left join,right join,inner join区别
left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录inner join(等值连接) 只 ...
- php提取背景图片
preg_match_all('/background\s*-\s*+image\s*:\s*url\s*\("*([^"]*)"*\)/i', $content,$ma ...
- Python全栈开发之MySQL(三)视图,存储过程触发器,函数,事务,索引
一:视图 1:什么是视图? 视图是指存储在数据库中的查询的SQL语句,具有简单.安全.逻辑数据独立性的作用及视点集中简化操作定制数据安全性的优点.视图包含一系列带有名称的列和行数据.但是,视图并不在数 ...
- ios:如何将自己编写的软件放到真正的iPhone上运行(转)
想要将自己编写的软件放到真正的iPhone上去运行,首先你需要成为Apple Developer计划的成员.其次,你需要设置程序ID和认证书,在这之后你就可以在你指定的iPhone上运行你的程序了.下 ...
- ZOJ 2562 More Divisors
又是个水题,刚刚开始没有用搜索,因为对于反素数有: n=2^t1*3^t2^5^t3*7^t4..... 这里有 t1>=t2>=t3>=t4. 而且相同的因数的情况下,素数越不同越 ...
- csu 10月 月赛 J 题
Description CSU又到了一年中评奖学金的时候了……各大学霸都或多或少地拿到了各种奖学金(你们自己看着办吧). 在这里,评奖学金有个很奇怪的规矩——每个同学得到的奖学金数一定满足相邻的两个非 ...