Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5408c98

Browse files
Checkin code
1 parent 7519c3e commit 5408c98

14 files changed

+733
-0
lines changed

‎src/conclusion/DecodeString.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package conclusion;
2+
3+
import java.util.Stack;
4+
5+
public class DecodeString {
6+
public String decodeString(String s) {
7+
StringBuilder res = new StringBuilder();
8+
Stack<Integer> countStack = new Stack<>();
9+
Stack<String> resStack = new Stack<>();
10+
int idx = 0;
11+
while (idx < s.length()) {
12+
if (Character.isDigit(s.charAt(idx))) {
13+
int count = 0;
14+
while (Character.isDigit(s.charAt(idx))) {
15+
count = 10 * count + (s.charAt(idx) - '0');
16+
idx++;
17+
}
18+
countStack.push(count);
19+
} else if (s.charAt(idx) == '[') {
20+
resStack.push(res.toString());
21+
res.delete(0, res.length());
22+
idx++;
23+
} else if (s.charAt(idx) == ']') {
24+
StringBuilder temp = new StringBuilder(resStack.pop());
25+
int repeatTimes = countStack.pop();
26+
for (int i = 0; i < repeatTimes; i++) {
27+
temp.append(res);
28+
}
29+
res.delete(0, res.length());
30+
res.append(temp);
31+
idx++;
32+
} else {
33+
res.append(s.charAt(idx++));
34+
}
35+
}
36+
return res.toString();
37+
}
38+
}

‎src/conclusion/FloodFill.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package conclusion;
2+
3+
public class FloodFill {
4+
5+
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
6+
int color = image[sr][sc];
7+
if (color != newColor)
8+
dfs(image, sr, sc, color, newColor);
9+
return image;
10+
}
11+
12+
public void dfs(int[][] image, int r, int c, int color, int newColor) {
13+
if (image[r][c] == color) {
14+
image[r][c] = newColor;
15+
if (r >= 1)
16+
dfs(image, r - 1, c, color, newColor);
17+
if (c >= 1)
18+
dfs(image, r, c - 1, color, newColor);
19+
if (r + 1 < image.length)
20+
dfs(image, r + 1, c, color, newColor);
21+
if (c + 1 < image[0].length)
22+
dfs(image, r, c + 1, color, newColor);
23+
}
24+
}
25+
}

‎src/conclusion/KeysAndRooms.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package conclusion;
2+
3+
import java.util.HashSet;
4+
import java.util.List;
5+
import java.util.Stack;
6+
7+
public class KeysAndRooms {
8+
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
9+
int n = rooms.size();
10+
if (n <= 1)
11+
return true;
12+
HashSet<Integer> set = new HashSet<Integer>();
13+
Stack<Integer> st = new Stack<Integer>();
14+
st.push(0);
15+
set.add(0);
16+
while (!st.isEmpty()) {
17+
for (int key : rooms.get(st.pop())) {
18+
if (!set.contains(key)) {
19+
set.add(key);
20+
st.push(key);
21+
}
22+
}
23+
}
24+
25+
return set.size() == n;
26+
}
27+
}

‎src/conclusion/Matrix01.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package conclusion;
2+
3+
public class Matrix01 {
4+
5+
public int[][] updateMatrix(int[][] matrix) {
6+
if (matrix.length == 0 || matrix[0].length == 0) {
7+
return matrix;
8+
}
9+
int range = matrix.length * matrix[0].length;
10+
11+
for (int i = 0; i < matrix.length; i++) {
12+
for (int j = 0; j < matrix[0].length; j++) {
13+
if (matrix[i][j] != 0) {
14+
int upCell = (i > 0) ? matrix[i - 1][j] : range;
15+
int leftCell = (j > 0) ? matrix[i][j - 1] : range;
16+
matrix[i][j] = Math.min(upCell, leftCell) + 1;
17+
}
18+
}
19+
}
20+
21+
for (int i = matrix.length - 1; i >= 0; i--) {
22+
for (int j = matrix[0].length - 1; j >= 0; j--) {
23+
if (matrix[i][j] != 0) {
24+
int downCell = (i < matrix.length - 1) ? matrix[i + 1][j] : range;
25+
int rightCell = (j < matrix[0].length - 1) ? matrix[i][j + 1] : range;
26+
matrix[i][j] = Math.min(Math.min(downCell, rightCell) + 1, matrix[i][j]);
27+
}
28+
}
29+
}
30+
31+
return matrix;
32+
}
33+
34+
}

