Skip to content
Closed

test #10

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
258 commits
Select commit Hold shift + click to select a range
d1c576e
add
yuchaozh Oct 29, 2018
25dbe14
121
yuchaozh Oct 30, 2018
6dd7c0a
70
yuchaozh Oct 30, 2018
c9258e1
highlight
yuchaozh Nov 5, 2018
84ad417
74
yuchaozh Nov 5, 2018
7c51b4d
658
yuchaozh Nov 5, 2018
1f378f8
155
yuchaozh Nov 6, 2018
2c241d4
35
yuchaozh Nov 6, 2018
96a1cae
50
yuchaozh Nov 7, 2018
1145eca
33
yuchaozh Nov 13, 2018
2a708d8
153
yuchaozh Nov 19, 2018
6b32dbe
Create 682. Baseball Game.md
yuchaozh Nov 19, 2018
f2c119d
Update 682. Baseball Game.md
yuchaozh Nov 19, 2018
9e37159
Create 496. Next Greater Element I.md
yuchaozh Nov 19, 2018
f76eff4
Create 232. Implement Queue using Stacks.md
yuchaozh Nov 20, 2018
0eb94d4
Create 225. Implement Stack using Queues.md
yuchaozh Nov 20, 2018
6aa2d32
Create 346. Moving Average from Data Stream.md
yuchaozh Nov 20, 2018
6772126
Create 876. Middle of the Linked List.md
yuchaozh Nov 20, 2018
958249d
Create 206. Reverse Linked List.md
yuchaozh Nov 20, 2018
f69de85
Create 237. Delete Node in a Linked List.md
yuchaozh Nov 21, 2018
59ffee6
Create 21. Merge Two Sorted Lists.md
yuchaozh Nov 21, 2018
10d18ea
Create 83. Remove Duplicates from Sorted List.md
yuchaozh Nov 21, 2018
2b6b20e
Create 203. Remove Linked List Elements.md
yuchaozh Nov 21, 2018
a1f78fd
Create 160. Intersection of Two Linked Lists.md
yuchaozh Nov 21, 2018
55a4ffa
Create 707. Design Linked List.md
yuchaozh Nov 21, 2018
d1463bc
Create 148. Sort List.md
yuchaozh Nov 21, 2018
4ca6199
Create 189. Rotate Array.md
yuchaozh Nov 21, 2018
1a6e5d9
Create 104. Maximum Depth of Binary Tree.md
yuchaozh Nov 22, 2018
ad932e9
Create 110. Balanced Binary Tree.md
yuchaozh Nov 27, 2018
edcfa2a
101
yuchaozh Nov 27, 2018
be13e97
Create 703. Kth Largest Element in a Stream.md
yuchaozh Nov 29, 2018
9fececf
102 and 215
yuchaozh Nov 30, 2018
018ca3c
599
yuchaozh Dec 10, 2018
bd1e258
Create 743. Network Delay Time.md
yuchaozh Dec 10, 2018
ae955af
Merge branch 'master' of https://github.com/yuchaozh/LeetCode_MarkDown
yuchaozh Dec 10, 2018
c494d66
872
yuchaozh Dec 11, 2018
1095f7a
add
yuchaozh Dec 12, 2018
d0dae21
257
yuchaozh Dec 13, 2018
e2e7e04
111
yuchaozh Dec 13, 2018
d7d9260
add
yuchaozh Jan 2, 2019
a2a2b58
3
yuchaozh Jan 3, 2019
a4428da
929
yuchaozh Jan 5, 2019
9588b0b
557
yuchaozh Jan 5, 2019
f58940d
add
yuchaozh Jan 11, 2019
386b66d
476
yuchaozh Jan 16, 2019
ddc3d29
103
yuchaozh Jan 23, 2019
6e84c9e
new problems
Feb 22, 2019
27eee6e
Merge branch 'master' of https://github.com/yuchaozh/LeetCode_MarkDown
Feb 22, 2019
26a5d2b
new problemsj
Feb 23, 2019
faa0516
two more
Feb 26, 2019
3b3e98e
daily temperature
yuchaozh Feb 26, 2019
e692b90
add
yuchaozh Feb 26, 2019
6476b1b
more
yuchaozh Feb 26, 2019
e9e4898
not finished yet
yuchaozh Feb 26, 2019
3bb2ba3
new
Feb 27, 2019
81be2c5
Merge branch 'master' of https://github.com/yuchaozh/LeetCode_MarkDown
Feb 27, 2019
21ed53d
two more
yuchaozh Feb 28, 2019
e3c2fd9
Merge branch 'master' of https://github.com/yuchaozh/LeetCode_MarkDown
yuchaozh Feb 28, 2019
e170d73
1
yuchaozh Feb 28, 2019
7993973
841
yuchaozh Feb 28, 2019
5e0c795
recursive
yuchaozh Feb 28, 2019
2c9f047
update
yuchaozh Mar 1, 2019
0c454ef
in-complete
yuchaozh Mar 1, 2019
2f63280
finish
yuchaozh Mar 1, 2019
3a68178
add more solution
Mar 1, 2019
3c36b08
Create 118.pascals-triangle.java
yuchaozh Mar 5, 2019
d8b85f8
Create 119.pascals-triangle-ii.java
yuchaozh Mar 5, 2019
eae3388
Update 206.reverse-linked-list.java
yuchaozh Mar 5, 2019
cc93a65
Create 509.fibonacci-number.java
yuchaozh Mar 5, 2019
643dae2
Update 509.fibonacci-number.java
yuchaozh Mar 5, 2019
8009d2b
Create 70.climbing-stairs.java
yuchaozh Mar 5, 2019
949f579
more
yuchaozh Mar 5, 2019
0ee7c65
more
yuchaozh Mar 5, 2019
0f7596d
Create 104.maximum-depth-of-binary-tree.java
yuchaozh Mar 6, 2019
5c23cb0
Merge branch 'master' of https://github.com/yuchaozh/LeetCode_MarkDown
yuchaozh Mar 6, 2019
b8c7e0b
Create 21.merge-two-sorted-lists.java
yuchaozh Mar 6, 2019
9856573
Create 779.k-th-symbol-in-grammar.java
yuchaozh Mar 6, 2019
7a7d5b1
Create 95.unique-binary-search-trees-ii.java
yuchaozh Mar 6, 2019
3010e3f
Create 1.two-sum.java
yuchaozh Mar 7, 2019
2a6ae76
Create 2.add-two-numbers.java
yuchaozh Mar 7, 2019
a2fae85
Update 146.lru-cache.java
yuchaozh Mar 7, 2019
669b864
Update 146.lru-cache.java
yuchaozh Mar 7, 2019
658c63b
Create 42.trapping-rain-water.java
yuchaozh Mar 8, 2019
b642598
Create 3.longest-substring-without-repeating-characters.java
yuchaozh Mar 8, 2019
9e83cef
Create 5.longest-palindromic-substring.java
yuchaozh Mar 8, 2019
02609dd
Create 138.copy-list-with-random-pointer.java
yuchaozh Mar 8, 2019
c64e4b3
Create 167.two-sum-ii-input-array-is-sorted.java
yuchaozh Mar 8, 2019
d8814ee
Create 125.valid-palindrome.java
yuchaozh Mar 8, 2019
4eacb73
Update 125.valid-palindrome.java
yuchaozh Mar 8, 2019
f33d891
Create 28.implement-str-str.java
yuchaozh Mar 8, 2019
297a3a5
update
yuchaozh Mar 9, 2019
da26c45
Merge branch 'master' of https://github.com/yuchaozh/LeetCode_MarkDown
yuchaozh Mar 9, 2019
8a9b54e
Create 8.string-to-integer-atoi.java
yuchaozh Mar 9, 2019
87c3155
Create 65.valid-number.java
yuchaozh Mar 9, 2019
7f6fc13
Update 3.longest-substring-without-repeating-characters.java
yuchaozh Mar 9, 2019
fb22d3e
Create 159. Longest Substring with At Most Two Distinct Characters.md
yuchaozh Mar 10, 2019
37ea1fb
Update 159. Longest Substring with At Most Two Distinct Characters.md
yuchaozh Mar 10, 2019
d9cb408
Create 163. Missing Ranges.md
yuchaozh Mar 10, 2019
8f34fcb
Update 5.longest-palindromic-substring.java
yuchaozh Mar 10, 2019
b30db96
Update 5.longest-palindromic-substring.java
yuchaozh Mar 10, 2019
b5f856b
Create 7.reverse-integer.java
yuchaozh Mar 10, 2019
78e37fc
Create 161. One Edit Distance.md
yuchaozh Mar 10, 2019
3af1186
Create Insertion Sort.md
yuchaozh Mar 10, 2019
360831c
Update 347. Top K Frequent Elements.md
yuchaozh Mar 11, 2019
d12ee7d
Delete Insertion Sort.md
yuchaozh Mar 11, 2019
233b8a8
Create Sort Algorithm.md
yuchaozh Mar 11, 2019
f6c706d
Create 66.plus-one.java
yuchaozh Mar 11, 2019
25d8d3f
Create 9.palindrome-number.java
yuchaozh Mar 11, 2019
78cbb25
Create 692.top-k-frequent-words.java
yuchaozh Mar 11, 2019
30cc358
Update 21.merge-two-sorted-lists.java
yuchaozh Mar 12, 2019
228539b
Update 2.add-two-numbers.java
yuchaozh Mar 12, 2019
bafec9b
Create 23.merge-k-sorted-lists.java
yuchaozh Mar 12, 2019
c89ebde
Create 98.validate-binary-search-tree.java
yuchaozh Mar 12, 2019
43cde5d
Update 98.validate-binary-search-tree.java
yuchaozh Mar 13, 2019
c9e5955
Update 98.validate-binary-search-tree.java
yuchaozh Mar 13, 2019
1dfd18a
add
yuchaozh Mar 13, 2019
ffce068
Update 110.balanced-binary-tree.java
yuchaozh Mar 13, 2019
40c48e5
Create 295.find-median-from-data-stream.java
yuchaozh Mar 13, 2019
cae0a7c
add
yuchaozh Mar 14, 2019
b252d76
Create 156.binary-tree-upside-down.java
yuchaozh Mar 14, 2019
c34c4ca
Create 136.single-number.java
yuchaozh Mar 15, 2019
45aef0f
add
yuchaozh Mar 17, 2019
a4f5fb7
Create 12.integer-to-roman.java
yuchaozh Mar 17, 2019
d53d97b
Create 13.roman-to-integer.java
yuchaozh Mar 17, 2019
0764f5a
Update 13.roman-to-integer.java
yuchaozh Mar 17, 2019
4847f7c
add
yuchaozh Mar 18, 2019
24eabb6
Create 62.unique-paths.java
yuchaozh Mar 18, 2019
c278af6
aa
yuchaozh Mar 18, 2019
a9f8ee5
Create 53.maximum-subarray.java
yuchaozh Mar 20, 2019
d6405d2
Create .gitignore
yuchaozh Mar 20, 2019
6e0c190
Create 152.maximum-product-subarray.java
yuchaozh Mar 20, 2019
517f0f5
Create 165.compare-version-numbers.java
yuchaozh Mar 20, 2019
ffd4f14
Update 103. Binary Tree Zigzag Level Order Traversal.md
yuchaozh Mar 21, 2019
09076ea
add
yuchaozh Mar 21, 2019
4afb7f1
add
yuchaozh Mar 22, 2019
7442138
Create 252.meeting-rooms.java
yuchaozh Mar 22, 2019
1ef695e
Create 253.meeting-rooms-ii.java
yuchaozh Mar 23, 2019
96dab59
add
yuchaozh Mar 23, 2019
8908273
Update 8.string-to-integer-atoi.java
yuchaozh Mar 23, 2019
70c44ff
Update 344.reverse-string.java
yuchaozh Mar 23, 2019
63c5e6f
dd
yuchaozh Mar 23, 2019
2eb25f3
Update 49.group-anagrams.java
yuchaozh Mar 23, 2019
d6addb1
fsd
yuchaozh Mar 24, 2019
ba8136c
add
yuchaozh Mar 24, 2019
781c3ae
Create 116.populating-next-right-pointers-in-each-node.java
yuchaozh Mar 27, 2019
8f41c4d
Create 706.design-hash-map.java
yuchaozh Mar 27, 2019
fe06833
Create 285.inorder-successor-in-bst.java
yuchaozh Mar 27, 2019
c3b29ea
Create 545.boundary-of-binary-tree.java
yuchaozh Mar 27, 2019
18a28d7
Create 15.3-sum.java
yuchaozh Mar 27, 2019
30a702d
Create 794.valid-tic-tac-toe-state.java
yuchaozh Mar 27, 2019
182d3ad
Update 794.valid-tic-tac-toe-state.java
yuchaozh Mar 27, 2019
ff869d9
Create 122.best-time-to-buy-and-sell-stock-ii.java
yuchaozh Mar 27, 2019
ba29c81
Create 56.merge-intervals.java
yuchaozh Mar 27, 2019
4ae70d2
Update 15.3-sum.java
yuchaozh Mar 27, 2019
6f70cd5
Create 75.sort-colors.java
yuchaozh Mar 27, 2019
2ee9f10
Update 692.top-k-frequent-words.java
yuchaozh Mar 27, 2019
1059048
Create 904.fruit-into-baskets.java
yuchaozh Mar 28, 2019
575303e
Create 25.reverse-nodes-in-k-group.java
yuchaozh Mar 28, 2019
823d2be
Create 322.coin-change.java
yuchaozh Mar 28, 2019
ff832a9
Create 55.jump-game.java
yuchaozh Mar 28, 2019
19b40ed
add
Mar 29, 2019
df22606
sa
yuchaozh Mar 30, 2019
d990626
asdf
yuchaozh Apr 1, 2019
33a10d2
asf
Apr 3, 2019
658b973
Merge branch 'master' of https://github.com/yuchaozh/LeetCode_MarkDown
Apr 3, 2019
1a73b70
sdfsdf
Apr 10, 2019
e2574a3
sdf
May 10, 2019
b9a62af
add one
yuchaozh Jul 8, 2019
6bb1d7a
add
yuchaozh Jul 9, 2019
bbfdfad
Create 977.squares-of-a-sorted-array.java
yuchaozh Jul 9, 2019
74fe3bc
add
yuchaozh Jul 9, 2019
b2b7412
add
yuchaozh Jul 10, 2019
624be58
add
yuchaozh Jul 12, 2019
be1f46b
add
yuchaozh Jul 12, 2019
460ee88
add
yuchaozh Jul 13, 2019
87d9950
s
yuchaozh Sep 14, 2019
f5f359d
f
yuchaozh Sep 14, 2019
e873577
s
yuchaozh Sep 15, 2019
b3da690
s
yuchaozh Sep 15, 2019
91ff56f
add two array questions
yuchaozh Apr 19, 2020
753370e
Add 1431
yuchaozh May 5, 2020
13726d0
add 1389
yuchaozh May 7, 2020
1b05a3b
Add 1266
yuchaozh May 8, 2020
d718569
add
yuchaozh Jul 25, 2020
ba17490
add two problems
yuchaozh Jul 27, 2020
f99b1d1
update
yuchaozh Jul 27, 2020
ca9a357
add
yuchaozh Jul 28, 2020
5154986
1464
yuchaozh Jul 28, 2020
bbb5672
add
yuchaozh Jul 28, 2020
b8665e1
2
yuchaozh Jul 29, 2020
384f36e
add
yuchaozh Aug 14, 2020
4f7bdf5
add more
yuchaozh Sep 19, 2020
cff7d19
add
yuchaozh Jan 2, 2022
13f6549
Create 217.contains-duplicate.java
yuchaozh Jan 2, 2022
2eeb804
Update 53.maximum-subarray.java
yuchaozh Jan 3, 2022
cd8cb3b
Create 1920.build-array-from-permutation.java
yuchaozh Jan 3, 2022
b8b4812
add
yuchaozh Jan 3, 2022
8b82689
add
yuchaozh Jan 4, 2022
c8cf269
add
yuchaozh Jan 4, 2022
8fea47e
Update 118.pascals-triangle.java
yuchaozh Jan 5, 2022
a7f8184
Create 36.valid-sudoku.java
yuchaozh Jan 6, 2022
ee335bf
create
yuchaozh Jan 7, 2022
68c0444
Create 232.implement-queue-using-stacks.java
yuchaozh Jan 8, 2022
a15bab1
Update 21.merge-two-sorted-lists.java
yuchaozh Jan 8, 2022
79d2529
create
yuchaozh Jan 9, 2022
71c6d2c
Merge branch 'master' of https://github.com/yuchaozh/LeetCode_MarkDown
yuchaozh Jan 9, 2022
780411d
Create 1614.maximum-nesting-depth-of-the-parentheses.java
yuchaozh Jan 19, 2022
7b54888
Create 1047.remove-all-adjacent-duplicates-in-string.java
yuchaozh Jan 19, 2022
216efe3
Create 1475.final-prices-with-a-special-discount-in-a-shop.java
yuchaozh Jan 21, 2022
5a1a9f0
Merge branch 'master' of https://github.com/yuchaozh/LeetCode_MarkDown
yuchaozh Jan 21, 2022
3905a44
Create 1700.number-of-students-unable-to-eat-lunch.java
yuchaozh Jan 24, 2022
407ea98
340
yuchaozh May 20, 2022
18f8345
test
yuchaozh May 31, 2022
db66587
add
yuchaozh Jun 1, 2022
0c3f3df
update
yuchaozh Jun 10, 2022
a00c3d1
update
yuchaozh Jun 10, 2022
a9aab8a
add
yuchaozh Jun 10, 2022
822f61e
add
yuchaozh Jun 21, 2022
a78e60f
add
yuchaozh Jul 8, 2022
84f9bf6
add
yuchaozh Jul 9, 2022
db29691
add
yuchaozh Jul 9, 2022
2a485f1
Update MaximumSumSubarrayOfSizeK.java
yuchaozh Jul 9, 2022
3727174
add
yuchaozh Jul 13, 2022
135de91
add
yuchaozh Jul 19, 2022
5cd6ec8
add
yuchaozh Jul 21, 2022
5268744
add
yuchaozh Aug 12, 2022
1dd8d63
add
yuchaozh Aug 12, 2022
565c94c
Update 49.group-anagrams.java
yuchaozh Aug 25, 2022
cdf07d9
Update 347. Top K Frequent Elements.md
yuchaozh Aug 25, 2022
3df9294
Create 238.product-of-array-except-self.java
yuchaozh Aug 26, 2022
bcdb5c4
Update 977.squares-of-a-sorted-array.java
yuchaozh Aug 30, 2022
2562bd7
add
yuchaozh Sep 1, 2022
4a5dd43
add
yuchaozh Sep 5, 2022
8324910
add
yuchaozh Sep 5, 2022
3e5fec9
Create 392.is-subsequence.java
yuchaozh Oct 13, 2022
9b8edf8
Update 1.two-sum.java
yuchaozh Nov 23, 2022
2837c0d
Create 1523.count-odd-numbers-in-an-interval-range.java
yuchaozh Nov 24, 2022
8876a04
Update 1523.count-odd-numbers-in-an-interval-range.java
yuchaozh Nov 24, 2022
4bf7411
Update 141.linked-list-cycle.java
yuchaozh Jan 23, 2023
82da3b6
Create 142.linked-list-cycle-ii.java
yuchaozh Jan 23, 2023
1a727bf
Update 142.linked-list-cycle-ii.java
yuchaozh Jan 23, 2023
95fff17
add
yuchaozh Jan 25, 2023
8957fb7
add
yuchaozh Jan 25, 2023
e42e1fe
add
yuchaozh Jan 26, 2023
da71df4
add
yuchaozh Jan 26, 2023
a673693
Create 2367.number-of-arithmetic-triplets.java
yuchaozh Jan 26, 2023
98ad2e1
Create 557.reverse-words-in-a-string-iii.java
yuchaozh Jan 26, 2023
fb42e7b
update
yuchaozh Jan 28, 2023
0415936
test
yuchaozh Jan 28, 2023
a1d141c
Merge branch 'github:main' into master
yuchaozh Jan 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

