Skip to content

Commit a5a012f

Browse files
author
xutao
committed
提交
1 parent 29003af commit a5a012f

4 files changed

Lines changed: 117 additions & 1 deletion

File tree

Week07/ClimbStairs.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* 爬楼梯
3+
* fn=fn-1+fn-2
4+
* 滑动数组
5+
*/
6+
class ClimbStairs {
7+
public int climbStairs(int n) {
8+
int p = 0, q = 1, r = 0;
9+
for (int i = 1; i <= n; i++) {
10+
r = p + q;
11+
p = q;
12+
q = r;
13+
14+
}
15+
return r;
16+
}
17+
}

Week07/LadderLength.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import javafx.util.Pair;
2+
3+
import java.util.*;
4+
5+
class LadderLength {
6+
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
7+
8+
// Since all words are of same length.
9+
int L = beginWord.length();
10+
11+
// Dictionary to hold combination of words that can be formed,
12+
// from any given word. By changing one letter at a time.
13+
Map<String, List<String>> allComboDict = new HashMap<>();
14+
15+
wordList.forEach(
16+
word -> {
17+
for (int i = 0; i < L; i++) {
18+
// Key is the generic word
19+
// Value is a list of words which have the same intermediate generic word.
20+
String newWord = word.substring(0, i) + '*' + word.substring(i + 1, L);
21+
List<String> transformations = allComboDict.getOrDefault(newWord, new ArrayList<>());
22+
transformations.add(word);
23+
allComboDict.put(newWord, transformations);
24+
}
25+
});
26+
27+
// Queue for BFS
28+
Queue<Pair<String, Integer>> Q = new LinkedList<>();
29+
Q.add(new Pair(beginWord, 1));
30+
31+
// Visited to make sure we don't repeat processing same word.
32+
Map<String, Boolean> visited = new HashMap<>();
33+
visited.put(beginWord, true);
34+
35+
while (!Q.isEmpty()) {
36+
Pair<String, Integer> node = Q.remove();
37+
String word = node.getKey();
38+
int level = node.getValue();
39+
for (int i = 0; i < L; i++) {
40+
41+
// Intermediate words for current word
42+
String newWord = word.substring(0, i) + '*' + word.substring(i + 1, L);
43+
44+
// Next states are all the words which share the same intermediate state.
45+
for (String adjacentWord : allComboDict.getOrDefault(newWord, new ArrayList<>())) {
46+
// If at any point if we find what we are looking for
47+
// i.e. the end word - we can return with the answer.
48+
if (adjacentWord.equals(endWord)) {
49+
return level + 1;
50+
}
51+
// Otherwise, add it to the BFS Queue. Also mark it visited
52+
if (!visited.containsKey(adjacentWord)) {
53+
visited.put(adjacentWord, true);
54+
Q.add(new Pair(adjacentWord, level + 1));
55+
}
56+
}
57+
}
58+
}
59+
60+
return 0;
61+
}
62+
}

Week07/NOTE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
学习笔记
1+
来不及写了,明早补

Week07/ValidSudoku.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.HashMap;
2+
3+
class ValidSudoku {
4+
public boolean isValidSudoku(char[][] board) {
5+
// init data
6+
HashMap<Integer, Integer>[] rows = new HashMap[9];
7+
HashMap<Integer, Integer> [] columns = new HashMap[9];
8+
HashMap<Integer, Integer> [] boxes = new HashMap[9];
9+
for (int i = 0; i < 9; i++) {
10+
rows[i] = new HashMap<Integer, Integer>();
11+
columns[i] = new HashMap<Integer, Integer>();
12+
boxes[i] = new HashMap<Integer, Integer>();
13+
}
14+
15+
// validate a board
16+
for (int i = 0; i < 9; i++) {
17+
for (int j = 0; j < 9; j++) {
18+
char num = board[i][j];
19+
if (num != '.') {
20+
int n = (int)num;
21+
int box_index = (i / 3 ) * 3 + j / 3;
22+
23+
// keep the current cell value
24+
rows[i].put(n, rows[i].getOrDefault(n, 0) + 1);
25+
columns[j].put(n, columns[j].getOrDefault(n, 0) + 1);
26+
boxes[box_index].put(n, boxes[box_index].getOrDefault(n, 0) + 1);
27+
28+
// check if this value has been already seen before
29+
if (rows[i].get(n) > 1 || columns[j].get(n) > 1 || boxes[box_index].get(n) > 1)
30+
return false;
31+
}
32+
}
33+
}
34+
35+
return true;
36+
}
37+
}

0 commit comments

Comments
 (0)