Skip to content

Commit 5ab4b89

Browse files
committed
[Function add]
1. Add some leetcode solutions.
1 parent e027aa3 commit 5ab4b89

File tree

4 files changed

+147
-1
lines changed

4 files changed

+147
-1
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,22 @@
229229

230230
[120. Triangle](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/120.%20Triangle.md)
231231

232+
[121. Best Time to Buy and Sell Stock](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/121.%20Best%20Time%20to%20Buy%20and%20Sell%20Stock.md)
233+
234+
[122. Best Time to Buy and Sell Stock II](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/122.%20Best%20Time%20to%20Buy%20and%20Sell%20Stock%20II.md)
235+
236+
[123. Best Time to Buy and Sell Stock III](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/123.%20Best%20Time%20to%20Buy%20and%20Sell%20Stock%20III.md)
237+
238+
[124. Binary Tree Maximum Path Sum](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/124.%20Binary%20Tree%20Maximum%20Path%20Sum.md)
239+
240+
[125. Valid Palindrome](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/125.%20Valid%20Palindrome.md)
241+
242+
[127. Word Ladder](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/127.%20Word%20Ladder.md)
243+
244+
[128. Longest Consecutive Sequence](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/128.%20Longest%20Consecutive%20Sequence.md)
245+
246+
[129. Sum Root to Leaf Numbers](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/129.%20Sum%20Root%20to%20Leaf%20Numbers.md)
247+
232248
## Algorithm(4th_Edition)
233249
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.
234250
All java realization codes are placed in different packages.

leetcode/127. Word Ladder.md

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,40 @@
11
## 127. Word Ladder
2+
### Question:
3+
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:
4+
1. Only one letter can be changed at a time.
5+
2. Each transformed word must exist in the word list. Note that beginWord is not a transformed word.
6+
7+
Note:
8+
* Return 0 if there is no such transformation sequence.
9+
* All words have the same length.
10+
* All words contain only lowercase alphabetic characters.
11+
* You may assume no duplicates in the word list.
12+
* You may assume beginWord and endWord are non-empty and are not the same.
13+
14+
```
15+
Example 1:
16+
17+
Input:
18+
beginWord = "hit",
19+
endWord = "cog",
20+
wordList = ["hot","dot","dog","lot","log","cog"]
21+
22+
Output: 5
23+
24+
Explanation: As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
25+
return its length 5.
26+
27+
Example 2:
28+
29+
Input:
30+
beginWord = "hit"
31+
endWord = "cog"
32+
wordList = ["hot","dot","dog","lot","log"]
33+
34+
Output: 0
35+
```
36+
Explanation: The endWord "cog" is not in wordList, therefore no possible transformation.
37+
238
### Thinking:
339
* Method1:
440
* 通过递归实现, 无法AC
@@ -80,4 +116,42 @@ class Solution {
80116
return 0;
81117
}
82118
}
83-
```
119+
```
120+
121+
### 二刷
122+
1. 一刷的时候没有写出来这道题目,尝试了bfs但是无法AC。
123+
2. 二刷的时候参考了[LeetCode 127. Word Ladder](https://www.jianshu.com/p/753bd585d57e), 实际上这就是一道最短路径的题目。
124+
```Java
125+
class Solution {
126+
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
127+
if(beginWord == null || endWord == null) return 0;
128+
Set<String> wordSet = new HashSet<>(wordList);
129+
Set<String> visited = new HashSet<>();
130+
visited.add(beginWord);
131+
int res = 1;
132+
while(!visited.contains(endWord)){
133+
Set<String> temp = new HashSet<>();
134+
for(String s : visited){
135+
for(int i = 0; i < s.length(); i++){
136+
char[] arr = s.toCharArray();
137+
for(char c = 'a'; c <= 'z'; c++){
138+
arr[i] = c;
139+
String newWord = new String(arr);
140+
if(wordSet.contains(newWord)){
141+
temp.add(newWord);
142+
wordSet.remove(newWord);
143+
}
144+
}
145+
}
146+
}
147+
if(temp.size() == 0) return 0;
148+
res ++;
149+
visited = temp;
150+
}
151+
return res;
152+
}
153+
}
154+
```
155+
156+
### Reference
157+
1. [LeetCode 127. Word Ladder](https://www.jianshu.com/p/753bd585d57e)

leetcode/128. Longest Consecutive Sequence.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,27 @@ class Solution {
6565
return max;
6666
}
6767
}
68+
```
69+
70+
### 二刷
71+
其实用Set就可以解决问题,最重要的是,要判断前面的那一位数字并不存在,减少了很多无谓的判断。
72+
```Java
73+
class Solution {
74+
public int longestConsecutive(int[] nums) {
75+
if(nums == null || nums.length == 0) return 0;
76+
Set<Integer> set = new HashSet<>();
77+
for(int n : nums) set.add(n);
78+
int result = 0;
79+
for(int i = 0; i < nums.length; i++){
80+
int res = 1;
81+
if(!set.contains(nums[i] - 1)){
82+
while(set.contains(++nums[i])){
83+
res++;
84+
}
85+
}
86+
result = Math.max(res, result);
87+
}
88+
return result;
89+
}
90+
}
6891
```

leetcode/129. Sum Root to Leaf Numbers.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,37 @@ class Solution {
7373
dfs(root.right, sum, result);
7474
}
7575
}
76+
```
77+
78+
### 二刷
79+
典型的dfs,深度优先。
80+
```Java
81+
/**
82+
* Definition for a binary tree node.
83+
* public class TreeNode {
84+
* int val;
85+
* TreeNode left;
86+
* TreeNode right;
87+
* TreeNode(int x) { val = x; }
88+
* }
89+
*/
90+
class Solution {
91+
public int sumNumbers(TreeNode root) {
92+
if(root == null) return 0;
93+
List<Integer> temp = new LinkedList<>();
94+
dfs(root, temp, 0);
95+
int result = 0;
96+
for(Integer s : temp) result += s;
97+
return result;
98+
}
99+
private void dfs(TreeNode node, List<Integer> sum, int cur){
100+
cur = cur * 10 + node.val;
101+
if(node.left == null && node.right == null)
102+
sum.add(cur);
103+
else{
104+
if(node.left != null) dfs(node.left, sum, cur);
105+
if(node.right != null) dfs(node.right, sum, cur);
106+
}
107+
}
108+
}
76109
```

0 commit comments

Comments
 (0)