Medium 80题 Remove Duplicates from Sorted Array II
2016-10-02 06:00
274 查看
Question:
Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?
For example,
Given sorted array nums =
Your function should return length =
It doesn't matter what you leave beyond the new length.
SOLUTION:
public class Solution {
public int removeDuplicates(int[] nums) {
int i=0;
for(int j=0;j<=nums.length-1;j++)
{
if(i<2||nums[j]>nums[i-2])
{
nums[i]=nums[j];
i++;
}
}
return i;
}
}
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,
2and
3.
It doesn't matter what you leave beyond the new length.
SOLUTION:
public class Solution {
public int removeDuplicates(int[] nums) {
int i=0;
for(int j=0;j<=nums.length-1;j++)
{
if(i<2||nums[j]>nums[i-2])
{
nums[i]=nums[j];
i++;
}
}
return i;
}
}
相关文章推荐
- LeetCode-80-Remove Duplicates from Sorted Array II(递归)-Medium
- [leetcode 80, Medium] Remove Duplicates from Sorted Array II
- Lettcode_80_Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- Leetcode_80_Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 80 Remove Duplicates from Sorted Array II
- leetcode: 80. Remove Duplicates from Sorted Array II
- leetcode[80]:Remove Duplicates from Sorted Array II
- Middle-题目47:80. Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- Leetcode 80 Remove Duplicates from Sorted Array II
- Leetcode_80_Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- leetcode || 80、Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II LeetCode
- [80]Remove Duplicates from Sorted Array II
- 【leetcode】Array——Remove Duplicates from Sorted Array II(80)