[leetcode]26. Remove Duplicates from Sorted Array
2016-12-08 17:56
381 查看
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 =
Your function should return length =
It doesn't matter what you leave beyond the new length.
java代码
public class Solution {
public int removeDuplicates(int[] A) {
if(A.length<=1)
{
return A.length;
}
int n=0,m=1;
while(m<A.length)
{
if(A
==A[m])
{
m++;
}
else
{
A[++n]=A[m++];
}
}
return n+1;
}
}
go代码
func removeDuplicates(nums []int) int {
var length int = len(nums)
if length < 1{
return length
}
var n int = 0
var m int = 1
for m < length{
if nums[m] == nums
{
m++
}else{
nums[n+1] = nums[m]
n++
m++
}
}
return n+1
}
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
1and
2respectively.
It doesn't matter what you leave beyond the new length.
java代码
public class Solution {
public int removeDuplicates(int[] A) {
if(A.length<=1)
{
return A.length;
}
int n=0,m=1;
while(m<A.length)
{
if(A
==A[m])
{
m++;
}
else
{
A[++n]=A[m++];
}
}
return n+1;
}
}
go代码
func removeDuplicates(nums []int) int {
var length int = len(nums)
if length < 1{
return length
}
var n int = 0
var m int = 1
for m < length{
if nums[m] == nums
{
m++
}else{
nums[n+1] = nums[m]
n++
m++
}
}
return n+1
}
相关文章推荐
- LeetCode 26. Remove Duplicates from Sorted Array(删除重复)
- Leetcode26-Remove Duplicates from Sorted Array
- leetcode-26-Remove Duplicates from Sorted Array 顺序表
- Leetcode 26. Remove Duplicates from Sorted Array (Easy) (java)
- 【LeetCode】【Array】【26】Remove Duplicates from Sorted Array
- LeetCode 26. Remove Duplicates from Sorted Array
- [LeetCode] 26. Remove Duplicates from Sorted Array
- LeetCode 26 Remove Duplicates from Sorted Array
- leetCode26:Remove Duplicates from Sorted Array
- leetcode26:Remove Duplicates from Sorted Array
- LeetCode(26)题解:Remove Duplicates from Sorted Array
- LeetCode 26_Remove Duplicates from Sorted Array
- Leetcode||26.Remove Duplicates from Sorted Array
- leetcode题解-26 Remove Duplicates from Sorted Array
- [leetcode:python]26.Remove Duplicates from Sorted Array
- 【LeetCode】26 Remove Duplicates from Sorted Array
- LeetCode 26 Remove Duplicates from Sorted Array(从已排序数组中移除重复元素)
- leetcode 26. Remove Duplicates from Sorted Array
- leetcode - 26 Remove Duplicates from Sorted Array
- leetcode 26. Remove Duplicates from Sorted Array