leetcode 26. Remove Duplicates from Sorted Array
来源:互联网 发布:电脑管家硬件数据 编辑:程序博客网 时间:2024/06/06 20:09
题目
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.
解1(超时,这么笨的办法,我还想了那么久,(⊙﹏⊙))
public int removeDuplicates(int[] nums) { if(nums.length==0||nums.length==1) return nums.length; int i=0; int j=1; int k=nums.length-1; while(i<nums.length && j<nums.length && j<k){ if(nums[i]==nums[j]) { do{ moveBack(nums,j,k); k--; }while((nums[i]==nums[j])&& j<k); moveBack(nums,i,k); k--; } else{ i++; j=i+1; } } return k+1; } public void moveBack(int[] nums,int j,int k){ int temp=nums[j]; nums[j]=nums[k]; nums[k]=temp; String b=Arrays.toString(nums); System.out.println("Before:"+b); if(k-j>1) Arrays.sort(nums, j, k); b=Arrays.toString(nums); System.out.println("After:"+b); }
解2(让元素后面的元素与前面的元素相比)
public class Solution { public int removeDuplicates(int[] nums) { if(nums.length ==0){ return 0; } int count=1; for(int i=1; i<nums.length; ++i){ if(nums[i] != nums[i-1]){ //注意这行代码 nums[count]=nums[i]; count++; } } return count; }}
解3( 让前面的元素与后面的元素相比)
public int removeDuplicates(int[] nums) { if(nums.length ==0){ return 0; } int count=1; for(int i=1; i<nums.length; ++i){ if(nums[count-1] != nums[i]){ //注意这行代码 nums[count]=nums[i]; count++; } } return count; }
参考链接
1:http://www.tuicool.com/articles/ayeuYf
0 0
- [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
- 26. Remove Duplicates from Sorted Array LeetCode
- 【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
- 26.[Leetcode]Remove Duplicates from Sorted Array
- LOG4NET用法(个人比较喜欢的用法)
- 对象流笔记整理
- 面向对象编程特点
- Linearlayout 测量过程源码解析(二)
- 查看文件目录清单
- leetcode 26. Remove Duplicates from Sorted Array
- H264视频压缩编码标准(一)
- Codeforces Round #304 (Div. 2)-D. Soldier and Number Game,素因子打表,超时哭晕~~
- cx_Oracle如何获取存储过程的返回值
- 跨域解决方案 ajax代理
- MySQL 导入的问题 Lost connection to MySQL server during query
- String类(1)
- 使用对象流读写对象代码笔记(ObjectInputStream ,ObjectOutputStream)
- javascript取整 取余等操作