Skip to content

Commit 1e7d634

Browse files
committed
[Function add]
1. Add leetcode solutions.
1 parent d568e78 commit 1e7d634

4 files changed

+115
-0
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,12 @@
463463

464464
[295.Find Median from Data Stream](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/295.Find%20Median%20from%20Data%20Stream.md)
465465

466+
[297. Serialize and Deserialize Binary Tree](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/297.%20Serialize%20and%20Deserialize%20Binary%20Tree.md)
467+
468+
[299. Bulls and Cows](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/299.%20Bulls%20and%20Cows.md)
469+
470+
[300. Longest Increasing Subsequence](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/300.%20Longest%20Increasing%20Subsequence.md)
471+
466472

467473
## Algorithm(4th_Edition)
468474
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.

leetcode/297. Serialize and Deserialize Binary Tree.md

+55
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,61 @@ public class Codec {
7373
}
7474
}
7575

76+
// Your Codec object will be instantiated and called as such:
77+
// Codec codec = new Codec();
78+
// codec.deserialize(codec.serialize(root));
79+
```
80+
81+
### Second time
82+
![Imgur](https://i.imgur.com/IcQCB2w.png)
83+
```Java
84+
/**
85+
* Definition for a binary tree node.
86+
* public class TreeNode {
87+
* int val;
88+
* TreeNode left;
89+
* TreeNode right;
90+
* TreeNode(int x) { val = x; }
91+
* }
92+
*/
93+
public class Codec {
94+
private final String split = ",";
95+
private final String empty = "#";
96+
97+
// Encodes a tree to a single string.
98+
public String serialize(TreeNode root) {
99+
StringBuilder sb = new StringBuilder();
100+
serialize(root, sb);
101+
return sb.toString();
102+
}
103+
private void serialize(TreeNode node, StringBuilder sb){
104+
if(node == null){
105+
sb.append(empty).append(split);
106+
}else{
107+
sb.append(node.val).append(split);
108+
serialize(node.left, sb);
109+
serialize(node.right, sb);
110+
}
111+
}
112+
// Decodes your encoded data to tree.
113+
public TreeNode deserialize(String data) {
114+
String[] nodes = data.split(split);
115+
LinkedList<String> q = new LinkedList<>();
116+
q.addAll(Arrays.asList(nodes));
117+
return deserialize(q);
118+
}
119+
private TreeNode deserialize(LinkedList<String> q){
120+
String val = q.poll();
121+
if(val.equals(empty)){
122+
return null;
123+
}else{
124+
TreeNode node = new TreeNode(Integer.parseInt(val));
125+
node.left = deserialize(q);
126+
node.right = deserialize(q);
127+
return node;
128+
}
129+
}
130+
}
76131
// Your Codec object will be instantiated and called as such:
77132
// Codec codec = new Codec();
78133
// codec.deserialize(codec.serialize(root));

leetcode/299. Bulls and Cows.md

+30
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,34 @@ class Solution {
6464
return sb.toString();
6565
}
6666
}
67+
```
68+
69+
### Second time
70+
1. We actually need 2 arrays to save the frequency of the each letters.
71+
2. If save position and value, nothing saved to table, and A++.
72+
3. If not save, increase the frequency for both of the values.
73+
4. Finally we take the sum of the minimum values in bonth arrays, which is the value of B.
74+
```Java
75+
class Solution {
76+
public String getHint(String secret, String guess) {
77+
char[] sArr = secret.toCharArray();
78+
char[] gArr = guess.toCharArray();
79+
int A = 0;
80+
int B = 0;
81+
int[] aCount = new int[10];
82+
int[] bCount = new int[10];
83+
for(int i = 0; i < sArr.length; i++){
84+
if(sArr[i] == gArr[i]){
85+
A++;
86+
}else{
87+
aCount[sArr[i] - '0']++;
88+
bCount[gArr[i] - '0']++;
89+
}
90+
}
91+
for(int i = 0; i < 10; i++){
92+
B += Math.min(aCount[i], bCount[i]);
93+
}
94+
return A + "A" + B + "B";
95+
}
96+
}
6797
```

leetcode/300. Longest Increasing Subsequence.md

+24
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,28 @@ class Solution {
3535
return res;
3636
}
3737
}
38+
```
39+
40+
### Second time
41+
1. Still use dp to solve this question. O(N^2)
42+
```Java
43+
class Solution {
44+
public int lengthOfLIS(int[] nums) {
45+
if(nums == null || nums.length == 0) return 0;
46+
int len = nums.length;
47+
int[] dp = new int[len + 1];
48+
for(int i = 1; i <= len; i++) dp[i] = 1;
49+
int result = 1;
50+
for(int i = 2; i <= len; i++){
51+
int num = nums[i - 1];
52+
for(int j = i - 1; j >= 1; j--){
53+
if(num > nums[j - 1]){
54+
dp[i] = Math.max(dp[i], dp[j] + 1);
55+
}
56+
}
57+
result = Math.max(dp[i], result);
58+
}
59+
return result;
60+
}
61+
}
3862
```

0 commit comments

Comments
 (0)