80Remove Duplicates from Sorted Array II
来源:互联网 发布:64码高清网络电视apk 编辑:程序博客网 时间:2024/05/22 14:38
题目链接:https://leetcode.com/problems/remove-duplicates-from-sorted-list/
题目:
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.
解题思路:
这题比较简单,维护一个对相同数字的计数值,当计数值大于 2 时,就跳过该元素。
同时,还要维护一个存放当前元素的下标位置,由于前面的元素可能存在计数值大于 2 的情况,遂要把后面的元素往前挪。
特别指出,题目只关心挪动元素后新数组的前 length 个元素
代码实现:
public class Solution { public int removeDuplicates(int[] nums) { if(nums == null || nums.length == 0) return 0; int n = 1; int tempN = 1; int index = 0; for(int i = 1; i < nums.length; i ++) { if(nums[i] != nums[i - 1]) { n ++; tempN = 1; nums[++ index] = nums[i]; } else { if(tempN < 2) { n ++; nums[++ index] = nums[i]; } tempN ++; } } return n; }}
164 / 164 test cases passed.Status: AcceptedRuntime: 2 ms
0 0
- 80 Remove Duplicates from Sorted Array II
- 80Remove Duplicates from Sorted Array II
- [80]Remove Duplicates from Sorted Array II
- 80Remove Duplicates from Sorted Array II
- 80 - Remove Duplicates from Sorted Array II
- LeetCode-Array-80 Remove Duplicates from Sorted Array II
- LeetCode: Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array II
- LeetCode: Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array II
- [LeetCode] Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- [leetcode] Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- Remove duplicates from sorted array II
- Remove Duplicates from Sorted Array II
- [LeetCode]Remove Duplicates from Sorted Array II
- hibernate抓取策略(fetch)
- Objective-C:数组排序、过滤
- Android Studio安装的时候遇到的一些问题解决方法
- Spring MVC controller 读取配置文件
- autorun file for ohasd is missing
- 80Remove Duplicates from Sorted Array II
- Apache Commons IO入门教程
- poj 2955【区间dp】
- 学习git小结(二)
- node项目上线配置流程
- Franklin let me know
- 0 or 1(最短路转化)
- android自定义控件
- WSGI规范细则