Skip to content

Commit ab7b354

Browse files
committed
[Function add]
1. Add some leetcode solutions.
1 parent 11bec07 commit ab7b354

File tree

5 files changed

+269
-0
lines changed

5 files changed

+269
-0
lines changed

leetcode/127. Word Ladder.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
## 127. Word Ladder
2+
### Thinking:
3+
* Method1:
4+
* 通过递归实现, 无法AC
5+
6+
```Java
7+
class Solution {
8+
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
9+
if(wordList == null || wordList.size() == 0) return 0;
10+
int len = wordList.size();
11+
boolean[] used = new boolean[len];
12+
List<Integer> result = new ArrayList<>();
13+
change(beginWord, endWord, wordList, used, result, 1);
14+
if(result.size() == 0) return 0;
15+
int min = Integer.MAX_VALUE;
16+
for(int i = 0; i < result.size(); i++)
17+
min = Math.min(min, result.get(i));
18+
return min;
19+
}
20+
public static void change(String current, String endWord, List<String> wordList, boolean[] used, List<Integer> result, int step){
21+
if(current.equals(endWord)){
22+
result.add(step);
23+
}else{
24+
int len = wordList.size();
25+
for(int i = 0; i < len; i++){
26+
if(used[i]) continue;
27+
String temp = wordList.get(i);
28+
if(valid(current, temp)){
29+
used[i] = true;
30+
change(temp, endWord, wordList, used, result, step + 1);
31+
used[i] = false;
32+
}
33+
}
34+
}
35+
}
36+
private static boolean valid(String s, String t){
37+
int len = s.length();
38+
int temp = 0;
39+
for(int i = 0; i < len; i++){
40+
if(s.charAt(i) != t.charAt(i))
41+
temp++;
42+
if(temp == 2) return false;
43+
}
44+
return true;
45+
}
46+
}
47+
```
48+
49+
* Method 2
50+
* 考虑使用BFS,仍然无法AC
51+
52+
```Java
53+
class Solution {
54+
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
55+
LinkedList<String> q = new LinkedList<>();
56+
if(!wordList.contains(endWord)) return 0;
57+
q.add(beginWord);
58+
int count = 0;
59+
while(!q.isEmpty()){
60+
count++;
61+
for(int i = 0; i < q.size(); i++){
62+
String current = q.poll();
63+
if(current.equals(endWord)) return count;
64+
char[] arr = current.toCharArray();
65+
for(int j = 0; j < arr.length; j++){
66+
for(char a = 'a'; a <= 'z'; a++){
67+
char tempChar = arr[j];
68+
if(a == tempChar) continue;
69+
arr[j] = a;
70+
String temp = String.valueOf(arr);
71+
if(wordList.contains(temp)){
72+
q.add(temp);
73+
wordList.remove(temp);
74+
}
75+
arr[j] = tempChar;
76+
}
77+
}
78+
}
79+
}
80+
return 0;
81+
}
82+
}
83+
```
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
## 130. Surrounded Regions
2+
### Thinking:
3+
* Method1:
4+
* 只有与最外圈的O相连接的结点无法被转换。
5+
* 如果最外层为O,我们通过BFS,不断获取与之相邻的O结点。
6+
7+
```Java
8+
class Solution {
9+
public void solve(char[][] board) {
10+
if(board == null || board.length <= 2 || board[0].length <= 2) return;
11+
int height = board.length;
12+
int width = board[0].length;
13+
//row
14+
for(int i = 0; i < width; i++){
15+
if(board[0][i] == 'O')
16+
fill(board, 0, i);
17+
if(board[height - 1][i] == 'O')
18+
fill(board, height - 1, i);
19+
}
20+
//col
21+
for(int i = 0; i < height; i++){
22+
if(board[i][0] == 'O')
23+
fill(board, i, 0);
24+
if(board[i][width - 1] == 'O')
25+
fill(board, i, width - 1);
26+
}
27+
for(int i = 0; i < height; i++){
28+
for(int j = 0; j < width; j++){
29+
if(board[i][j] == 'O') board[i][j] = 'X';
30+
else if(board[i][j] == '#') board[i][j] = 'O';
31+
}
32+
}
33+
}
34+
private static void fill(char[][] board, int row, int col){
35+
LinkedList<Integer> q = new LinkedList<>();
36+
int width = board[0].length;
37+
board[row][col] = '#';
38+
q.add(row * width + col);
39+
while(!q.isEmpty()){
40+
Integer n = q.poll();
41+
int x = n / width;
42+
int y = n % width;
43+
if(x > 0) if(board[x - 1][y] == 'O'){board[x - 1][y] = '#'; q.add((x - 1) * width + y);}
44+
if(y > 0) if(board[x][y - 1] == 'O'){board[x][y - 1] = '#'; q.add(x * width + y - 1);}
45+
if(x < board.length - 1) if(board[x + 1][y] == 'O'){board[x + 1][y] = '#'; q.add((x + 1) * width + y);}
46+
if(y < board[0].length - 1) if(board[x][y + 1] == 'O'){board[x][y + 1] = '#'; q.add(x * width + y + 1);}
47+
}
48+
}
49+
}
50+
```
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
## 131. Palindrome Partitioning
2+
### Thinking:
3+
* Method1:
4+
* 通过递归实现。
5+
* 扫描每一位可能并通过回溯,当index的长度和字符串的长度一致时添加进入结果。
6+
7+
```Java
8+
class Solution {
9+
public List<List<String>> partition(String s) {
10+
List<List<String>> result = new ArrayList<>();
11+
if(s == null || s.length() == 0) return result;
12+
backtrace(s, result, new ArrayList<String>(), 0);
13+
return result;
14+
}
15+
public static void backtrace(String s, List<List<String>> result, List<String> list, int index){ //index: end position
16+
if(index == s.length()) result.add(new ArrayList<String>(list));
17+
else{
18+
for(int i = index + 1; i <= s.length(); i++){
19+
String sub = s.substring(index, i);
20+
if(isPalindrome(sub)){
21+
list.add(sub);
22+
backtrace(s, result, list, i);
23+
list.remove(list.size() - 1);
24+
}
25+
}
26+
}
27+
}
28+
private static boolean isPalindrome(String s){
29+
int len = s.length();
30+
if(len == 0 || len == 1) return true;
31+
int half = len / 2;
32+
int low = -1, high = len;
33+
while(++low < len && --high >= 0 && low <= high){
34+
char c1 = s.charAt(low);
35+
char c2 = s.charAt(high);
36+
if(c1 != c2) return false;
37+
}
38+
return true;
39+
}
40+
}
41+
```

