Skip to content

Commit 092560c

Browse files
committed
Signed-off-by: yuduozhou <[email protected]>
1 parent 22e34d7 commit 092560c

7 files changed

+223
-0
lines changed

RecoverBinaryTree.java

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Definition for binary tree
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
public class Solution {
11+
public void recoverTree(TreeNode root) {
12+
// Start typing your Java solution below
13+
// DO NOT write main() function
14+
ArrayList<TreeNode> inOrder = new ArrayList<TreeNode>();
15+
int error1 = 0;
16+
int error2 = 0;
17+
inOrderTrav(root, inOrder);
18+
int prev = 0;
19+
for (int i = 1; i < inOrder.size(); i++){
20+
// Value is in ascening order.
21+
if (inOrder.get(i).val > inOrder.get(prev).val){
22+
prev = i;
23+
}
24+
// Previous node is bigger.
25+
else {
26+
error1 = prev;
27+
break;
28+
}
29+
}
30+
// Find the smallest node afterwards.
31+
error2 = prev + 1;
32+
for (int i = error2 + 1; i < inOrder.size(); i++){
33+
if (inOrder.get(i).val < inOrder.get(error2).val){
34+
error2 = i;
35+
}
36+
}
37+
int temp = inOrder.get(error1).val;
38+
inOrder.get(error1).val = inOrder.get(error2).val;
39+
inOrder.get(error2).val = temp;
40+
}
41+
public void inOrderTrav(TreeNode root, ArrayList<TreeNode> inOrder){
42+
if(root == null) return;
43+
inOrderTrav(root.left, inOrder);
44+
inOrder.add(root);
45+
inOrderTrav(root.right, inOrder);
46+
}
47+
}

RegularExpressionMatching.java

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Thanks to heartfire.cc
2+
3+
public class Solution {
4+
public boolean isMatch(String s, String p) {
5+
//Java note: s.substring(n) will be "" if n == s.length(), but if n > s.length(), index oob error
6+
// Start typing your Java solution below
7+
// DO NOT write main() function
8+
9+
int i = 0, j = 0;
10+
//you don't have to construct a state machine for this problem
11+
12+
if (s.length() == 0) {
13+
return checkEmpty(p);
14+
}
15+
16+
if (p.length() == 0) {
17+
return false;
18+
}
19+
20+
char c1 = s.charAt(0);
21+
char d1 = p.charAt(0), d2 = '0'; //any init value except '*'for d2 will do
22+
23+
if (p.length()>1){
24+
d2 = p.charAt(1);
25+
}
26+
27+
if (d2 == '*') {
28+
if (compare(c1, d1)) {
29+
//fork here: 1. consume the character, and use the same pattern again.
30+
//2. keep the character, and skip 'd1*' pattern
31+
32+
//Here is also an opportunity to use DP, but the idea is the same
33+
return isMatch(s.substring(1), p) || isMatch(s, p.substring(2));
34+
}
35+
else {
36+
return isMatch(s, p.substring(2));
37+
}
38+
}
39+
else {
40+
if (compare(c1, d1)) {
41+
return isMatch(s.substring(1), p.substring(1));
42+
}
43+
else {
44+
return false;
45+
}
46+
}
47+
}
48+
49+
public boolean compare(char c1, char d1){
50+
return d1 == '.' || c1 == d1;
51+
}
52+
53+
public boolean checkEmpty(String p) {
54+
if (p.length()%2 != 0) {
55+
return false;
56+
}
57+
58+
for (int i = 1; i < p.length(); i+=2) {
59+
if (p.charAt(i) != '*') {
60+
return false;
61+
}
62+
}
63+
return true;
64+
}
65+
}

RemoveDuplicateFromSortedArray.java

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class Solution {
2+
public int removeDuplicates(int[] A) {
3+
if (A.length == 0) return 0;
4+
int i = 0, j = 1;
5+
for(; j < A.length; j++){
6+
if (A[j] != A[j - 1]) {
7+
A[++i] = A[j];
8+
}
9+
}
10+
return ++i;
11+
}
12+
}

RemoveDuplicateFromSortedArrayII.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class Solution {
2+
public int removeDuplicates(int[] A) {
3+
if (A.length == 0) return 0;
4+
int i = 0, int j = 1;
5+
boolean seen
6+
for(; j < A.length; j ++){
7+
if (A[j] != A[j - 1]) {
8+
A[++i] = A[j];
9+
seen = false;
10+
}
11+
else if(!seen){
12+
A[++i] = A[j];
13+
seen = true;
14+
}
15+
}
16+
return ++i;
17+
}
18+
}

RemoveDuplicateFromSortedList.java

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) {
7+
* val = x;
8+
* next = null;
9+
* }
10+
* }
11+
*/
12+
public class Solution {
13+
public ListNode deleteDuplicates(ListNode head) {
14+
// Start typing your Java solution below
15+
// DO NOT write main() function
16+
if (head == null) return null;
17+
ListNode p1 = head, p2 = p1.next;
18+
while(p2 != null){
19+
if(p2.val != p1.val){
20+
p1 = p2;
21+
p2 = p2.next;
22+
}
23+
else{
24+
p2 = p2.next;
25+
p1.next = p2;
26+
}
27+
}
28+
return head;
29+
}
30+
}

RemoveDuplicateFromSortedListII.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) {
7+
* val = x;
8+
* next = null;
9+
* }
10+
* }
11+
*/
12+
public class Solution {
13+
public ListNode deleteDuplicates(ListNode head) {
14+
// Start typing your Java solution below
15+
// DO NOT write main() function
16+
ListNode sent = new ListNode(0); //Sentinel to guard against the changed head node
17+
sent.next = head;
18+
ListNode cur = head, prev = sent;
19+
while (cur != null) {
20+
boolean dup = false;
21+
while (cur.next != null && cur.next.val == cur.val) {
22+
dup = true;
23+
cur = cur.next;
24+
}
25+
if (dup) {
26+
prev.next = cur.next;
27+
}
28+
else {
29+
prev = cur;
30+
}
31+
cur = cur.next;
32+
}
33+
return sent.next;
34+
}
35+
}

RemoveElement.java

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class Solution {
2+
public int removeElement(int[] A, int elem) {
3+
// Start typing your Java solution below
4+
// DO NOT write main() function
5+
if(A.length == 0) return 0;
6+
int slow = 0, fast = 0;
7+
while(fast < A.length){
8+
if(A[fast] != elem){
9+
A[slow] = A[fast];
10+
slow ++;
11+
}
12+
fast ++;
13+
}
14+
return slow ++;
15+
}
16+
}

0 commit comments

Comments
 (0)