Skip to content

Commit fb31279

Browse files
committed
Write some code.
1 parent 6e9d57c commit fb31279

File tree

5 files changed

+236
-0
lines changed

5 files changed

+236
-0
lines changed

Interleaving String.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,51 @@ public boolean isInterleave(String s1, String s2, String s3) {
2323
mat[i][j] = (mat[i][j - 1] && s2.charAt(j - 1) == s3.charAt(i + j - 1)) || (mat[i - 1][j] && s1.charAt(i - 1) == s3.charAt(i + j - 1));
2424
return mat[s1.length()][s2.length()];
2525
}
26+
}
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
public class Solution {
39+
public boolean isInterleave(String s1, String s2, String s3) {
40+
return search(s1, s2, s3, 0, 0, 0);
41+
}
42+
43+
private boolean search(String s1, String s2, String s3, int i1, int i2, int i3) {
44+
int len1 = s1.length(), len2 = s2.length();
45+
if (i1 < len1 && i2 < len2) {
46+
boolean result = false;
47+
if (s1.charAt(i1) == s3.charAt(i3)) {
48+
i1++;
49+
i3++;
50+
result = search(s1, s2, s3, i1, i2, i3);
51+
if (result) {
52+
return true;
53+
} else {
54+
i1--;
55+
i3--;
56+
}
57+
}
58+
59+
if (s2.charAt(i2) == s3.charAt(i3)) {
60+
i2++;
61+
i3++;
62+
result = search(s1, s2, s3, i1, i2, i3);
63+
return result;
64+
}
65+
66+
return false;
67+
}
68+
if (i1 < len1) {
69+
return s1.substring(i1).compareTo(s3.substring(i3)) == 0;
70+
}
71+
return s2.substring(i2).compareTo(s3.substring(i3)) == 0;
72+
}
2673
}

Maximal Rectangle.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
2+
3+
public class Solution {
4+
public int maximalRectangle(char[][] matrix) {
5+
int row = matrix.length;
6+
if (row == 0) return 0;
7+
int col = matrix[0].length;
8+
int[][] map = new int[row][col];
9+
int area = 0;
10+
for (int i = 0; i < row; i++) {
11+
for (int j = 0; j < col; j++) {
12+
int prev = 0;
13+
if (i != 0) {
14+
prev = map[i - 1][j];
15+
}
16+
if (matrix[i][j] == '1') {
17+
map[i][j] = prev + 1;
18+
} else {
19+
map[i][j] = prev;
20+
}
21+
}
22+
}
23+
for (int i = 0; i < row; i++) {
24+
for (int j = i; j < row; j++) {
25+
int[] line = new int[col];
26+
for (int k = 0; k < col; k++) {
27+
line[k] = map[j][k] - map[i][k] + (matrix[i][k] == '0' ? 0 : 1);
28+
}
29+
int l = 0;
30+
int tmp = 0;
31+
for (int f = 0; f < col; f++) {
32+
if (line[f] == j - i + 1) tmp++;
33+
else {
34+
if (tmp > l) {
35+
l = tmp;
36+
}
37+
tmp = 0;
38+
}
39+
}
40+
if (tmp > l) {
41+
l = tmp;
42+
}
43+
int s = (j - i + 1) * l;
44+
if (s > area) {
45+
area = s;
46+
}
47+
}
48+
}
49+
return area;
50+
}
51+
}

