LeetCode 26 Remove Duplicates from Sorted Array
2016-10-26 17:35
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 = [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.
Subscribe to see which companies asked this question
代码解法:
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.
Subscribe to see which companies asked this question
代码解法:
public class solution { public int removeDuplicates(int[] nums) { if(nums.length == 1 && nums.length == 0) return 0; int index = 0; for(int i = 0 , j = 0; i < nums.length; i++) { if(i != nums.length -1) { if(nums[i] != nums[i+1]) nums[j++] = nums[i]; else index++; } else nums[j] = nums[i]; } return nums.length - index; } }
相关文章推荐
- leetCode 26.Remove Duplicates from Sorted Array(删除数组重复点) 解题思路和方法
- 【C++】【LeetCode】26. Remove Duplicates from Sorted Array
- [array] leetCode-26. Remove Duplicates from Sorted Array - Easy
- leetcode-26. Remove Duplicates from Sorted Array
- [LeetCode]26.Remove Duplicates from Sorted Array
- LeetCode-26-Remove Duplicates from Sorted Array(C语言实现)
- 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
- 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-java-26. Remove Duplicates from Sorted Array
- LeetCode 26. Remove Duplicates from Sorted Array && 80. Remove Duplicates from Sorted Array II
- leetcode 26 Remove Duplicates from Sorted Array
- leetcode-26 Remove Duplicates from Sorted Array
- leetcode 26. Remove Duplicates from Sorted Array