Leetcode刷题记—— Remove Duplicates from Sorted Array II(已排序数组移除重复元素2)
2017-03-04 17:48
741 查看
一、题目叙述:
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.
Subscribe to see which companies asked this question.
排序数组中一个元素最多只能重复2次,将多余的移除
二、解题思路:
Medium题。
思路:
(1)倒着遍历数组,若当前元素与其前前一个元素的值不同,计数加1;否则,将后续元素前移。
(2)注意当数组元素小于等于2的情况。
三、源码:
import java.util.Arrays;
public class Solution
{
public int removeDuplicates(int[] nums)
{
if (nums.length <= 2) return nums.length;
int count = 2;
for (int i = nums.length - 1; i >= 2; i--)
{
if (nums[i] != nums[i - 2])
count ++;
else
for (int j = i; j < nums.length - 1; j ++)
nums[j] = nums[j + 1];
}
System.out.println(Arrays.toString(nums));
return count;
}
public static void main(String args[])
{
Solution solution = new Solution();
int[] nums = {1, 1, 1, 2, 2, 3};
System.out.println(solution.removeDuplicates(nums));
}
}
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.
Subscribe to see which companies asked this question.
排序数组中一个元素最多只能重复2次,将多余的移除
二、解题思路:
Medium题。
思路:
(1)倒着遍历数组,若当前元素与其前前一个元素的值不同,计数加1;否则,将后续元素前移。
(2)注意当数组元素小于等于2的情况。
三、源码:
import java.util.Arrays;
public class Solution
{
public int removeDuplicates(int[] nums)
{
if (nums.length <= 2) return nums.length;
int count = 2;
for (int i = nums.length - 1; i >= 2; i--)
{
if (nums[i] != nums[i - 2])
count ++;
else
for (int j = i; j < nums.length - 1; j ++)
nums[j] = nums[j + 1];
}
System.out.println(Arrays.toString(nums));
return count;
}
public static void main(String args[])
{
Solution solution = new Solution();
int[] nums = {1, 1, 1, 2, 2, 3};
System.out.println(solution.removeDuplicates(nums));
}
}
相关文章推荐
- LeetCode 26 Remove Duplicates from Sorted Array(从已排序数组中移除重复元素)
- LeetCode 26 Remove Duplicates from Sorted Array(从已排序数组中移除重复元素)
- LeetCode: Remove Duplicates from Sorted Array II(在排序数组中删除重复元素)
- Remove Duplicates from Sorted Array II 从有序数组里移除重复出现元素,最多重复出现2次 @LeetCode
- leetcode 题解:Remove Duplicates from Sorted Array II(已排序数组去三次及以上重复元素)
- [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素
- leetcode 4. 移除有序数组中的重复元素 Remove Duplicates from Sorted Array
- LeetCode 80 Remove Duplicates from Sorted Array II(移除数组中出现两次以上的元素)
- LeetCode OJ:Remove Duplicates from Sorted Array II(移除数组中的重复元素II)
- [Leetcode] Remove duplicates from sorted array 从已排序的数组中删除重复元素
- LeetCode-26-Remove Duplicates from Sorted Array(消除已排序数组中的重复元素)
- LeetCode Remove Duplicates from Sorted Array II 删除整型数组中的重复元素并返回剩下元素个数2
- leetcode:Remove Duplicates from Sorted Array去掉排序数组中重复的元素
- Remove Duplicates from Sorted Array——移除排序数组中重复元素
- Lettcode_26_Remove Duplicates from Sorted Array——移除排序数组中重复元素
- Leetcode26:Remove Duplicates from Sorted Array(移除已排序数组中重复的数字)
- Leetcode刷题记——26. Remove Duplicates from Sorted Array(删除数组重复元素)
- [LeetCode-26] Remove Duplicates from Sorted Array(移除数组重复元素)
- leetcode 26. Remove Duplicates from Sorted Array从排序数组中移出重复元素(双指针)
- 【LeetCode-面试算法经典-Java实现】【026-Remove Duplicates from Sorted Array(删除排序数组中的重复元素)】