hs_err_pid11544.log
*.log
18 changes: 18 additions & 0 deletions .vscode/test.code-snippets
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
// Place your global snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
// description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
// is left empty or omitted, the snippet gets applied to all languages. The prefix is what is
// used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders.
// Placeholders with the same ids are connected.
// Example:
// "Print to console": {
// "scope": "javascript,typescript",
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
}
107 changes: 107 additions & 0 deletions 1.two-sum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import java.util.HashMap;
import java.util.Map;

/*
* @lc app=leetcode id=1 lang=java
*
* [1] Two Sum
*
* https://leetcode.com/problems/two-sum/description/
*
* algorithms
* Easy (41.89%)
* Total Accepted: 1.5M
* Total Submissions: 3.6M
* Testcase Example: '[2,7,11,15]\n9'
*
* Given an array of integers, return indices of the two numbers such that they
* add up to a specific target.
*
* You may assume that each input would have exactly one solution, and you may
* not use the same element twice.
*
* Example:
*
*
* Given nums = [2, 7, 11, 15], target = 9,
*
* Because nums[0] + nums[1] = 2 + 7 = 9,
* return [0, 1].
*
*
*
*
*/


// Solution 1
// brute force with two loops
// Time: O(n2)
// Space: O(1)
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] results = new int[2];