Scramble String.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.
2+
3+
Below is one possible representation of s1 = "great":
4+
5+
great
6+
/ \
7+
gr eat
8+
/ \ / \
9+
g r e at
10+
/ \
11+
a t
12+
To scramble the string, we may choose any non-leaf node and swap its two children.
13+
14+
For example, if we choose the node "gr" and swap its two children, it produces a scrambled string "rgeat".
15+
16+
rgeat
17+
/ \
18+
rg eat
19+
/ \ / \
20+
r g e at
21+
/ \
22+
a t
23+
We say that "rgeat" is a scrambled string of "great".
24+
25+
Similarly, if we continue to swap the children of nodes "eat" and "at", it produces a scrambled string "rgtae".
26+
27+
rgtae
28+
/ \
29+
rg tae
30+
/ \ / \
31+
r g ta e
32+
/ \
33+
t a
34+
We say that "rgtae" is a scrambled string of "great".
35+
36+
Given two strings s1 and s2 of the same length, determine if s2 is a scrambled string of s1.
37+
38+
public class Solution {
39+
public boolean isScramble(String s1, String s2) {
40+
int length1 = s1.length();
41+
int length2 = s2.length();
42+
if (length1 != length2)
43+
return false;
44+
45+
if (length1 == 0 || s1.equals(s2))
46+
return true;
47+
48+
char[] ca1 = s1.toCharArray();
49+
char[] ca2 = s2.toCharArray();
50+
Arrays.sort(ca1);
51+
Arrays.sort(ca2);
52+
if (!Arrays.equals(ca1, ca2))
53+
return false;
54+
55+
int i = 1;
56+
while (i < length1) {
57+
String a1 = s1.substring(0, i);
58+
String b1 = s1.substring(i, length1);
59+
String a2 = s2.substring(0, i);
60+
String b2 = s2.substring(i, length2);
61+
if (a1.equals(b2) && b1.equals(a2)) return true;
62+
boolean r = isScramble(a1, a2) && isScramble(b1, b2);
63+
if (!r) {
64+
String c2 = s2.substring(0, length1 - i);
65+
String d2 = s2.substring(length1 - i);
66+
r = isScramble(a1, d2) && isScramble(b1, c2);
67+
}
68+
if (r) return true;
69+
i++;
70+
}
71+
return false;
72+
}
73+
}

Set Matrix Zeroes.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
2+
3+
Follow up:
4+
Did you use extra space?
5+
A straight forward solution using O(mn) space is probably a bad idea.
6+
A simple improvement uses O(m + n) space, but still not the best solution.
7+
Could you devise a constant space solution?
8+
9+
public class Solution {
10+
public void setZeroes(int[][] matrix) {
11+
int row = matrix.length;
12+
if (row == 0) return;
13+
int col = matrix[0].length;
14+
for (int i = 0; i < row; i++) {
15+
for (int j = 0; j < col; j++) {
16+
if (matrix[i][j] == 0) {
17+
for (int k = 0; k < col; k++) {
18+
if (matrix[i][k] != 0)
19+
matrix[i][k] = -1;
20+
}
21+
for (int f = 0; f < row; f++) {
22+
if (matrix[f][j] != 0)
23+
matrix[f][j] = -1;
24+
}
25+
}
26+
}
27+
}
28+
for (int i = 0; i < row; i++) {
29+
for (int j = 0; j < col; j++) {
30+
if (matrix[i][j] == -1) {
31+
matrix[i][j] = 0;
32+
}
33+
}
34+
}
35+
}
36+
}

Unique Binary Search Trees.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Given n, how many structurally unique BST's (binary search trees) that store values 1...n?
2+
3+
For example,
4+
Given n = 3, there are a total of 5 unique BST's.
5+
6+
1 3 3 2 1
7+
\ / / / \ \
8+
3 2 1 1 3 2
9+
/ / \ \
10+
2 1 2 3
11+
12+
public class Solution {
13+
public int numTrees(int n) {
14+
if (n == 1) return 1;
15+
if (n == 2) return 2;
16+
int[] record = new int[n + 1];
17+
record[0] = 1;
18+
record[1] = 1;
19+
record[2] = 2;
20+
for (int i = 3; i <= n; i++) {
21+
int tmp = 0;
22+
for (int k = 0; k < i; k++) {
23+
tmp += (record[k] * record[i - k - 1]);
24+
}
25+
record[i] = tmp;
26+
}
27+
return record[n];
28+
}
29+
}

0 commit comments

Comments
 (0)