-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution26.java
More file actions
52 lines (50 loc) · 891 Bytes
/
Copy pathSolution26.java
File metadata and controls
52 lines (50 loc) · 891 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* @Title: Solution26.java——
* @Package EasyCode
* @Description: TODO
* @author msdumin@gmail.com
* @date 2019年3月14日 下午5:10:55
* @version V1.0
*/
package EasyCode;
/**
* @ClassName: Solution26——Remove Duplicates from Sorted Array
* @Description: TODO
* @author msdumin@gmail.com
* @date 2019年3月14日 下午5:10:55
*/
//要求原地算法
public class Solution26 {
public int removeDuplicates(int[] nums){
/* if(nums.length == 0)
return 0;
if(nums.length == 1)
return 1;
int tmp = nums[0];
int i = 1;
int count = 0;
while(i < nums.length){
if(nums[i] == tmp)
count ++;
else
{
tmp = nums[i];
nums[i - count] = tmp;
}
i++;
}
return nums.length - count;*/
//解法2
int i = 0,j = 1;
while(j < nums.length){
if(nums[i] == nums[j])
j++;
else{
nums[i + 1] = nums[j];
i++;
j++;
}
}
return i + 1;
}
}