问题描述
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.
Java
package array; import java.util.Arrays; public class RemoveDuplicateDemo { public static void main(String[] args) { int[] nums1 = {1, 1, 2, 2, 3}; int length = removeDuplicate(nums1); print(length); int[] nums2 = new int[length]; System.arraycopy(nums1, 0, nums2, 0, length); print(Arrays.toString(nums2)); } public static int removeDuplicate(int[] nums) { if(nums == null || nums.length == 0) { return 0; } int idx = 1; int sentinel = nums[0]; for(int num: nums) { if(num != sentinel) { nums[idx++] = num; sentinel = num; } } return idx; } public static void print(Object obj) { System.out.println(obj); } }
Python
# -*- coding: utf-8 -*- def remove_duplicate(nums): if not nums: return 0 sentinel = nums[0] idx = 1 for num in nums: if num != sentinel: sentinel = num nums[idx] = num idx += 1 return idx nums = [1, 1, 2, 2, 3, 4, 5, 5] l = remove_duplicate(nums) print l, nums[:l]
相关推荐
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 ...