- 浏览: 174474 次
- 性别:
- 来自: 济南
文章分类
最新评论
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 229Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 231You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 347Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 342Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 460Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 524Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 434Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 623Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 434The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 392Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 522Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 541Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 378All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 868Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 884Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 559Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 606Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 771Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 741You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 635For a undirected graph with tre ...
相关推荐
LeetCode Remove Duplicates from Sorted Array解决方案
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 ...
Remove Duplicates from Sorted Array II Longest Consecutive Sequence Two Sum 3Sum 3Sum Closest 4Sum Remove Element Move Zeroes Next Permutation Permutation Sequence Valid Sudoku Trapping Rain Water ...
Leetcode经典01背包 algo 1. 数据结构与算法 数组,链表,(串和序列) 堆,栈,队列 树,图 排序,搜索 贪心,回溯,动态规划 堆:一种完全二叉树,任意节点...II | | | 75 Sort Colors 计数排序 | | | 88 Merge So
第一章 顺序表1.1 删除排序数组中的重复数字 Remove Duplicates from Sorted Array给定一个排序数组,在原数组中删除重复出现的
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 ( "------------------------------
Remove Duplicates from Sorted Array iii. Plus One iv. Pascal's Triangle v. Merge Sorted Array vi. Sum vii. Find Minimum in Rotated Sorted Array viii. Largest Rectangle in Histogram ix. Maximal ...
Array Easy #27 Remove Element Easy #35 Search Insert Position Easy #38 Count and Say Easy #53 Maximum Subarray Easy #66 Plus One Easy #70 Climbing Stairs Easy #83 Remove Duplicates from Sorted L
* 《算法珠玑》数组_题1 * Remove Duplicates from Sorted Array * such as: * input: A = [1,2,2] * output: A = [1.2] * tips: * 1. no extra space * 2. return new length
Duplicates from Sorted Array 删除排序数组中的重复项 32 Longest Valid Parentheses 最长有效括号 33 Search in Rotated Sorted Array 搜索旋转排序数组 34 Find First and Last Position of Element in Sorted ...
leetcode 代码Remove Duplicates from Sorted Array
from Sorted Array Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do ...
lru缓存leetcode leetcode 1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating ...Sorted ...Sorted ...Remove Duplicates from Sorted Array 27. Remove Element 28. Implement strStr() 3
Sorted Array Easy 26-2 Remove Duplicates from Unsorted Array Easy 27 Remove Element Easy 剑指Offer 考点 题目 解法 难度 字符串 替换空格 易 字符串 字符串旋转 易 数组 二维数组中的查找 难 数组 旋转数组的...
26.Remove Duplicates from Sorted Array 53.Maximum Subarray 70.Climbing Stairs 121.Best Time to Buy and Sell Stock 122.Best Time to Buy and Sell Stock II 123.Best Time to Buy and Sell Stock III 141....
II - Input array is sorted (E) 653. Two Sum IV - Input is a BST (E) -> 2 26. Remove Duplicates from Sorted Array (E) 27. Remove Element (E) 31. Next Permutation (M) * -> index 주의, 부등호 하나 틀림 ...
Duplicates from Sorted Array 066 Plus One String 043 Multiply Strings 066 Add Binary Linked-list 002 Add Two Numbers Stack 020 Valid Parenthesis Hash Table 001 TwoSum Reference 完整的学习流程 How to ...
java lru leetcode :ice_cream: LeetCode Kindem 的个人 LeetCode ...Duplicates from Sorted Array 48 Rotate Image 53 Maximum Subarray 55 Jump Game 56 Merge Intervals 64 Minimum Path Sum 73