- 浏览: 184787 次
- 性别:
- 来自: 济南
文章分类
最新评论
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 this in place with constant memory.
For example,
Given input array nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.
解决这道题我们用两个指针,一个指针用于遍历数组,另外一个用于指向不重复的元素。实现代码如下:
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.
解决这道题我们用两个指针,一个指针用于遍历数组,另外一个用于指向不重复的元素。实现代码如下:
public class Solution { public int removeDuplicates(int[] nums) { if(nums == null || nums.length == 0) return 0; int index = 1; for(int i = 1; i < nums.length; i++) { if(nums[i] != nums[i - 1]) nums[index ++] = nums[i]; } return index; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 269Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 271You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 388Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 378Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 503Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 568Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 482Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 666Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 473The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 433Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 582Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 590Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 429All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 905Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 933Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 606Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 691Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 857Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 789You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 723For 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 ...
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
python python_leetcode题解之080_Remove_Duplicates_from_Sorted_Array_II
c语言入门 C语言_leetcode题解之26-remove-duplicates-from-sorted-array.c
javascript js_leetcode题解之80-remove-duplicates-from-sorted-array-ii.js
c c语言_leetcode题解之0080_remove_duplicates_from_sorted_array_ii.zip
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
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 ...
- Remove Duplicates from Sorted Array II(删除排序数组中的重复项II) - Merge Sorted Array(合并两个有序数组) - Merge Sorted Array II(合并两个有序数组II) - Median(中位数) - Partition Array by...
* 《算法珠玑》数组_题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
Leetcode经典01背包 algo 1. 数据结构与算法 数组,链表,(串和序列) ...Duplicates from Sorted Array | | | 80 Remove Duplicates from Sorted Array II | | | 75 Sort Colors 计数排序 | | | 88 Merge So
4. 数组操作相关问题:如Remove Duplicates from Sorted Array(删除排序数组中的重复项),Move Zeroes(移动零),Container With Most Water(最多水的容器)等,考察对数组操作的掌握。 5. 字符串处理问题:...
- **2.1.1 Remove Duplicates from Sorted Array** - 题目要求在排序后的数组中移除重复元素,同时保持剩余元素的顺序不变。 - 实现思路:使用双指针法,一个指针遍历数组,另一个指针记录不重复元素的位置。 - ...
Duplicates from Sorted Array 删除排序数组中的重复项 32 Longest Valid Parentheses 最长有效括号 33 Search in Rotated Sorted Array 搜索旋转排序数组 34 Find First and Last Position of Element in Sorted ...