Skip to content

Commit 4cc53bc

Browse files
committed
update
1 parent 12c63f5 commit 4cc53bc

File tree

19 files changed

+417
-21
lines changed

19 files changed

+417
-21
lines changed

README.md

Lines changed: 13 additions & 13 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+
18+
record Pair(TreeNode node, int level) { };
19+
20+
public int maxDepth(TreeNode root) {
21+
int res = 0;
22+
if (root == null) {
23+
return 0;
24+
}
25+
ArrayDeque<Pair> queue = new ArrayDeque<>();
26+
queue.offer(new Pair(root, 1));
27+
while (!queue.isEmpty()) {
28+
Pair pair = queue.poll();
29+
TreeNode node = pair.node;
30+
int level = pair.level;
31+
res = Math.max(res, level);
32+
for (TreeNode child: new TreeNode[]{node.left, node.right}) {
33+
if (child != null) {
34+
queue.offer(new Pair(child, level + 1));
35+
}
36+
}
37+
}
38+
return res;
39+
}
40+
}
41+
42+
// time O(n), due to bfs
43+
// space O(n), due to queue's size, it can be n/2 in a balanced tree's deepest level
44+
// using tree and bfs
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
18+
int res;
19+
20+
public int diameterOfBinaryTree(TreeNode root) {
21+
res = 0;
22+
dfs(root);
23+
return res;
24+
}
25+
26+
public int dfs(TreeNode node) {
27+
if (node == null) {
28+
return 0;
29+
}
30+
int leftL = dfs(node.left);
31+
int rightL = dfs(node.right);
32+
int nodeAsPeakL = leftL + rightL;
33+
res = Math.max(res, nodeAsPeakL);
34+
return Math.max(leftL + 1, rightL + 1);
35+
}
36+
}
37+
38+
// time O(n), due to traverse
39+
// space O(n), due to tree height for skewed tree
40+
// using tree and dfs (postorder, recursive)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
public ListNode reverseList(ListNode head) {
13+
if (head == null || head.next == null) {
14+
return head;
15+
}
16+
ListNode prev = null;
17+
ListNode cur = head;
18+
while (cur != null) {
19+
ListNode nxt = cur.next;
20+
cur.next = prev;
21+
prev = cur;
22+
cur = nxt;
23+
}
24+
return prev;
25+
}
26+
}
27+
28+
// time O(n)
29+
// space O(1)
30+
// using linked list and two pointers (prev and cur)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
public ListNode middleNode(ListNode head) {
13+
ListNode slow = head;
14+
ListNode fast = head;
15+
while (fast != null && fast.next != null) {
16+
slow = slow.next;
17+
fast = fast.next.next;
18+
}
19+
return slow;
20+
}
21+
}
22+
23+
// time O(n), due to traverse
24+
// space O(1)
25+
// using linked list and two pointers (slow and fast)
26+
/*
27+
1. If there are two middle nodes, return the second middle node: use 'while fast and fast.next'
28+
2. If there are two middle nodes, return the first middle node: use 'while fast.next and fast.next.next'
29+
*/
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public int romanToInt(String s) {
3+
HashMap<Character, Integer> charToWeight = new HashMap<>();
4+
charToWeight.put('I', 1);
5+
charToWeight.put('V', 5);
6+
charToWeight.put('X', 10);
7+
charToWeight.put('L', 50);
8+
charToWeight.put('C', 100);
9+
charToWeight.put('D', 500);
10+
charToWeight.put('M', 1000);
11+
int res = 0;
12+
for (int i = 0; i < s.length(); i++) {
13+
Character c = s.charAt(i);
14+
int w = charToWeight.get(c);
15+
if (i + 1 < s.length() && charToWeight.get(s.charAt(i + 1)) > w) {
16+
res -= w;
17+
} else {
18+
res += w;
19+
}
20+
}
21+
return res;
22+
}
23+
}
24+
25+
// time O(n), due to traverse, n is the length of input string
26+
// space O(1), hashmap's size is O(7)
27+
// using hashmap and store val
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public boolean containsDuplicate(int[] nums) {
3+
HashSet<Integer> set = new HashSet<>();
4+
for (int num: nums) {
5+
if (set.contains(num)) {
6+
return true;
7+
}
8+
set.add(num);
9+
}
10+
return false;
11+
}
12+
}
13+
14+
// time O(n)
15+
// space O(n)
16+
// using hashmap and store val and hashset

