Remove Duplicates 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 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.
题目解读:
给定一个有序数组,删除数组中重复的元素,使数组中的每个元素只出现一次,返回新数组的长度。不允许申请额外数组空间,必须在常量个内存空间中完成。例如给定一个数组nums=[1,1,2]。函数的返回结果为length=2,并且剩余数组中的前两个元素必须是1和2。剩余数组的长度大小无所谓。
解析:
由于数组是有序的,所以重复的数组元素是紧挨着的两个或多个。则在算法中需要使用当前元素与下一元素进行比较,如果下一元素与当前元素相同,则删除下一元素,以此类推。如下图所示:
Java代码:
public class Solution { public int removeDuplicates(int[] nums) { int k=0; for (int i=1; i<nums.length; i++) { if(nums[i-1] == nums[i]) { k++; continue; } else { nums[i-k] = nums[i]; } } return nums.length-k; } }
程序性能:
相关推荐
LeetCode Remove Duplicates from Sorted Array解决方案
26.Remove_Duplicates_from_Sorted_Array删除有序数组中的重复项【LeetCode单题讲解系列
leetcode从重复数组中删除重复项 给定一个已排序的数组nums,就地删除重复项,以使每个元素仅出现一次并返回新的长度。 不要为另一个数组分配额外的空间,必须通过使用O(1)额外的内存就地修改输入数组来做到这...
remove-duplicates-from-sorted-array 移除元素 remove-element 搜索插入位置 search-insert-position 最大子序和 maximum-subarray 加一 plus-one 合并两个有序数组 merge-sorted-array 杨辉三角 pascals-triangle ...
颜色分类leetcode My Leetcode Problems Solutions Using javascript(ES6) 1 Two Sum 两数之和 5 Longest Palindromic Substring 最长回文子串 7 Reverse Integer 整数反转 9 Palindrome Number 回文数 11 Container...
leetcode-goMy solution to LeetCode problems using GolangProblems 题库Array 数组NoTitle题名DifficultyStatus11Container With Most Water盛最多水的容器MediumSolved26Remove Duplicates from Sorted Array删除...
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....
83.删除排序链表中的重复元素 (Remove Duplicates from Sorted List) 88.合并两个有序数组 (Merge Sorted Array) 100.相同的树 (Same Tree) 104.二叉树的最大深度 (Maximum Depth of Binary Tree) 118.杨辉三角 ...
com.leetcode.array Search a 2D Matrix Spiral Matrix com.leetcode.list Linked List Cycle Linked List Cycle II Remove Duplicates from Sorted List com.leetcode.string Single Number com.leetcode.tree ...
lru缓存leetcode leetcode 1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 5. Longest Palindromic Substring 7. Reverse Integer 9. ...
O(m+n) time, O(m+n) sapce.*0026 Remove Duplicates from Sorted Array使用双指针,一个快指针,一个慢指针。开始时,两个指针都指向首元素。当两指针元素值相同时,快指针+1;当两指针元素不同时,慢
leetcode 答案LeetCode LeetCode in Swift 这个Repo 用来存下我在LeetCode 解题的原始档,包含解题中遇到的错误,也包含解出AC 的答案, 以下的清单将连结到我的Github Pages 中,皆有题目中文翻译与解题的过程。...
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
leetcode 浇花力扣解决方案 简单的 #0001 - Two Sum #0007 - Reverse Integer #0009 - Palindrome Number #0035 - Search Insert Position #0058 - Length of Last Word #0066 - Plus One #0083 - Remove Duplicates...
leetcode 1004 leetcode E:简单,M:中等,H:困难 数组和字符串 217. Contains Duplicate (E) 48. Rotate Image (M) -> 2 73. Set Matrix Zeroes (M) 1. Two Sum (E) 167. Two Sum II - Input array is sorted (E)...
leetcode写题闪退 #*的多少代表此题的有意思程度 有几题第一次写的时候思绪比较混乱: *****Regular Expression Matching 2014.10.29 对于Find Minimum in Rotated Sorted Array II 和 Find Minimum in Rotated ...
leetcode 苹果 OnlineJudge Code for Online Judge. LeetCode # Title Solution Difficulty 1 Two Sum Easy 26-1 Remove Duplicates from Sorted Array Easy 26-2 Remove Duplicates from Unsorted Array Easy 27 ...
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...
leetcode 2 LeetCode-练习 我的 Leetcode“解决方案”(在解决方案/文件夹中)来解决 leetcode 问题。 它用于练习和跟踪进度不是 100% 优化的。 我的账户链接 问题名称 运行时和内存使用 1. Two Sum 运行时间:4412 ...
leetcode LeetCode 这个库用于总结leetcode中遇到的习题 常用数据结构习题总结 1.线性表 解决进度 No. Describition mark 1 Remove Duplicates from Sorted Array 2 Remove Duplicates from Sorted Array II 3 ...