Skip to content

Commit 4d76c43

Browse files
committed
in-place remove duplicates
1 parent e3db24e commit 4d76c43

File tree

3 files changed

+164
-0
lines changed

3 files changed

+164
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package RemoveDuplicatesfromSortedArray;
2+
3+
/**
4+
* User: Danyang
5+
* Date: 1/26/2015
6+
* Time: 11:55
7+
*
8+
* Given a sorted array, remove the duplicates in place such that each element appear only once and return the new
9+
* length.
10+
11+
Do not allocate extra space for another array, you must do this in place with constant memory.
12+
13+
For example,
14+
Given input array A = [1,1,2],
15+
16+
Your function should return length = 2, and A is now [1,2].
17+
*/
18+
public class Solution {
19+
/**
20+
* Two Pointers
21+
* @param A
22+
* @return
23+
*/
24+
public int removeDuplicates(int[] A) {
25+
if(A.length==0)
26+
return 0;
27+
int i = 0;
28+
int j = 1;
29+
for(; i<A.length; i++) {
30+
while(j<A.length && A[j]==A[i]) j++;
31+
if(j==A.length) break;
32+
A[i+1] = A[j];
33+
}
34+
return i+1;
35+
}
36+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package RemoveDuplicatesfromSortedArrayII;
2+
3+
/**
4+
* User: Danyang
5+
* Date: 1/26/2015
6+
* Time: 12:18
7+
*
8+
* Follow up for "Remove Duplicates":
9+
What if duplicates are allowed at most twice?
10+
11+
For example,
12+
Given sorted array A = [1,1,1,2,2,3],
13+
14+
Your function should return length = 5, and A is now [1,1,2,2,3].
15+
*/
16+
public class Solution {
17+
public int removeDuplicates_error(int[] A) {
18+
if(A.length==0)
19+
return 0;
20+
int i = 0;
21+
int j = 1;
22+
int allowed = 1;
23+
for(; i<A.length; i++, j++) {
24+
while(j<A.length && A[j]==A[i]) {
25+
allowed--;
26+
j++;
27+
}
28+
if(allowed==0)
29+
A[i+1] = A[j-1];
30+
31+
if(allowed<1)
32+
i++;
33+
34+
if(j==A.length)
35+
break;
36+
A[i+1] = A[j];
37+
allowed = 1;
38+
}
39+
return i+1;
40+
}
41+
42+
public int removeDuplicates(int [] A) {
43+
if(A.length==0)
44+
return 0;
45+
int i = 0;
46+
int j = 1;
47+
boolean repeated = false;
48+
while(j<A.length) { // one loop would be clear
49+
if(A[i]==A[j] &&!repeated) { // duplicated but not repeated
50+
repeated = true;
51+
A[i+1] = A[j];
52+
j++;
53+
i++;
54+
}
55+
else if(A[i]==A[j] && repeated) { // duplicated and repeated
56+
j++;
57+
}
58+
else { // not duplicated
59+
repeated = false;
60+
A[i+1] = A[j];
61+
i++;
62+
j++;
63+
}
64+
}
65+
return i+1;
66+
}
67+
}

src/RotateList/Solution.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package RotateList;
2+
3+
import commons.datastructures.ListNode;
4+
5+
/**
6+
* User: Danyang
7+
* Date: 1/26/2015
8+
* Time: 11:40
9+
*
10+
* Given a list, rotate the list to the right by k places, where k is non-negative.
11+
12+
For example:
13+
Given 1->2->3->4->5->NULL and k = 2,
14+
return 4->5->1->2->3->NULL.
15+
*/
16+
public class Solution {
17+
/**
18+
* LinkedList, diagram
19+
* Notice:
20+
* 1. n = n%l;
21+
* @param head
22+
* @param n
23+
* @return
24+
*/
25+
public ListNode rotateRight(ListNode head, int n) {
26+
ListNode dummy = new ListNode(0);
27+
dummy.next = head;
28+
ListNode pre = dummy;
29+
30+
int cnt = 0;
31+
int l = getLength(head);
32+
if(l==0)
33+
return head;
34+
n = n%l;
35+
ListNode pre_br = new ListNode(0);
36+
ListNode last = new ListNode(0);
37+
while(pre.next!=null) {
38+
cnt++;
39+
if(cnt==l-n)
40+
pre_br = pre.next;
41+
pre = pre.next;
42+
if(pre.next==null)
43+
last = pre;
44+
}
45+
46+
last.next = dummy.next;
47+
dummy.next = pre_br.next;
48+
pre_br.next = null;
49+
return dummy.next;
50+
}
51+
52+
int getLength(ListNode head) {
53+
ListNode cur = head;
54+
int l = 0;
55+
while(cur!=null) {
56+
l++;
57+
cur = cur.next;
58+
}
59+
return l;
60+
}
61+
}

0 commit comments

Comments
 (0)