[C]hashmap/[C]hashmap/242-valid-anagram.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public boolean isAnagram(String s, String t) {
1212
tCharToFreq.compute(tc, (k, v) -> v == null ? 1 : v + 1);
1313
}
1414
for (Character c: sCharToFreq.keySet()) {
15-
if (!sCharToFreq.get(c).equals(tCharToFreq.get(c))) {
15+
if (!Objects.equals(sCharToFreq.get(c), tCharToFreq.get(c))) {
1616
return false;
1717
}
1818
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public boolean canConstruct(String ransomNote, String magazine) {
3+
HashMap<Character, Integer> rCharToFreq = new HashMap<>();
4+
HashMap<Character, Integer> mCharToFreq = new HashMap<>();
5+
for (int i = 0; i < ransomNote.length(); i++) {
6+
Character c = ransomNote.charAt(i);
7+
rCharToFreq.compute(c, (k, v) -> v == null ? 1 : v + 1);
8+
}
9+
for (int i = 0; i < magazine.length(); i++) {
10+
Character c = magazine.charAt(i);
11+
mCharToFreq.compute(c, (k, v) -> v == null ? 1 : v + 1);
12+
}
13+
for (Character c: rCharToFreq.keySet()) {
14+
if (mCharToFreq.containsKey(c) && rCharToFreq.get(c) <= mCharToFreq.get(c)) {
15+
continue;
16+
}
17+
return false;
18+
}
19+
return true;
20+
}
21+
}
22+
23+
// time O(n+m)
24+
// space O(26) == O(1), constant
25+
// using hashmap and store sth’s freq
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public int longestPalindrome(String s) {
3+
HashMap<Character, Integer> charToFreq = new HashMap<>();
4+
for (int i = 0; i < s.length(); i++) {
5+
Character c = s.charAt(i);
6+
charToFreq.compute(c, (k, v) -> v == null ? 1 : v + 1);
7+
}
8+
9+
int res = 0;
10+
for (Map.Entry<Character, Integer> kv: charToFreq.entrySet()) {
11+
Character k = kv.getKey();
12+
Integer v = kv.getValue();
13+
if (res % 2 == 0) {
14+
res += v;
15+
} else {
16+
if (v % 2 == 1) {
17+
v -= 1;
18+
}
19+
res += v;
20+
}
21+
}
22+
return res;
23+
}
24+
}
25+
26+
// time O(n)
27+
// space O(1)
28+
// using hashmap and store sth’s freq

[C]hashmap/[C]hashmap/409-longest-palindrome.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ def longestPalindrome(self, s: str) -> int:
1414
return res
1515

1616
# time O(n)
17-
# space O(n)
17+
# space O(1)
1818
# using hashmap and store sth’s freq
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Solution {
2+
public boolean backspaceCompare(String s, String t) {
3+
int i = s.length() - 1;
4+
int j = t.length() - 1;
5+
int iBack = 0;
6+
int jBack = 0;
7+
while (i >= 0 || j >= 0) {
8+
Character iChar = null;
9+
Character jChar = null;
10+
while (i >= 0) {
11+
if (s.charAt(i) == '#') {
12+
iBack += 1;
13+
i -= 1;
14+
}
15+
else if (iBack > 0) {
16+
iBack -= 1;
17+
i -= 1;
18+
} else {
19+
iChar = s.charAt(i);
20+
i -= 1;
21+
break;
22+
}
23+
}
24+
while (j >= 0) {
25+
if (t.charAt(j) == '#') {
26+
jBack += 1;
27+
j -= 1;
28+
}
29+
else if (jBack > 0) {
30+
jBack -= 1;
31+
j -= 1;
32+
} else {
33+
jChar = t.charAt(j);
34+
j -= 1;
35+
break;
36+
}
37+
}
38+
if (!Objects.equals(iChar, jChar)) {
39+
return false;
40+
}
41+
}
42+
return true;
43+
}
44+
}
45+
46+
// time O(n+m)
47+
// space O(1)
48+
// using string and traverse from end and two pointers
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public boolean canAttendMeetings(int[][] intervals) {
3+
if (intervals.length == 0) {
4+
return true;
5+
}
6+
Arrays.sort(intervals, (a, b) -> a[0] == b[0] ? Integer.compare(a[1], b[1]) : Integer.compare(a[0], b[0]));
7+
int prevE = intervals[0][1];
8+
for (int i = 1; i < intervals.length; i++) {
9+
int curS = intervals[i][0];
10+
int curE = intervals[i][1];
11+
if (prevE > curS) {
12+
return false;
13+
}
14+
prevE = Math.max(prevE, curE);
15+
}
16+
return true;
17+
}
18+
}
19+
20+
// time O(nlogn)
21+
// space O(1), or consider sort's cost
22+
// using array and line sweep and compare two intervals each round

[H]array/[H]array-two-pointers-opposite-direction/125-valid-palindrome.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ public boolean isPalindrome(String s) {
33
int left = 0;
44
int right = s.length() - 1;
55
while (left < right) {
6-
Character lc = s.charAt(left);
7-
Character rc = s.charAt(right);
6+
char lc = s.charAt(left);
7+
char rc = s.charAt(right);
88
if (!Character.isLetterOrDigit(lc)) {
99
left += 1;
1010
} else if (!Character.isLetterOrDigit(rc)) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public int majorityElement(int[] nums) {
3+
Integer cand = null;
4+
int vote = 0;
5+
for (int i = 0; i < nums.length; i++) {
6+
if (cand != null && cand == nums[i]) {
7+
vote += 1;
8+
} else if (cand == null) {
9+
cand = nums[i];
10+
vote = 1;
11+
} else {
12+
vote -= 1;
13+
if (vote == 0) {
14+
cand = null;
15+
}
16+
}
17+
}
18+
return cand;
19+
}
20+
}
21+
22+
// time O(n)
23+
// space O(1)
24+
// using array and boyer moore vote algorithm

[I]stack-queue/[I]stack-queue/20-valid-parentheses.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public boolean isValid(String s) {
77
ArrayDeque<Character> stack = new ArrayDeque<>();
88
for (Character c: s.toCharArray()) {
99
if (closeOpen.containsKey(c)) {
10-
if (!closeOpen.get(c).equals(stack.peek())) {
10+
if (!Objects.equals(stack.peek(), closeOpen.get(c))) {
1111
return false;
1212
}
1313
stack.pop();

0 commit comments

Comments
 (0)