leetcode/134. Gas Station.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
## 134. Gas Station
2+
### Thinking:
3+
* Method1:慢
4+
* 关于入口要找到gas大于cost。
5+
6+
```Java
7+
class Solution {
8+
public int canCompleteCircuit(int[] gas, int[] cost) {
9+
int len = gas.length;
10+
for(int i = 0; i < len; i++){
11+
if(cost[i] <= gas[i]){
12+
if(valid(gas, cost, i))
13+
return i;
14+
}
15+
}
16+
return -1;
17+
}
18+
private static boolean valid(int[] gas, int[] cost, int start){
19+
int remain = 0;
20+
int temp = start;
21+
for(; start < gas.length; start++){
22+
remain += (gas[start] - cost[start]);
23+
if(remain < 0) return false;
24+
}
25+
for(start = 0; start < temp; start++){
26+
remain += (gas[start] - cost[start]);
27+
if(remain < 0) return false;
28+
}
29+
return true;
30+
}
31+
}
32+
```
33+
34+
* Method2:
35+
36+
```Java
37+
class Solution {
38+
public int canCompleteCircuit(int[] gas, int[] cost) {
39+
int len = gas.length;
40+
int sum = 0;
41+
int total = 0;
42+
int index = 0;
43+
for(int i = 0; i < len; i++){
44+
sum += gas[i] - cost[i];
45+
if(sum < 0){
46+
total += sum;
47+
sum = 0;
48+
index = i+1;
49+
}
50+
}
51+
total += sum;
52+
return (total >= 0) ? index : -1;
53+
}
54+
}
55+
```
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## 138. Copy List with Random Pointer
2+
### Thinking:
3+
* Method1:
4+
5+
```Java
6+
/**
7+
* Definition for singly-linked list with a random pointer.
8+
* class RandomListNode {
9+
* int label;
10+
* RandomListNode next, random;
11+
* RandomListNode(int x) { this.label = x; }
12+
* };
13+
*/
14+
public class Solution {
15+
public RandomListNode copyRandomList(RandomListNode head) {
16+
RandomListNode cur = head;
17+
RandomListNode dummy = new RandomListNode(-1);
18+
RandomListNode dummyCur = dummy;
19+
Map<Integer, RandomListNode> m = new HashMap<>();
20+
while(cur != null){
21+
dummyCur.next = new RandomListNode(cur.label);
22+
m.put(cur.label, dummyCur.next);
23+
dummyCur = dummyCur.next;
24+
cur = cur.next;
25+
}
26+
dummyCur.next = null;
27+
cur = head;
28+
dummyCur = dummy.next;
29+
while(cur != null){
30+
if(cur.random == null)
31+
dummyCur.random = null;
32+
else
33+
dummyCur.random = m.get(cur.random.label);
34+
dummyCur = dummyCur.next;
35+
cur = cur.next;
36+
}
37+
return dummy.next;
38+
}
39+
}
40+
```

0 commit comments

Comments
 (0)