- 浏览: 19315 次
- 性别:
- 来自: 北京
-
文章分类
最新评论
题目描述
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 A = [1,1,2],
Your function should return length = 2, and A is now [1,2].
解题思路
将数组中多余的元素删去,难点在于不使用额外的空间,这时就需要用到重复元素的数目这个变量。
自己的代码
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 A = [1,1,2],
Your function should return length = 2, and A is now [1,2].
解题思路
将数组中多余的元素删去,难点在于不使用额外的空间,这时就需要用到重复元素的数目这个变量。
自己的代码
package leetcode; import java.util.Arrays; public class RemoveDuplicatesFromSortedArray { public int removeDuplicates(int[] A) { int sum = 0; if(A.length > 1){ for(int i = 1; i < A.length; i++){ if(A[i] == A[i-1]) sum++; else A[i-sum] = A[i]; } } return A.length - sum; } public static void main(String[] args) { int[] A = {1, 2, 2, 2, 3, 3, 4}; RemoveDuplicatesFromSortedArray rdfsa = new RemoveDuplicatesFromSortedArray(); System.out.println(rdfsa.removeDuplicates(A)); System.out.println(Arrays.toString(A)); } }
发表评论
-
Java中String与StringBuffer的区别
2014-10-29 21:07 302String和StringBuffer的区别,网上资料可以说 ... -
String to Integer (atoi)
2014-10-29 17:13 401题目描述 Implement atoi to convert ... -
Implement strStr()
2014-10-28 15:17 289题目描述 Implement strStr(). Retu ... -
Valid Palindrome
2014-10-23 10:32 425题目描述 Given a string, determine ... -
ZigZag Conversion
2014-10-22 19:51 346题目描述 The string "PAYPALIS ... -
Add Binary
2014-10-22 19:43 305题目描述 Given two binary strings, ... -
Longest Common Prefix
2014-10-22 19:44 333题目描述 Write a function to find t ... -
Count and Say
2014-10-22 19:44 348题目描述 The count-and-say sequence ... -
Valid Sudoku
2014-10-21 10:22 358题目描述 Determine if a Sudoku is v ... -
Valid Parentheses
2014-10-21 09:41 327题目描述 Given a string containing ... -
Palindrome Number
2014-10-21 09:41 349题目描述 Determine whether an integ ... -
Length of Last Word
2014-10-21 09:41 359题目描述 Given a string s consists ... -
Minimum Depth of Binary Tree
2014-10-21 09:41 310题目描述 Given a binary tree, find ... -
Remove Nth Node From End of List
2014-10-20 16:36 259题目描述 Given a linked list, remov ... -
Path Sum
2014-10-20 15:37 299题目描述 Given a binary tree and a ... -
Binary Tree Level Order Traversal II
2014-10-20 11:17 236题目描述 Given a binary tree, retur ... -
Binary Tree Level Order Traversal
2014-10-20 11:03 293题目描述 Given a binary tree, retur ... -
Pascal's Triangle II
2014-10-20 10:07 262题目描述 Given an index k, return t ... -
Pascal's Triangle
2014-10-19 12:24 322题目描述 Given numRows, generate th ... -
Plus One
2014-10-19 11:51 340题目描述 Given a non-negative numbe ...
相关推荐
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 ...
第一章 顺序表1.1 删除排序数组中的重复数字 Remove Duplicates from Sorted Array给定一个排序数组,在原数组中删除重复出现的
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 ...
* 《算法珠玑》数组_题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
Duplicates from Sorted Array 删除排序数组中的重复项 32 Longest Valid Parentheses 最长有效括号 33 Search in Rotated Sorted Array 搜索旋转排序数组 34 Find First and Last Position of Element in Sorted ...
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 考点 题目 解法 难度 字符串 替换空格 易 字符串 字符串旋转 易 数组 二维数组中的查找 难 数组 旋转数组的...
Duplicates from Sorted Array (E) 27. Remove Element (E) 31. Next Permutation (M) * -> index 주의, 부등호 하나 틀림 33. Search in Rotated Sorted Array (M) * -> 부등호 주의, 부등호 하나 틀림 34. Find ...
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....
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
O(m+n) time, O(m+n) sapce.*0026 Remove Duplicates from Sorted Array使用双指针,一个快指针,一个慢指针。开始时,两个指针都指向首元素。当两指针元素值相同时,快指针+1;当两指针元素不同时,慢
String/26_remove_duplicates_from_sorted_array.md) [(雅虎)139。 Word Break](Leetcode 问题/数组和字符串/139.word_break.md) [140. Word Break ii](Leetcode 问题/数组和字符串/140.word_break_ii.md) [151. ...
leetcode 代码Remove Duplicates from Sorted Array