Skip to content

Commit 12e004c

Browse files
author
applewjg
committed
add
Change-Id: Ib581f574559afe39c0f5e22fb79ffec45a3f56bf
1 parent 950f25f commit 12e004c

File tree

4 files changed

+309
-0
lines changed

4 files changed

+309
-0
lines changed

BinaryTreePreorderTraversal.java

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
Author: King, [email protected]
3+
Date: Dec 25, 2014
4+
Problem: Binary Tree Preorder Traversal
5+
Difficulty: Easy
6+
Source: http://oj.leetcode.com/problems/binary-tree-preorder-traversal/
7+
Notes:
8+
Given a binary tree, return the preorder traversal of its nodes' values.
9+
For example:
10+
Given binary tree {1,#,2,3},
11+
1
12+
\
13+
2
14+
/
15+
3
16+
return [1,2,3].
17+
Note: Recursive solution is trivial, could you do it iteratively?
18+
19+
Solution: 1. Iterative way (stack). Time: O(n), Space: O(n).
20+
2. Recursive solution. Time: O(n), Space: O(n).
21+
3. Threaded tree (Morris). Time: O(n), Space: O(1).
22+
*/
23+
24+
/**
25+
* Definition for binary tree
26+
* public class TreeNode {
27+
* int val;
28+
* TreeNode left;
29+
* TreeNode right;
30+
* TreeNode(int x) { val = x; }
31+
* }
32+
*/
33+
public class Solution {
34+
public List<Integer> preorderTraversal_1(TreeNode root) {
35+
List<Integer> res = new ArrayList<Integer>();
36+
if(root == null) return res;
37+
res.add(root.val);
38+
List<Integer> left = preorderTraversal(root.left);
39+
List<Integer> right = preorderTraversal(root.right);
40+
res.addAll(left);
41+
res.addAll(right);
42+
return res;
43+
}
44+
public void preorderTraversalRe(TreeNode root, List<Integer> res) {
45+
if (root == null) return;
46+
res.add(root.val);
47+
preorderTraversalRe(root.left, res);
48+
preorderTraversalRe(root.right, res);
49+
}
50+
public List<Integer> preorderTraversal_2(TreeNode root) {
51+
List<Integer> res = new ArrayList<Integer>();
52+
if(root == null) return res;
53+
preorderTraversalRe(root, res);
54+
return res;
55+
}
56+
public List<Integer> preorderTraversal_3(TreeNode root) {
57+
List<Integer> res = new ArrayList<Integer>();
58+
if(root == null) return res;
59+
Stack<TreeNode> stk = new Stack<TreeNode>();
60+
stk.push(root);
61+
while (stk.isEmpty() == false) {
62+
TreeNode cur = stk.pop();
63+
res.add(cur.val);
64+
if (cur.right != null) stk.push(cur.right);
65+
if (cur.left != null) stk.push(cur.left);
66+
}
67+
return res;
68+
}
69+
public List<Integer> preorderTraversal_4(TreeNode root) {
70+
List<Integer> res = new ArrayList<Integer>();
71+
if(root == null) return res;
72+
Stack<TreeNode> stk = new Stack<TreeNode>();
73+
TreeNode cur = root;
74+
while (stk.isEmpty() == false || cur != null) {
75+
if (cur != null) {
76+
stk.push(cur);
77+
res.add(cur.val);
78+
cur = cur.left;
79+
} else {
80+
cur = stk.pop();
81+
cur = cur.right;
82+
}
83+
}
84+
return res;
85+
}
86+
public List<Integer> preorderTraversal_5(TreeNode root) {
87+
List<Integer> res = new ArrayList<Integer>();
88+
if(root == null) return res;
89+
TreeNode cur = root;
90+
while (cur) {
91+
if (cur.left == null) {
92+
res.add(cur.val);
93+
cur = cur.right;
94+
} else {
95+
TreeNode node = cur.left;
96+
while (node.right != null && node.right != cur)
97+
node = node.right;
98+
if (node == null) {
99+
node.right = cur;
100+
res.add(cur.val);
101+
cur = cur.left;
102+
} else {
103+
node.right = null;
104+
cur = cur.right;
105+
}
106+
}
107+
}
108+
return res;
109+
}
110+
}

RotateImage.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Author: King, [email protected]
3+
Date: Dec 25, 2014
4+
Problem: Rotate Image
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/rotate-image/
7+
Notes:
8+
You are given an n x n 2D matrix representing an image.
9+
Rotate the image by 90 degrees (clockwise).
10+
Follow up:
11+
Could you do this in-place?
12+
13+
Solution: 1. 123 -> 147 -> 741 (preferable)
14+
456 258 852
15+
789 369 963
16+
2. Rotate one-fourth of the image clockwise.
17+
*/
18+
public class Solution {
19+
public void rotate_1(int[][] matrix) {
20+
int n = matrix.length;
21+
if (n <= 1) return;
22+
for(int i=0;i<n;i++){
23+
for(int j=0;j<i;j++){
24+
int tmp = matrix[i][j];
25+
matrix[i][j]=matrix[j][i];
26+
matrix[j][i]=tmp;
27+
}
28+
}
29+
30+
for(int i=0;i<n;i++){
31+
for(int j=0;j<n/2;j++){
32+
int tmp = matrix[i][j];
33+
matrix[i][j]=matrix[i][n-1-j];
34+
matrix[i][n-1-j] = tmp;
35+
}
36+
}
37+
}
38+
public void rotate_2(int[][] matrix) {
39+
int n = matrix.length;
40+
if (n <= 1) return;
41+
int level = 0;
42+
while (level < n / 2) {
43+
for (int i = level; i < n - 1 - level; ++i) {
44+
int tmp = matrix[i][level];
45+
matrix[i][level] = matrix[n - 1 - level][i];
46+
matrix[n - 1 - level][i] = matrix[n - 1 - i][n - 1 - level];
47+
matrix[n - 1 - i][n - 1 - level] = matrix[level][n - 1 - i];
48+
matrix[level][n - 1 - i] = tmp;
49+
}
50+
++level;
51+
}
52+
}
53+
}

RotateList.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Author: King, [email protected]
3+
Date: Dec 25, 2014
4+
Problem: Rotate List
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/rotate-list/
7+
Notes:
8+
Given a list, rotate the list to the right by k places, where k is non-negative.
9+
10+
For example:
11+
Given 1->2->3->4->5->NULL and k = 2,
12+
return 4->5->1->2->3->NULL.
13+
14+
Solution: Notice that k can be larger than the list size (k % list_size).
15+
This solution traverses the list twice at most.
16+
*/
17+
18+
/**
19+
* Definition for singly-linked list.
20+
* struct ListNode {
21+
* int val;
22+
* ListNode *next;
23+
* ListNode(int x) : val(x), next(NULL) {}
24+
* };
25+
*/
26+
/**
27+
* Definition for singly-linked list.
28+
* public class ListNode {
29+
* int val;
30+
* ListNode next;
31+
* ListNode(int x) {
32+
* val = x;
33+
* next = null;
34+
* }
35+
* }
36+
*/
37+
public class Solution {
38+
public ListNode rotateRight(ListNode head, int k) {
39+
if (head == null) return head;
40+
int n = 1;
41+
ListNode tail = head, cur = head;
42+
while (tail.next != null) {
43+
tail = tail.next;
44+
++n;
45+
}
46+
k = k % n;
47+
if (k == 0) return head;
48+
for (int i = 0; i < n - k - 1; ++i)
49+
cur = cur.next;
50+
ListNode newHead = cur.next;
51+
tail.next = head;
52+
cur.next = null;
53+
return newHead;
54+
}
55+
}

ScrambleString.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
Author: King, [email protected]
3+
Date: Dec 25, 2014
4+
Problem: Scramble String
5+
Difficulty: Medium
6+
Source: https://oj.leetcode.com/problems/scramble-string/
7+
Notes:
8+
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.
9+
Below is one possible representation of s1 = "great":
10+
great
11+
/ \
12+
gr eat
13+
/ \ / \
14+
g r e at
15+
/ \
16+
a t
17+
To scramble the string, we may choose any non-leaf node and swap its two children.
18+
For example, if we choose the node "gr" and swap its two children, it produces a scrambled string "rgeat".
19+
rgeat
20+
/ \
21+
rg eat
22+
/ \ / \
23+
r g e at
24+
/ \
25+
a t
26+
We say that "rgeat" is a scrambled string of "great".
27+
Similarly, if we continue to swap the children of nodes "eat" and "at", it produces a scrambled string "rgtae".
28+
rgtae
29+
/ \
30+
rg tae
31+
/ \ / \
32+
r g ta e
33+
/ \
34+
t a
35+
We say that "rgtae" is a scrambled string of "great".
36+
Given two strings s1 and s2 of the same length, determine if s2 is a scrambled string of s1.
37+
38+
Solution: 1. Recursion + pruning.
39+
2. 3-dimensional dp.
40+
'dp[k][i][j] == true' means string s1(start from i, length k) is a scrambled string of string s2(start from j, length k).
41+
*/
42+
public class Solution {
43+
public boolean isScramble_1(String s1, String s2) {
44+
if (s1.compareTo(s2) == 0) return true;
45+
if (s1.length() != s2.length()) return false;
46+
return isScrambleRe(s1, s2);
47+
}
48+
public boolean isScrambleRe(String s1, String s2) {
49+
if (hasSameLetters(s1, s2) == false) return false;
50+
int len = s1.length();
51+
if (len == 0 || len == 1) return true;
52+
for (int i = 1; i < len; ++i) {
53+
if (isScrambleRe(s1.substring(0,i), s2.substring(0,i))
54+
&& isScrambleRe(s1.substring(i), s2.substring(i))
55+
|| isScrambleRe(s1.substring(0,i), s2.substring(len-i))
56+
&& isScrambleRe(s1.substring(i), s2.substring(0,len-i))) {
57+
return true;
58+
}
59+
}
60+
return false;
61+
}
62+
public boolean hasSameLetters(String s1, String s2) {
63+
if (s1.compareTo(s2) == 0) return true;
64+
if (s1.length() != s2.length()) return false;
65+
int[] count = new int[256];
66+
for (int i = 0; i < s1.length(); ++i) count[(int)s1.charAt(i)]++;
67+
for (int i = 0; i < s2.length(); ++i) count[(int)s2.charAt(i)]--;
68+
for (int i = 0; i < 256; ++i)
69+
if (count[i] != 0) return false;
70+
return true;
71+
}
72+
public boolean isScramble(String s1, String s2) {
73+
if (s1.compareTo(s2) == 0) return true;
74+
if (s1.length() != s2.length()) return false;
75+
int N = s1.length();
76+
boolean[][][] dp = new boolean[N+1][N][N];
77+
for (int k = 1; k <= N; ++k) {
78+
for (int i = 0; i <= N - k; ++i) {
79+
for (int j = 0; j <= N - k; ++j) {
80+
dp[k][i][j] = false;
81+
if (k == 1) dp[1][i][j] = (s1.charAt(i) == s2.charAt(j));
82+
for (int p = 1; p < k && !dp[k][i][j]; ++p) {
83+
if (dp[p][i][j] && dp[k-p][i+p][j+p] || dp[p][i][j+k-p] && dp[k-p][i+p][j])
84+
dp[k][i][j] = true;
85+
}
86+
}
87+
}
88+
}
89+
return dp[N][0][0];
90+
}
91+
}

0 commit comments

Comments
 (0)