Skip to content

Commit cbb1b6d

Browse files
committed
Added 1 solution & modified 4 solutions
1 parent 53e20c6 commit cbb1b6d

5 files changed

+116
-98
lines changed

Easy/Robot Return to Origin.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
class Solution {
2-
Map<Character, int[]> map;
32
public boolean judgeCircle(String moves) {
4-
map = new HashMap<>();
5-
map.put('U', new int[]{-1, 0});
6-
map.put('D', new int[]{1, 0});
7-
map.put('L', new int[]{0, -1});
8-
map.put('R', new int[]{0, 1});
93
int x = 0;
104
int y = 0;
11-
for (char c : moves.toCharArray()) {
12-
int[] dir = map.get(c);
13-
x += dir[0];
14-
y += dir[1];
5+
for (char move : moves.toCharArray()) {
6+
if (move == 'U' || move == 'D') {
7+
y += move == 'U' ? 1 : -1;
8+
}
9+
else {
10+
x += move == 'L' ? -1 : 1;
11+
}
1512
}
1613
return x == 0 && y == 0;
1714
}
Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,53 @@
11
/**
2-
* Definition for binary tree
2+
* Definition for a binary tree node.
33
* public class TreeNode {
44
* int val;
55
* TreeNode left;
66
* TreeNode right;
7-
* TreeNode(int x) { val = x; }
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
814
* }
915
*/
10-
11-
public class BSTIterator {
12-
Stack<TreeNode> stack;
13-
public BSTIterator(TreeNode root) {
14-
stack = new Stack<>();
15-
pushLeft(root);
16+
class BSTIterator {
17+
Stack<TreeNode> stack;
18+
public BSTIterator(TreeNode root) {
19+
stack = new Stack<>();
20+
update(root);
21+
}
22+
23+
private void update(TreeNode node) {
24+
if (node == null) {
25+
return;
1626
}
17-
18-
private void pushLeft(TreeNode root) {
19-
while(root != null) {
20-
stack.push(root);
21-
root = root.left;
22-
}
27+
stack.add(node);
28+
TreeNode leftNode = node.left;
29+
while (leftNode != null) {
30+
stack.add(leftNode);
31+
leftNode = leftNode.left;
2332
}
33+
}
2434

25-
/** @return whether we have a next smallest number */
26-
public boolean hasNext() {
27-
return !stack.isEmpty();
28-
}
35+
/** @return the next smallest number */
36+
public int next() {
37+
TreeNode node = stack.pop();
38+
update(node.right);
39+
return node.val;
40+
}
2941

30-
/** @return the next smallest number */
31-
public int next() {
32-
TreeNode node = stack.pop();
33-
pushLeft(node.right);
34-
35-
return node.val;
36-
}
42+
/** @return whether we have a next smallest number */
43+
public boolean hasNext() {
44+
return !stack.isEmpty();
45+
}
3746
}
3847

3948
/**
40-
* Your BSTIterator will be called like this:
41-
* BSTIterator i = new BSTIterator(root);
42-
* while (i.hasNext()) v[f()] = i.next();
49+
* Your BSTIterator object will be instantiated and called as such:
50+
* BSTIterator obj = new BSTIterator(root);
51+
* int param_1 = obj.next();
52+
* boolean param_2 = obj.hasNext();
4353
*/

Medium/Divide Two Integers.java

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,32 @@
11
class Solution {
2-
public int divide(int dividend, int divisor) {
3-
4-
boolean isNeg = false;
5-
if ((dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0)) {
6-
isNeg = true;
7-
}
8-
9-
int ans = 0;
10-
11-
long Ldividend = Math.abs((long) dividend);
12-
long Ldivisor = Math.abs((long) divisor);
13-
14-
if (Ldivisor == 0) return Integer.MAX_VALUE;
15-
if (Ldividend == 0 || (Ldividend < Ldivisor)) return 0;
16-
17-
long quot = ldivide(Ldividend, Ldivisor);
18-
19-
if(quot > Integer.MAX_VALUE) {
20-
ans = isNeg == false ? Integer.MAX_VALUE : Integer.MIN_VALUE;
21-
}
22-
else {
23-
ans = (int)(isNeg ? -quot : quot);
24-
}
25-
26-
return ans;
2+
private static int HALF_INT_MIN = -1073741824;
3+
public int divide(int dividend, int divisor) {
4+
if (dividend == Integer.MIN_VALUE && divisor == -1) {
5+
return Integer.MAX_VALUE;
276
}
28-
29-
private long ldivide(long ldividend, long ldivisor) {
30-
if (ldividend < ldivisor) return 0;
31-
32-
long sum = ldivisor;
33-
long multiple = 1;
34-
while ((sum+sum) <= ldividend) {
35-
sum += sum;
36-
multiple += multiple;
37-
}
38-
39-
return multiple + ldivide(ldividend - sum, ldivisor);
7+
int negatives = 2;
8+
if (dividend > 0) {
9+
negatives--;
10+
dividend = -dividend;
4011
}
12+
if (divisor > 0) {
13+
negatives--;
14+
divisor = -divisor;
15+
}
16+
int quotient = 0;
17+
while (divisor >= dividend) {
18+
int powerOfTwo = -1;
19+
int value = divisor;
20+
while (value >= HALF_INT_MIN && value + value >= dividend) {
21+
value += value;
22+
powerOfTwo += powerOfTwo;
23+
}
24+
quotient += powerOfTwo;
25+
dividend -= value;
26+
}
27+
if (negatives != 1) {
28+
return -quotient;
29+
}
30+
return quotient;
31+
}
4132
}

Medium/Is Graph Bipartite.java

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
11
class Solution {
2-
public boolean isBipartite(int[][] graph) {
3-
int[] colors = new int[graph.length];
4-
Arrays.fill(colors, -1);
2+
public boolean isBipartite(int[][] graph) {
3+
int n = graph.length;
4+
int[] color = new int[n];
5+
Arrays.fill(color, -1);
6+
for (int i = 0; i < n; i++) {
7+
if (color[i] == -1) {
58
Stack<Integer> stack = new Stack<>();
6-
7-
for (int i = 0; i < graph.length; i++) {
8-
if (colors[i] == -1) {
9-
stack.push(i);
10-
colors[i] = 0;
11-
12-
while (!stack.isEmpty()) {
13-
Integer removed = stack.pop();
14-
for (Integer connect : graph[removed]) {
15-
if (colors[connect] == -1) {
16-
stack.push(connect);
17-
colors[connect] = colors[removed] == 1 ? 0 : 1;
18-
}
19-
else if (colors[connect] == colors[removed]) {
20-
return false;
21-
}
22-
}
23-
}
9+
stack.push(i);
10+
color[i] = 0;
11+
while (!stack.isEmpty()) {
12+
Integer node = stack.pop();
13+
for (Integer neighbor : graph[node]) {
14+
if (color[neighbor] == -1) {
15+
stack.push(neighbor);
16+
color[neighbor] = color[node] ^ 1;
2417
}
18+
else if (color[neighbor] == color[node]) {
19+
return false;
20+
}
21+
}
2522
}
26-
27-
return true;
23+
}
2824
}
25+
return true;
26+
}
2927
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int removeStones(int[][] stones) {
3+
Set<int[]> visited = new HashSet<>();
4+
int numOfIslands = 0;
5+
for (int[] stone : stones) {
6+
if (!visited.contains(stone)) {
7+
dfs(stones, visited, stone);
8+
numOfIslands++;
9+
}
10+
}
11+
return stones.length - numOfIslands;
12+
}
13+
14+
private void dfs(int[][] stones, Set<int[]> visited, int[] stone) {
15+
visited.add(stone);
16+
for (int[] st : stones) {
17+
if ((st[0] == stone[0] || st[1] == stone[1]) && !visited.contains(st)) {
18+
dfs(stones, visited, st);
19+
}
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)