for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] == target) {
results[0] = i;
results[1] = j;
return results;
}
}
}

return results;
}
}

// Solution 2
// One pass with HashMap
// Time: O(n)
// Space: O(n)
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] results = new int[2];
Map<Integer, Integer> map = new HashMap<>();

for (int i = 0; i < nums.length; i++) {
map.put(nums[i], i);
}

for (int i = 0; i < nums.length; i++) {
int sub = target - nums[i];
// 还需判断找到的元素不是当前元素
if (map.containsKey(sub) && map.get(sub) != i) {
results[0] = i;
results[1] = map.get(sub);
return results;
}
}

return results;
}
}

// Solution 3
// one pass with HashMap
// Time complexity: O(n)
// Space complexity: O(n)
class Solution {
public int[] twoSum(int[] nums, int target) {
if (nums == null || nums.length < 2) {
return new int[]{};
}
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
// 不需要判断是不是当前元素了,因为当前元素还没有添加进 map 里
if (map.containsKey(complement)) {
return new int[]{map.get(complement), i};
}
map.put(nums[i], i);
}
return new int[]{};
}
}

122 changes: 122 additions & 0 deletions 100. Same Tree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
### **Description** ###
Given two binary trees, write a function to check if they are the same or not.

Two binary trees are considered the same if they are structurally identical and the nodes have the same value.

