Skip to content

Commit b744e3e

Browse files
author
digitspace
committed
569-Week 06
1 parent 24492e2 commit b744e3e

File tree

4 files changed

+295
-0
lines changed

4 files changed

+295
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* 130. Surrounded Regions
3+
* 被围绕的区域
4+
* 给定一个二维的矩阵,包含 'X' 和 'O'(字母 O)。
5+
6+
找到所有被 'X' 围绕的区域,并将这些区域里所有的 'O' 用 'X' 填充。
7+
8+
示例:
9+
10+
X X X X
11+
X O O X
12+
X X O X
13+
X O X X
14+
运行你的函数后,矩阵变为:
15+
16+
X X X X
17+
X X X X
18+
X X X X
19+
X O X X
20+
21+
*/
22+
public class LeetCode_130_569 {
23+
24+
public static void main(String[] args) {
25+
// TODO Auto-generated method stub
26+
27+
}
28+
29+
class Solution {
30+
public void solve(char[][] board) {
31+
for( int i = 0; i < board.length; i++ ) {
32+
for( int j = 0; j < board[0].length; j++ ) {
33+
if( ( (i==0) || (j==0) || (i==board.length-1) || (j==board[0].length-1) )
34+
&& board[i][j] == 'O' ) {
35+
dfs(board, i, j);
36+
}
37+
}
38+
}
39+
for( int i = 0; i < board.length; i++ ) {
40+
for( int j = 0; j < board[0].length; j++ ) {
41+
if( board[i][j] == 'O' )
42+
board[i][j] = 'X';
43+
if( board[i][j] == '#' )
44+
board[i][j] = 'O';
45+
}
46+
}
47+
}
48+
49+
void dfs( char[][] board, int i, int j ) {
50+
if( i >= 0 && i < board.length && j >= 0 && j < board[0].length && board[i][j] == 'O') {
51+
board[i][j] = '#';
52+
dfs(board, i-1, j);
53+
dfs(board, i+1, j);
54+
dfs(board, i, j-1);
55+
dfs(board, i, j+1);
56+
}
57+
}
58+
}
59+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* 208. Implement Trie (Prefix Tree)
3+
* 实现 Trie (前缀树)
4+
* 实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作。
5+
6+
示例:
7+
8+
Trie trie = new Trie();
9+
10+
trie.insert("apple");
11+
trie.search("apple"); // 返回 true
12+
trie.search("app"); // 返回 false
13+
trie.startsWith("app"); // 返回 true
14+
trie.insert("app");
15+
trie.search("app"); // 返回 true
16+
说明:
17+
18+
你可以假设所有的输入都是由小写字母 a-z 构成的。
19+
保证所有输入均为非空字符串。
20+
21+
*/
22+
public class LeetCode_208_569 {
23+
24+
public static void main(String[] args) {
25+
// TODO Auto-generated method stub
26+
27+
}
28+
29+
30+
class Trie {
31+
32+
class TrieNode {
33+
char value;
34+
boolean bWord;
35+
TrieNode[] children = new TrieNode[26];
36+
37+
public TrieNode(){}
38+
public TrieNode(char c) {
39+
value = c;
40+
}
41+
42+
public TrieNode get( char c ) {
43+
return children[c-'a'];
44+
}
45+
46+
public void set( char c, TrieNode node ) {
47+
children[c-'a'] = node;
48+
}
49+
50+
public boolean contain( char c ) {
51+
return children[c-'a'] != null;
52+
}
53+
54+
public void setWord() {
55+
bWord = true;
56+
}
57+
58+
public boolean isWord() {
59+
return bWord;
60+
}
61+
}
62+
63+
TrieNode root;
64+
65+
/** Initialize your data structure here. */
66+
public Trie() {
67+
root = new TrieNode(' ');
68+
}
69+
70+
/** Inserts a word into the trie. */
71+
public void insert(String word) {
72+
TrieNode node = root;
73+
for( int i = 0; i < word.length(); i++ ) {
74+
if( !node.contain(word.charAt(i)) )
75+
node.set(word.charAt(i), new TrieNode(word.charAt(i)));
76+
node = node.get(word.charAt(i));
77+
}
78+
node.setWord();
79+
}
80+
81+
/** Returns if the word is in the trie. */
82+
public boolean search(String word) {
83+
TrieNode node = root;
84+
for( int i = 0; i < word.length(); i++ ) {
85+
if( !node.contain(word.charAt(i)) )
86+
return false;
87+
node = node.get(word.charAt(i));
88+
}
89+
return node.isWord();
90+
}
91+
92+
/** Returns if there is any word in the trie that starts with the given prefix. */
93+
public boolean startsWith(String prefix) {
94+
TrieNode node = root;
95+
for( int i = 0; i < prefix.length(); i++ ) {
96+
if( !node.contain(prefix.charAt(i)) )
97+
return false;
98+
node = node.get(prefix.charAt(i));
99+
}
100+
return true;
101+
}
102+
}
103+
104+
/**
105+
* Your Trie object will be instantiated and called as such:
106+
* Trie obj = new Trie();
107+
* obj.insert(word);
108+
* boolean param_2 = obj.search(word);
109+
* boolean param_3 = obj.startsWith(prefix);
110+
*/
111+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* 547. Friend Circles
3+
* 朋友圈
4+
*
5+
* 班上有 N 名学生。其中有些人是朋友,有些则不是。他们的友谊具有是传递性。如果已知 A 是 B 的朋友,B 是 C 的朋友,那么我们可以认为 A 也是 C 的朋友。所谓的朋友圈,是指所有朋友的集合。
6+
7+
给定一个 N * N 的矩阵 M,表示班级中学生之间的朋友关系。如果M[i][j] = 1,表示已知第 i 个和 j 个学生互为朋友关系,否则为不知道。你必须输出所有学生中的已知的朋友圈总数。
8+
9+
示例 1:
10+
11+
输入:
12+
[[1,1,0],
13+
[1,1,0],
14+
[0,0,1]]
15+
输出: 2
16+
说明:已知学生0和学生1互为朋友,他们在一个朋友圈。
17+
第2个学生自己在一个朋友圈。所以返回2。
18+
示例 2:
19+
20+
输入:
21+
[[1,1,0],
22+
[1,1,1],
23+
[0,1,1]]
24+
输出: 1
25+
说明:已知学生0和学生1互为朋友,学生1和学生2互为朋友,所以学生0和学生2也是朋友,所以他们三个在一个朋友圈,返回1。
26+
27+
28+
*/
29+
public class LeetCode_547_569 {
30+
31+
public static void main(String[] args) {
32+
// TODO Auto-generated method stub
33+
34+
}
35+
36+
class Solution {
37+
class UnionFind {
38+
private int count = 0;
39+
private int[] parent;
40+
public UnionFind(int n) {
41+
count = n;
42+
parent = new int[n];
43+
for (int i = 0; i < n; i++) {
44+
parent[i] = i;
45+
}
46+
}
47+
public int find(int p) {
48+
while (p != parent[p]) {
49+
parent[p] = parent[parent[p]];
50+
p = parent[p];
51+
}
52+
return p;
53+
}
54+
public void union(int p, int q) {
55+
int rootP = find(p);
56+
int rootQ = find(q);
57+
if (rootP == rootQ) return;
58+
parent[rootP] = rootQ;
59+
count--;
60+
}
61+
}
62+
public int findCircleNum(int[][] M) {
63+
int n = M.length;
64+
UnionFind uf = new UnionFind(n);
65+
for( int i = 0; i < M.length - 1; i++ ) {
66+
for( int j = i + 1; j < M.length; j++ ) {
67+
if( M[i][j] == 1 )
68+
uf.union(i, j);
69+
}
70+
}
71+
return uf.count;
72+
}
73+
}
74+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* 70. Climbing Stairs
3+
*
4+
* 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
5+
6+
每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
7+
8+
注意:给定 n 是一个正整数。
9+
10+
示例 1:
11+
12+
输入: 2
13+
输出: 2
14+
解释: 有两种方法可以爬到楼顶。
15+
1. 1 阶 + 1 阶
16+
2. 2 阶
17+
示例 2:
18+
19+
输入: 3
20+
输出: 3
21+
解释: 有三种方法可以爬到楼顶。
22+
1. 1 阶 + 1 阶 + 1 阶
23+
2. 1 阶 + 2 阶
24+
3. 2 阶 + 1 阶
25+
26+
*/
27+
public class LeetCode_70_569 {
28+
29+
public static void main(String[] args) {
30+
// TODO Auto-generated method stub
31+
int n = 3;
32+
int result = new LeetCode_70_569().new Solution().climbStairs(n);
33+
System.out.println(result);
34+
}
35+
36+
class Solution {
37+
public int climbStairs(int n) {
38+
if( n <= 2)
39+
return n;
40+
int prepre = 1;
41+
int pre = 2;
42+
int result = 0;
43+
for( int i = 2; i < n; i++ ) {
44+
result = prepre + pre;
45+
prepre = pre;
46+
pre = result;
47+
}
48+
return result;
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)