- 浏览: 183622 次
- 性别:
- 来自: 济南
文章分类
最新评论
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.
这道题目是Remove Duplicates from Sorted Array 的follow up。这里每个元素可以出现两次。我们只需要维护一个计数指针就可以了。当计数指针大于2的时候我们就需要忽略当前的元素,继续往下遍历。代码如下:
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.
这道题目是Remove Duplicates from Sorted Array 的follow up。这里每个元素可以出现两次。我们只需要维护一个计数指针就可以了。当计数指针大于2的时候我们就需要忽略当前的元素,继续往下遍历。代码如下:
public class Solution { public int removeDuplicates(int[] nums) { if(nums == null || nums.length == 0) return 0; int index = 0; int counter = 0; for(int i = 0; i < nums.length; i++) { if(i > 0 && nums[i] == nums[i - 1]) { if(counter >= 2) continue; else { nums[index ++] = nums[i]; counter ++; } } else { nums[index ++] = nums[i]; counter = 1; } } return index; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 429Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 581Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 674Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 844Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 783You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 705For a undirected graph with tre ...
相关推荐
int removeDuplicates(vector<int>& nums) { vector<int>::iterator iter = nums.begin(); while (nums.begin() != nums.end()) { vector<int>::iterator temp = iter; vector<int>::iterator temp2 = ++iter; ...
26.Remove_Duplicates_from_Sorted_Array删除有序数组中的重复项【LeetCode单题讲解系列
Remove Duplicates from Sorted Array 2 Remove Duplicates from Sorted Array II 3 Search in Rotated Sorted Array 4 Search in Rotated Sorted Array II 5 Median of Two Sorted Arrays 递归实现find kth 6 ...
python python_leetcode题解之080_Remove_Duplicates_from_Sorted_Array_II
javascript js_leetcode题解之80-remove-duplicates-from-sorted-array-ii.js
c c语言_leetcode题解之0080_remove_duplicates_from_sorted_array_ii.zip
- Remove Duplicates from Sorted Array II(删除排序数组中的重复项II) - Merge Sorted Array(合并两个有序数组) - Merge Sorted Array II(合并两个有序数组II) - Median(中位数) - Partition Array by...
c c语言_leetcode 0026_remove_duplicates_from_sorted_array.zip
java入门 java_leetcode题解之026_Remove_Duplicates_from_Sorted_Array
js js_leetcode题解之26-remove-duplicates-from-sorted-array.js
c语言入门 C语言_leetcode题解之26-remove-duplicates-from-sorted-array.c
Leetcode经典01背包 algo 1. 数据结构与算法 数组,链表,(串和序列) 堆,栈,队列 树,图 排序,搜索 贪心,回溯,动态规划 堆:一种完全二叉树,任意节点...II | | | 75 Sort Colors 计数排序 | | | 88 Merge So
7. 双指针技术:常用于处理有序数组或排序后的数据结构,如在Two Sum II、Remove Duplicates from Sorted Array II(删除排序数组中的重复项 II)中使用。 8. 数据结构设计问题:如Median of Two Sorted Arrays(两...
- **2.1.2 Remove Duplicates from Sorted Array II** - 与上一题类似,但允许每个元素最多出现两次。 - **2.1.3 Search in Rotated Sorted Array** - 给定一个旋转后的有序数组,找到某个元素的位置。 - 实现...
##### 2.1.2 Remove Duplicates from Sorted Array II **题目描述**:与上一题类似,但允许每个元素最多出现两次。 **解题思路**: 1. 使用类似的方法,但是增加了一个计数器 `count` 来记录当前元素出现的次数。 ...
1. 题目80:删除排序数组中的重复项 II (Remove Duplicates from Sorted Array II) 这道题要求在保持数组排序的前提下,删除连续重复的元素,只保留一个。解题的关键在于使用双指针,一个指向当前元素,一个指向下...
26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)| [C++](./C++/remove-duplicates-from-sorted-array.cpp) [Python](./Python/remove-duplicates...
删除重复排序数组运行此脚本$ node index.js 所有输入... log ( "Length = " + removeDuplicates ( nums ) ) ;console . log ( "Modified nums = [" + nums + "]" ) ;console . log ( "------------------------------