Skip to content

Commit 08a1f4e

Browse files
committed
Added 1 solution & modified 1 solution
1 parent 7e08e87 commit 08a1f4e

File tree

3 files changed

+70
-28
lines changed

3 files changed

+70
-28
lines changed

Easy/Power of Two.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public boolean isPowerOfTwo(int n) {
3+
if (n % 2 != 0 && n != 1) {
4+
return false;
5+
}
6+
int start = 0;
7+
int end = n / 2;
8+
while (start <= end) {
9+
int mid = (start + end) / 2;
10+
int pow = (int) (Math.pow(2, mid));
11+
if (n == pow) {
12+
return true;
13+
}
14+
else if (n > pow) {
15+
start = mid + 1;
16+
}
17+
else {
18+
end = mid - 1;
19+
}
20+
}
21+
return false;
22+
}
23+
}

Easy/power_of_2.java

Lines changed: 0 additions & 28 deletions
This file was deleted.

Medium/Design Browser History.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class BrowserHistory {
2+
3+
Node root;
4+
public BrowserHistory(String homepage) {
5+
root = new Node(homepage);
6+
}
7+
8+
public void visit(String url) {
9+
Node node = new Node(url);
10+
root.next = null;
11+
root.next = node;
12+
node.prev = root;
13+
root = root.next;
14+
}
15+
16+
public String back(int steps) {
17+
while (steps-- > 0 && root.prev != null) {
18+
root = root.prev;
19+
}
20+
return root.val;
21+
}
22+
23+
public String forward(int steps) {
24+
while (steps-- > 0 && root.next != null) {
25+
root = root.next;
26+
}
27+
return root.val;
28+
}
29+
}
30+
31+
class Node {
32+
String val;
33+
Node next;
34+
Node prev;
35+
36+
public Node(String val) {
37+
this.val = val;
38+
}
39+
}
40+
41+
/**
42+
* Your BrowserHistory object will be instantiated and called as such:
43+
* BrowserHistory obj = new BrowserHistory(homepage);
44+
* obj.visit(url);
45+
* String param_2 = obj.back(steps);
46+
* String param_3 = obj.forward(steps);
47+
*/

0 commit comments

Comments
 (0)