Skip to content

Commit 764962d

Browse files
committed
Modified 4 solutions
1 parent 597c4ab commit 764962d

File tree

4 files changed

+51
-41
lines changed

4 files changed

+51
-41
lines changed

Medium/Bulb Switcher.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Solution {
2-
public int bulbSwitch(int n) {
3-
return (int)Math.sqrt(n);
4-
}
2+
public int bulbSwitch(int n) {
3+
return (int) Math.sqrt(n);
4+
}
55
}

Medium/Kth Smallest Element in a BST.java

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,28 @@
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
*/
1016
class Solution {
11-
public int kthSmallest(TreeNode root, int k) {
12-
Stack<TreeNode> stack = new Stack<>();
13-
14-
while (root != null || !stack.empty()) {
15-
while (root != null) {
16-
stack.push(root);
17-
root = root.left;
18-
}
19-
20-
root = stack.peek();
21-
stack.pop();
22-
23-
if (--k == 0) {
24-
return root.val;
25-
}
26-
27-
root = root.right;
28-
}
29-
30-
return -1;
17+
public int kthSmallest(TreeNode root, int k) {
18+
Stack<TreeNode> stack = new Stack<>();
19+
while (true) {
20+
while (root != null) {
21+
stack.push(root);
22+
root = root.left;
23+
}
24+
root = stack.pop();
25+
if (--k == 0) {
26+
return root.val;
27+
}
28+
root = root.right;
3129
}
30+
}
3231
}

Medium/Pow.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
class Solution {
2-
public double myPow(double x, int n) {
3-
return Math.pow(x,n);
2+
public double myPow(double x, int n) {
3+
long N = n;
4+
if (N < 0) {
5+
x = 1 / x;
6+
N = -N;
47
}
8+
double ans = 1;
9+
double currProd = x;
10+
for (long i = N; i > 0; i /= 2) {
11+
if ((i % 2) == 1) {
12+
ans = ans * currProd;
13+
}
14+
currProd = currProd * currProd;
15+
}
16+
return ans;
17+
}
518
}

Medium/Reconstruct Itinerary.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@ class Solution {
22
List<String> result = null;
33
public List<String> findItinerary(List<List<String>> tickets) {
44
Map<String, List<String>> map = new HashMap<>();
5-
Map<String, boolean[]> visitMap = new HashMap<>();
65
int flights = tickets.size();
76
for (List<String> ticket : tickets) {
87
map.computeIfAbsent(ticket.get(0), k -> new ArrayList<>()).add(ticket.get(1));
98
}
9+
Map<String, boolean[]> visited = new HashMap<>();
1010
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
1111
Collections.sort(entry.getValue());
12-
visitMap.put(entry.getKey(), new boolean[entry.getValue().size()]);
12+
visited.put(entry.getKey(), new boolean[entry.getValue().size()]);
1313
}
1414
LinkedList<String> route = new LinkedList<String>();
1515
route.add("JFK");
16-
backtrack(map, visitMap, flights, route, "JFK");
16+
backtrack(map, visited, flights, route, "JFK");
1717
return result;
1818
}
1919

2020
private boolean backtrack(
2121
Map<String, List<String>> map,
22-
Map<String, boolean[]> visitMap,
22+
Map<String, boolean[]> visited,
2323
int flights,
2424
LinkedList<String> route,
2525
String origin
@@ -31,20 +31,18 @@ private boolean backtrack(
3131
if (!map.containsKey(origin)) {
3232
return false;
3333
}
34-
int i = 0;
35-
boolean[] visited = visitMap.get(origin);
36-
for (String dest : map.get(origin)) {
37-
if (!visited[i]) {
38-
visited[i] = true;
39-
route.add(dest);
40-
boolean res = backtrack(map, visitMap, flights, route, dest);
34+
boolean[] visit = visited.get(origin);
35+
for (int i = 0; i < map.get(origin).size(); i++) {
36+
if (!visit[i]) {
37+
visit[i] = true;
38+
route.add(map.get(origin).get(i));
39+
boolean res = backtrack(map, visited, flights, route, map.get(origin).get(i));
4140
route.pollLast();
42-
visited[i] = false;
43-
if (res == true) {
44-
return true;
41+
visit[i] = false;
42+
if (res) {
43+
return res;
4544
}
4645
}
47-
i++;
4846
}
4947
return false;
5048
}

0 commit comments

Comments
 (0)