‎src/conclusion/QueueUsingStack.java

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package conclusion;
2+
3+
import java.util.Stack;
4+
5+
public class QueueUsingStack {
6+
public class usingStackGet {
7+
Stack<Integer> st;
8+
int first, size;
9+
10+
/** Initialize your data structure here. */
11+
public usingStackGet() {
12+
st = new Stack<Integer>();
13+
first = 0;
14+
size = 0;
15+
}
16+
17+
/** Push element x to the back of queue. */
18+
public void push(int x) {
19+
st.push(x);
20+
size++;
21+
}
22+
23+
/** Removes the element from in front of queue and returns that element. */
24+
public int pop() {
25+
size--;
26+
int res = st.get(first);
27+
first++;
28+
return res;
29+
}
30+
31+
/** Get the front element. */
32+
public int peek() {
33+
return st.get(first);
34+
}
35+
36+
/** Returns whether the queue is empty. */
37+
public boolean empty() {
38+
return size == 0;
39+
}
40+
}
41+
42+
public class MyQueue {
43+
44+
Stack<Integer> s1, s2;
45+
int first;
46+
47+
/** Initialize your data structure here. */
48+
public MyQueue() {
49+
s1 = new Stack<Integer>();
50+
s2 = new Stack<Integer>();
51+
}
52+
53+
/** Push element x to the back of queue. */
54+
public void push(int x) {
55+
if (s1.isEmpty())
56+
first = x;
57+
s1.push(x);
58+
}
59+
60+
/** Removes the element from in front of queue and returns that element. */
61+
public int pop() {
62+
if (s2.isEmpty()) {
63+
while (!s1.isEmpty())
64+
s2.push(s1.pop());
65+
}
66+
67+
return s2.pop();
68+
}
69+
70+
/** Get the front element. */
71+
public int peek() {
72+
if (s2.isEmpty())
73+
return first;
74+
return s2.peek();
75+
}
76+
77+
/** Returns whether the queue is empty. */
78+
public boolean empty() {
79+
return (s1.isEmpty() && s2.isEmpty());
80+
}
81+
}
82+
}
83+
84+
/**
85+
* Your MyQueue object will be instantiated and called as such: MyQueue obj =
86+
* new MyQueue(); obj.push(x); int param_2 = obj.pop(); int param_3 =
87+
* obj.peek(); boolean param_4 = obj.empty();
88+
*/

‎src/conclusion/StackUsingQueues.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package conclusion;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
public class StackUsingQueues {
7+
Queue<Integer> q1;
8+
int size;
9+
10+
/** Initialize your data structure here. */
11+
public StackUsingQueues() {
12+
q1 = new LinkedList<Integer>();
13+
}
14+
15+
/** Push element x onto stack. */
16+
public void push(int x) {
17+
q1.add(x);
18+
size = q1.size();
19+
while(size-- > 1) {
20+
q1.add(q1.poll());
21+
}
22+
}
23+
24+
/** Removes the element on top of the stack and returns that element. */
25+
public int pop() {
26+
return q1.poll();
27+
}
28+
29+
/** Get the top element. */
30+
public int top() {
31+
return q1.peek();
32+
}
33+
34+
/** Returns whether the stack is empty. */
35+
public boolean empty() {
36+
return (q1.isEmpty());
37+
}
38+
}
39+
40+
/**
41+
* Your MyStack object will be instantiated and called as such: MyStack obj =
42+
* new MyStack(); obj.push(x); int param_2 = obj.pop(); int param_3 = obj.top();
43+
* boolean param_4 = obj.empty();
44+
*/

‎src/queue/MyCircularQueue.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package queue;
2+
3+
public class MyCircularQueue {
4+
5+
int[] q;
6+
int head = 0, tail = -1, n = 0;
7+
/** Initialize your data structure here. Set the size of the queue to be k. */
8+
public MyCircularQueue(int k) {
9+
q = new int[k];
10+
}
11+
12+
/** Insert an element into the circular queue. Return true if the operation is successful. */
13+
public boolean enQueue(int value) {
14+
if (isFull())
15+
return false;
16+
17+
if (tail == q.length - 1)
18+
tail = -1;
19+
20+
q[++tail] = value;
21+
n++;
22+
return true;
23+
}
24+
25+
/** Delete an element from the circular queue. Return true if the operation is successful. */
26+
public boolean deQueue() {
27+
if (isEmpty())
28+
return false;
29+
30+
if (head == q.length - 1) {
31+
head = 0;
32+
n--;
33+
return true;
34+
}
35+
36+
head++; n--;
37+
return true;
38+
}
39+
40+
/** Get the front item from the queue. */
41+
public int Front() {
42+
if (isEmpty())
43+
return -1;
44+
return q[head];
45+
}
46+
47+
/** Get the last item from the queue. */
48+
public int Rear() {
49+
if (isEmpty())
50+
return -1;
51+
return q[tail];
52+
}
53+
54+
/** Checks whether the circular queue is empty or not. */
55+
public boolean isEmpty() {
56+
return n <= 0;
57+
}
58+
59+
/** Checks whether the circular queue is full or not. */
60+
public boolean isFull() {
61+
return n >= q.length;
62+
}
63+
}
64+
65+
/**
66+
* Your MyCircularQueue object will be instantiated and called as such:
67+
* MyCircularQueue obj = new MyCircularQueue(k);
68+
* boolean param_1 = obj.enQueue(value);
69+
* boolean param_2 = obj.deQueue();
70+
* int param_3 = obj.Front();
71+
* int param_4 = obj.Rear();
72+
* boolean param_5 = obj.isEmpty();
73+
* boolean param_6 = obj.isFull();
74+
*/

‎src/queue/NumberOfIslands.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package queue;
2+
3+
public class NumberOfIslands {
4+
int n = 0, m = 0;
5+
6+
public int numIslands(char[][] grid) {
7+
n = grid.length;
8+
if (n == 0)
9+
return 0;
10+
m = grid[0].length;
11+
int c = 0;
12+
for (int i = 0; i < n; ++i) {
13+
for (int j = 0; j < m; ++j) {
14+
if (grid[i][j] == '1') {
15+
explore(grid, i, j);
16+
c++;
17+
}
18+
}
19+
}
20+
21+
return c;
22+
}
23+
24+
public void explore(char[][] grid, int i, int j) {
25+
if (i < 0 || j < 0 || i >= n || j >= m || grid[i][j] != '1')
26+
return;
27+
grid[i][j] = 0;
28+
explore(grid, i + 1, j);
29+
explore(grid, i - 1, j);
30+
explore(grid, i, j + 1);
31+
explore(grid, i, j - 1);
32+
}
33+
34+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /