LeetCode-Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice? For example,
Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't matter what you leave beyond the new length.
感觉这道题的oa有点问题。。。先这样吧
public class Solution {
public int removeDuplicates(int[] nums) {
int res=0;
int len=nums.length;
int set=0;
for(int i=0; i<len; i++){
if(i==0){
set++;
res++;
}
else{
if(nums[i]==nums[i-1]){
set++;
if(set<=2){
res++;
}
}
else{
set=1;
res++;
}
}
}
return res;
}
}
要in place 改变原数组:
public class Solution {
public int removeDuplicates(int[] nums) {
if(nums==null || nums.length==0){
return 0;
}
int startPosition=0;
boolean isRepeated=false;
for(int i=1; i<nums.length; i++){
if(nums[i] != nums[startPosition]){
isRepeated=false;
startPosition++;
nums[startPosition] = nums[i];
}
else{
if(isRepeated == false){
startPosition++;
nums[startPosition]=nums[i];
isRepeated=true;
}
}
}
return startPosition+1;
}
}
LeetCode-Remove Duplicates from Sorted Array II的更多相关文章
- [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 (C++)
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- [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 II 删除整型数组中的重复元素并返回剩下元素个数2
class Solution { public: int removeDuplicates(int A[], int n) { ],*e=&A[]; //s指向“连续数字”的第一个,e往后遍历 ...
- 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 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
随机推荐
- Oracle存储过程动态创建临时表/存储过程执行权限问题--AUTHID CURRENT_USER
关于Oracle存储过程执行权限问题的解决 http://blog.sina.com.cn/s/blog_6ceed3280101hvlo.html (2014-04-02 04:06:28) 转载▼ ...
- Scala编程--基本类型和操作
如果你熟悉Java,你会很开心地发现Java基本类型和操作符在Scala里有同样的意思.然而即使你是一位资深Java开发者,这里也仍然有一些有趣的差别使得本章值得一读.因为本章提到的一些Scala的方 ...
- 火狐和IE浏览器的兼容问题汇总
1.window.event code=(navigator.appName="Netscape")?event.which:event.keycode; 2.event.x mx ...
- CenOS 6.5下安装docker(转)
2014-12-15 10:23 blessed24 To be Done的博客 字号:T | T 最近在自己的centos上搭建了一个Docker,顺便将一些常用操作记录下. AD:51CTO网+ ...
- Theoretical comparison between the Gini Index and Information Gain criteria
Knowledge Discovery in Databases (KDD) is an active and important research area with the promise for ...
- windows上传代码到github
上传代码到github上有很多种方法,在这里我介绍一种比较简单的一种.工具嘛,越简单越好用啊. 1.首先下载github在windows下的客户端 下载地址:https://desktop.githu ...
- ViewPager实现自动翻页功能 --转载出处找不到了,根据自己的理解写个随笔方便以后的记忆以及代码的共享,感谢给我启发的那位高手--第一次写博客哈
xml文件 textview 用于显示图片的标题 viewpager 用于实现翻页效果 <LinearLayout xmlns:android="http://schemas.andr ...
- AXUre
[ Javascript ] 一.javascript能用来干什么? 1.数据的验证. 2.对动态这本写到网页当中. 3.可以对事件做出响应. 4.可以读写html 中的内室. 5.可以检测浏览器 6 ...
- linux 录制并回放终端会话
发现一个比较好玩的命令,然后这块做一下记录 以下内容复制来源于 LINUX shell 脚本攻略第二版 当你需要为别人在终端上演示某些操作或是需要准备一个命令行教程时,通常得一边手动输入命令一边演示, ...
- 1.3 PROGRAM DEVELOPMENT ENVIRONMENT
1.3 PROGRAM DEVELOPMENT ENVIRONMENT 1.4 WIN32 EXECUTEABLE FILE FORMAT We should also know that compl ...