Skip to content

Commit 3880986

Browse files
committed
update java solution
1 parent d0a6ecb commit 3880986

File tree

7 files changed

+204
-10
lines changed

7 files changed

+204
-10
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Large diffs are not rendered by default.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
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+
* }
14+
* }
15+
*/
16+
class Solution {
17+
public List<List<Integer>> levelOrder(TreeNode root) {
18+
List<List<Integer>> res = new ArrayList<>();
19+
if (root == null) {
20+
return res;
21+
}
22+
ArrayDeque<TreeNode> queue = new ArrayDeque<>();
23+
queue.offer(root);
24+
while (!queue.isEmpty()) {
25+
ArrayList<Integer> cur = new ArrayList<>();
26+
int levelCount = queue.size();
27+
for (int i = 0; i < levelCount; i++) {
28+
TreeNode node = queue.poll();
29+
cur.add(node.val);
30+
for (TreeNode child: new TreeNode[]{node.left, node.right}) {
31+
if (child != null) {
32+
queue.offer(child);
33+
}
34+
}
35+
}
36+
res.add(cur);
37+
}
38+
return res;
39+
}
40+
}
41+
42+
// time O(n), due to traversal
43+
// space O(n), queue can have a size of n/2 or the list's size to store nodes
44+
// using tree and bfs
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public int lengthOfLongestSubstring(String s) {
3+
HashMap<Character, Integer> charToFreq = new HashMap<>();
4+
int res = 0;
5+
int left = 0;
6+
for (int right = 0; right < s.length(); right++) {
7+
Character c = s.charAt(right);
8+
charToFreq.compute(c, (k, v) -> v == null ? 1 : v + 1);
9+
while (charToFreq.get(c) > 1) {
10+
Character removeC = s.charAt(left);
11+
charToFreq.put(removeC, charToFreq.get(removeC) - 1);
12+
if (Objects.equals(charToFreq.get(removeC), 0)) {
13+
charToFreq.remove(removeC);
14+
}
15+
left += 1;
16+
}
17+
res = Math.max(res, right - left + 1);
18+
}
19+
return res;
20+
}
21+
}
22+
23+
// time O(n)
24+
// space O(1), due to the hashmap could contain all unique chars
25+
// using array and standard sliding window and hashmap
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class Solution {
2+
public List<List<Integer>> threeSum(int[] nums) {
3+
Arrays.sort(nums);
4+
List<List<Integer>> res = new ArrayList<>();
5+
for (int i = 0; i < nums.length - 2; i++) {
6+
if (i - 1 >= 0 && nums[i - 1] == nums[i]) {
7+
continue;
8+
}
9+
int j = i + 1;
10+
int k = nums.length - 1;
11+
int target = 0 - nums[i];
12+
if (nums[j] + nums[j + 1] > target) {
13+
break;
14+
}
15+
if (nums[k - 1] + nums[k] < target) {
16+
continue;
17+
}
18+
while (j < k) {
19+
int total = nums[j] + nums[k];
20+
if (total == target) {
21+
res.add(new ArrayList<>(Arrays.asList(nums[i], nums[j], nums[k])));
22+
j += 1;
23+
k -= 1;
24+
} else if (total > target) {
25+
k -= 1;
26+
} else {
27+
j += 1;
28+
}
29+
while (j < k && j - 1 > i && nums[j - 1] == nums[j]) {
30+
j += 1;
31+
}
32+
while (j < k && k + 1 < nums.length && nums[k] == nums[k + 1]) {
33+
k -= 1;
34+
}
35+
}
36+
}
37+
return res;
38+
}
39+
}
40+
41+
// time O(n**2)
42+
// space O(1), or due to built in sort's cost
43+
// using array and two pointers opposite direction and shrink type and sort
44+
/*
45+
1. be aware of handling duplicate
46+
*/
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public int evalRPN(String[] tokens) {
3+
ArrayDeque<Integer> stack = new ArrayDeque<>();
4+
for (String token: tokens) {
5+
if ("+-*/".contains(token)) {
6+
Integer second = stack.pop();
7+
Integer first = stack.pop();
8+
if (Objects.equals(token, "+")){
9+
stack.push(first + second);
10+
} else if (Objects.equals(token, "-")) {
11+
stack.push(first - second);
12+
} else if (Objects.equals(token, "*")) {
13+
stack.push(first * second);
14+
} else {
15+
stack.push(first / second);
16+
}
17+
} else {
18+
stack.push(Integer.parseInt(token));
19+
}
20+
}
21+
return stack.peek();
22+
}
23+
}
24+
25+
// time O(n), due to traverse
26+
// space O(n), due to stack
27+
// using stack and queue and use stack to store the last states
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
// Definition for a Node.
3+
class Node {
4+
public int val;
5+
public List<Node> neighbors;
6+
public Node() {
7+
val = 0;
8+
neighbors = new ArrayList<Node>();
9+
}
10+
public Node(int _val) {
11+
val = _val;
12+
neighbors = new ArrayList<Node>();
13+
}
14+
public Node(int _val, ArrayList<Node> _neighbors) {
15+
val = _val;
16+
neighbors = _neighbors;
17+
}
18+
}
19+
*/
20+
21+
class Solution {
22+
public Node cloneGraph(Node node) {
23+
HashMap<Node, Node> oldToNew = new HashMap<>();
24+
if (node == null) {
25+
return node;
26+
}
27+
oldToNew.put(node, new Node(node.val));
28+
ArrayDeque<Node> queue = new ArrayDeque<>();
29+
queue.offer(node);
30+
while (!queue.isEmpty()) {
31+
Node oldNode = queue.poll();
32+
Node newNode = oldToNew.get(oldNode);
33+
for (Node neighbor: oldNode.neighbors) {
34+
if (!oldToNew.containsKey(neighbor)) {
35+
oldToNew.put(neighbor, new Node(neighbor.val));
36+
queue.offer(neighbor);
37+
}
38+
Node newNeighbor = oldToNew.get(neighbor);
39+
newNode.neighbors.add(newNeighbor);
40+
}
41+
}
42+
return oldToNew.get(node);
43+
}
44+
}
45+
46+
// time O(n)
47+
// space O(n)
48+
// using graph and bfs with single source and hashmap