Example 1:
```
Input: 1 1
/ \ / \
2 3 2 3

[1,2,3], [1,2,3]

Output: true
```
Example 2:
```
Input: 1 1
/ \
2 2

[1,2], [1,null,2]

Output: false
```
Example 3:
```
Input: 1 1
/ \ / \
2 1 1 2

[1,2,1], [1,1,2]

Output: false
```
---
### **Solution 1** ###
Recursion
```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) {
return true;
}
if (p == null || q == null) {
return false;
}
if (p.val != q.val) {
return false;
}
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}
```
---
### **Solution 2** ###
Similar to the first one, need to make sure call recursion when `p.val == q.val`
```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p == null && q == null) return true;
if(p == null || q == null) return false;
if(p.val == q.val)
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
return false;
}
}
```
---
### **Solution 3** ###
Iterative - BFS
queue
```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
Queue<TreeNode> queue = new LinkedList<>();
queue.add(p);
queue.add(q);
while (!queue.isEmpty()) {
TreeNode f = queue.poll();
TreeNode s = queue.poll();
if (f == null && s == null) {
continue;
} else if (f == null || s == null || f.val != s.val) {
return false;
}
queue.add(f.left);
queue.add(s.left);
queue.add(f.right);
queue.add(s.right);
}
return true;
}
}
```
86 changes: 86 additions & 0 deletions 1002.find-common-characters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* @lc app=leetcode id=1002 lang=java
*
* [1002] Find Common Characters
*
* https://leetcode.com/problems/find-common-characters/description/
*
* algorithms
* Easy (65.76%)
* Likes: 306
* Dislikes: 44
* Total Accepted: 27.8K
* Total Submissions: 42.3K
* Testcase Example: '["bella","label","roller"]'
*
* Given an array A of strings made only from lowercase letters, return a list
* of all characters that show up in all strings within the list (including
* duplicates).  For example, if a character occurs 3 times in all strings but
* not 4 times, you need to include that character three times in the final
* answer.
*
* You may return the answer in any order.
*
*
*
*
* Example 1:
*
*
* Input: ["bella","label","roller"]
* Output: ["e","l","l"]
*
*
*
* Example 2:
*
*
* Input: ["cool","lock","cook"]
* Output: ["c","o"]
*
*
*
*
* Note:
*
*
* 1 <= A.length <= 100
* 1 <= A[i].length <= 100
* A[i][j] is a lowercase letter
*
*
*
*/
class Solution {
public List<String> commonChars(String[] A) {
if (A == null || A.length < 1) {
return new ArrayList<String>();
}

List<String> result = new ArrayList<>();
// a global array which stores the min frequent chars among all strings
int[] count = new int[26];
Arrays.fill(count, Integer.MAX_VALUE);

for (String str : A) {
// find out the frequent for all char from one string
int[] temp = new int[26];
for (int i = 0; i < str.length(); i++) {
temp[str.charAt(i) - 'a']++;
}
// update the global array, which determin the min frequent char among all strings
for (int i = 0; i < 26; i++) {
count[i] = Math.min(count[i], temp[i]);
}
}

for (int i = 0; i < count.length; i++) {
while (count[i]-- > 0) {
result.add(String.valueOf((char)(i + 'a')));
}
}

return result;
}
}

Loading