Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c3c74d5

Browse files
20200905
1 parent ea21740 commit c3c74d5

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* @lc app=leetcode id=1305 lang=java
3+
*
4+
* [1305] All Elements in Two Binary Search Trees
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* public class TreeNode {
11+
* int val;
12+
* TreeNode left;
13+
* TreeNode right;
14+
* TreeNode() {}
15+
* TreeNode(int val) { this.val = val; }
16+
* TreeNode(int val, TreeNode left, TreeNode right) {
17+
* this.val = val;
18+
* this.left = left;
19+
* this.right = right;
20+
* }
21+
* }
22+
*/
23+
*/
24+
class Solution {
25+
/*
26+
Two stack and merge sort
27+
time: O(n)
28+
space: O(h)
29+
*/
30+
public List<Integer> getAllElements(TreeNode root1, TreeNode root2) {
31+
List<Integer> res = new ArrayList<>();
32+
Stack<TreeNode> s1 = new Stack<>();
33+
Stack<TreeNode> s2 = new Stack<>();
34+
pushLeft(s1, root1);
35+
pushLeft(s2, root2);
36+
while (!s1.isEmpty() || !s2.isEmpty()) {
37+
Stack<TreeNode> cur;
38+
if (s1.isEmpty()) cur = s2;
39+
else if (s2.isEmpty()) cur = s1;
40+
else {
41+
cur = s1.peek().val < s2.peek().val ? s1 : s2;
42+
}
43+
TreeNode node = cur.pop();
44+
res.add(node.val);
45+
pushLeft(cur, node.right);
46+
}
47+
return res;
48+
}
49+
50+
private void pushLeft(Stack<TreeNode> stack, TreeNode root) {
51+
while (root != null) {
52+
stack.push(root);
53+
root = root.left;
54+
}
55+
}
56+
}
57+
// @lc code=end
58+

‎Java/198.house-robber.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* @lc app=leetcode id=198 lang=java
3+
*
4+
* [198] House Robber
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
/*
10+
prevMax记录前一家没有抢的max, curMax记录当前家一定抢的max
11+
time: O(n)
12+
space: O(1)
13+
*/
14+
public int rob(int[] nums) {
15+
if (nums == null || nums.length == 0) return 0;
16+
int prevMax = 0, curMax = 0;
17+
for (int num : nums) {
18+
int temp = curMax;
19+
curMax = Math.max(curMax, prevMax + num);
20+
prevMax = temp;
21+
}
22+
return curMax;
23+
}
24+
}
25+
// @lc code=end
26+

‎Java/213.house-robber-ii.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* @lc app=leetcode id=213 lang=java
3+
*
4+
* [213] House Robber II
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
/*
10+
分第一家抢or not两种情况取dp最大值
11+
time: O(n)
12+
space: O(1)
13+
*/
14+
public int rob(int[] nums) {
15+
if (nums == null || nums.length == 0) return 0;
16+
if (nums.length == 1) return nums[0];
17+
return Math.max(helper(nums, 0, nums.length - 2), helper(nums, 1, nums.length - 1));
18+
}
19+
20+
private int helper(int[] nums, int start, int end) {
21+
int prevMax = 0, curMax = 0;
22+
for (int i = start; i <= end; ++i) {
23+
int temp = curMax;
24+
curMax = Math.max(curMax, prevMax + nums[i]);
25+
prevMax = temp;
26+
}
27+
return curMax;
28+
}
29+
}
30+
// @lc code=end
31+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /