Skip to content

Commit 16e09dc

Browse files
committed
dfs
1 parent 4d76c43 commit 16e09dc

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

src/Subsets/Solution.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package Subsets;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
/**
8+
* User: Danyang
9+
* Date: 1/26/2015
10+
* Time: 13:51
11+
*
12+
* Given a set of distinct integers, S, return all possible subsets.
13+
14+
Note:
15+
Elements in a subset must be in non-descending order.
16+
The solution set must not contain duplicate subsets.
17+
For example,
18+
If S = [1,2,3], a solution is:
19+
20+
[
21+
[3],
22+
[1],
23+
[2],
24+
[1,2,3],
25+
[1,3],
26+
[2,3],
27+
[1,2],
28+
[]
29+
]
30+
*/
31+
public class Solution {
32+
/**
33+
* Tree
34+
* Notice:
35+
* 1. add at leaves
36+
* @param S
37+
* @return
38+
*/
39+
public List<List<Integer>> subsets(int[] S) {
40+
Arrays.sort(S);
41+
List<List<Integer>> ret = new ArrayList<>();
42+
dfs(S, 0, new ArrayList<>(), ret);
43+
return ret;
44+
}
45+
46+
void dfs(int[] S, int i, List<Integer> cur, List<List<Integer>> ret) {
47+
if(i==S.length)
48+
ret.add(new ArrayList<>(cur));
49+
if(i<S.length) {
50+
dfs(S, i+1, cur, ret);
51+
cur.add(S[i]);
52+
dfs(S, i+1, cur, ret);
53+
cur.remove(cur.size()-1);
54+
}
55+
}
56+
}

src/WordSearch/Solution.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package WordSearch;
2+
3+
/**
4+
* User: Danyang
5+
* Date: 1/26/2015
6+
* Time: 14:16
7+
*
8+
* Given a 2D board and a word, find if the word exists in the grid.
9+
10+
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally
11+
or vertically neighboring. The same letter cell may not be used more than once.
12+
13+
For example,
14+
Given board =
15+
16+
[
17+
["ABCE"],
18+
["SFCS"],
19+
["ADEE"]
20+
]
21+
word = "ABCCED", -> returns true,
22+
word = "SEE", -> returns true,
23+
word = "ABCB", -> returns false.
24+
*/
25+
public class Solution {
26+
int[][] directions = new int[][] {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
27+
/**
28+
* O(N*2)*O(N*2)
29+
* traverse + dfs
30+
* @param board
31+
* @param word
32+
* @return
33+
*/
34+
public boolean exist(char[][] board, String word) {
35+
int m = board.length;
36+
int n = board[0].length;
37+
boolean[][] visited = new boolean[m][n];
38+
for(int i=0; i<m; i++)
39+
for(int j=0; j<n; j++)
40+
visited[i][j] = false;
41+
42+
43+
for(int i=0; i<m; i++)
44+
for(int j=0; j<n; j++)
45+
if(dfs(board, word, visited, 0, i, j))
46+
return true;
47+
return false;
48+
}
49+
50+
boolean dfs(char[][] board, String word, boolean[][] visited, int cur, int i, int j) {
51+
int m = board.length;
52+
int n = board[0].length;
53+
if(board[i][j]==word.charAt(cur)) {
54+
if(cur==word.length()-1)
55+
return true;
56+
visited[i][j] = true;
57+
for(int dir=0; dir<4; dir++) {
58+
int next_i = i+directions[dir][0];
59+
int next_j = j+directions[dir][1];
60+
if(next_i>=0 && next_i<m && next_j>=0 && next_j<n && !visited[next_i][next_j]) {
61+
if(dfs(board, word, visited, cur+1, next_i, next_j))
62+
return true;
63+
}
64+
}
65+
visited[i][j] = false;
66+
}
67+
return false;
68+
}
69+
70+
public static void main(String[] args) {
71+
new Solution().exist(new char[][] {{'a', 'a'}}, "aa");
72+
}
73+
}

0 commit comments

Comments
 (0)