[Q]basic/basic.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2078,12 +2078,17 @@ public class Main {
20782078
System.out.println(s5); // Hello World!!
20792079
System.out.println("-----");
20802080

2081-
// String from int O(logn) (base 10) or O(1)
2081+
// String from int in O(logn) (base 10) or O(1)
20822082
int i = 1001;
20832083
String iString = i + "";
20842084
System.out.println(iString); // 1001
20852085
System.out.println("-----");
20862086

2087+
// String to int in O(n)
2088+
int intFromString = Integer.parseInt(iString);
2089+
System.out.println(intFromString); // 1001
2090+
System.out.println("-----");
2091+
20872092
// char related methods
20882093
char c1 = 'a';
20892094
char c2 = 'A';
@@ -2293,6 +2298,7 @@ public class Main {
22932298
```Java
22942299
package NumberSyntax;
22952300

2301+
import java.util.Objects;
22962302
import java.util.Random;
22972303

22982304
public class Main {
@@ -2393,16 +2399,14 @@ public class Main {
23932399
Character.compare(x, y)
23942400
23952401
03. int x, Integer y. use:
2396-
x == y (notice: not null-safe)
2397-
x != y (notice: not null-safe)
2402+
Objects.equals(x, y)
23982403
x > y (notice: not null-safe)
23992404
x < y (notice: not null-safe)
24002405
x >= y (notice: not null-safe)
24012406
x <= y (notice: not null-safe)
24022407
24032408
04. char x, Character y. use:
2404-
x == y (notice: not null-safe)
2405-
x != y (notice: not null-safe)
2409+
Objects.equals(x, y)
24062410
x > y (notice: not null-safe)
24072411
x < y (notice: not null-safe)
24082412
x >= y (notice: not null-safe)

0 commit comments

Comments
 (0)