Skip to content

Commit e31f912

Browse files
committed
190820
1 parent 0e2f917 commit e31f912

6 files changed

+249
-0
lines changed

213_House Robber II.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// 1.0 dp O(n)/O(n)
2+
class Solution {
3+
public int rob(int[] nums) {
4+
if (nums.length == 0) return 0;
5+
if (nums.length == 1) return nums[0];
6+
return Math.max(helper(nums, 0, nums.length - 1),
7+
helper(nums, 1, nums.length));
8+
}
9+
10+
private int helper(int[] nums, int p, int q) {
11+
int[] f = new int[q];
12+
f[p] = nums[p];
13+
if (q - p > 1)
14+
f[p + 1] = Math.max(nums[p], nums[p + 1]);
15+
for (int i = p + 2; i < q; i++) {
16+
f[i] = Math.max(f[i-2] + nums[i], f[i-1]);
17+
}
18+
return f[q - 1];
19+
}
20+
}
21+
22+
// 1.1 O(n)/O(1)
23+
class Solution {
24+
public int rob(int[] nums) {
25+
if (nums.length == 0) return 0;
26+
if (nums.length == 1) return nums[0];
27+
return Math.max(helper(nums, 0, nums.length - 1),
28+
helper(nums, 1, nums.length));
29+
}
30+
31+
private int helper(int[] nums, int p, int q) {
32+
int pre2 = 0, pre1 = 0;
33+
for (int i = first; i <= last; i++) {
34+
int cur = Math.max(pre1, pre2 + nums[i]);
35+
pre2 = pre1;
36+
pre1 = cur;
37+
}
38+
return pre1;
39+
}
40+
}

283_Move Zeroes.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public void moveZeroes(int[] nums) {
3+
int count = 0;
4+
for (int i = 0; i < nums.length; i++) {
5+
if (nums[i] == 0) {
6+
count++;
7+
} else {
8+
nums[i - count] = nums[i];
9+
}
10+
}
11+
for (int i = nums.length - 1; i >= nums.length - count; i--) nums[i] = 0;
12+
}
13+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// 1.0 DFS, pre-order
2+
/**
3+
* Definition for a binary tree node.
4+
* public class TreeNode {
5+
* int val;
6+
* TreeNode left;
7+
* TreeNode right;
8+
* TreeNode(int x) { val = x; }
9+
* }
10+
*/
11+
public class Codec {
12+
private static final String SPL = ",";
13+
private static final String NUL = "X";
14+
15+
// Encodes a tree to a single string.
16+
public String serialize(TreeNode root) {
17+
StringBuilder sb = new StringBuilder();
18+
buildString(root, sb);
19+
return sb.toString();
20+
}
21+
22+
private void buildString(TreeNode node, StringBuilder sb) {
23+
if (node == null) {
24+
sb.append(NUL).append(SPL);
25+
} else {
26+
sb.append(node.val).append(SPL);
27+
buildString(node.left, sb);
28+
buildString(node.right,sb);
29+
}
30+
}
31+
// Decodes your encoded data to tree.
32+
public TreeNode deserialize(String data) {
33+
Deque<String> nodes = new LinkedList<>();
34+
nodes.addAll(Arrays.asList(data.split(SPL)));
35+
return buildTree(nodes);
36+
}
37+
38+
private TreeNode buildTree(Deque<String> nodes) {
39+
String val = nodes.remove();
40+
if (val.equals(NUL)) return null;
41+
else {
42+
TreeNode node = new TreeNode(Integer.valueOf(val));
43+
node.left = buildTree(nodes);
44+
node.right = buildTree(nodes);
45+
return node;
46+
}
47+
}
48+
}
49+
50+
// Your Codec object will be instantiated and called as such:
51+
// Codec codec = new Codec();
52+
// codec.deserialize(codec.serialize(root));
53+
54+
// 2.0 BFS
55+
public class Codec {
56+
public String serialize(TreeNode root) {
57+
if (root == null) return "";
58+
Queue<TreeNode> q = new LinkedList<>();
59+
StringBuilder res = new StringBuilder();
60+
q.add(root);
61+
while (!q.isEmpty()) {
62+
TreeNode node = q.poll();
63+
if (node == null) {
64+
res.append("n ");
65+
continue;
66+
}
67+
res.append(node.val + " ");
68+
q.add(node.left);
69+
q.add(node.right);
70+
}
71+
return res.toString();
72+
}
73+
74+
public TreeNode deserialize(String data) {
75+
if (data == "") return null;
76+
Queue<TreeNode> q = new LinkedList<>();
77+
String[] values = data.split(" ");
78+
TreeNode root = new TreeNode(Integer.parseInt(values[0]));
79+
q.add(root);
80+
for (int i = 1; i < values.length; i++) {
81+
TreeNode parent = q.poll();
82+
if (!values[i].equals("n")) {
83+
TreeNode left = new TreeNode(Integer.parseInt(values[i]));
84+
parent.left = left;
85+
q.add(left);
86+
}
87+
if (!values[++i].equals("n")) {
88+
TreeNode right = new TreeNode(Integer.parseInt(values[i]));
89+
parent.right = right;
90+
q.add(right);
91+
}
92+
}
93+
return root;
94+
}
95+
}

303_Range Sum Query - Immutable.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class NumArray {
2+
3+
private int[] sums = null;
4+
5+
public NumArray(int[] nums) {
6+
sums = new int[nums.length + 1];
7+
for (int i = 1; i <= nums.length; i++) {
8+
sums[i] = sums[i - 1] + nums[i - 1];
9+
}
10+
}
11+
12+
public int sumRange(int i, int j) {
13+
return sums[j + 1] - sums[i];
14+
}
15+
}
16+
17+
/**
18+
* Your NumArray object will be instantiated and called as such:
19+
* NumArray obj = new NumArray(nums);
20+
* int param_1 = obj.sumRange(i,j);
21+
*/

343_Integer Break.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// 1.0 O(n^2)/O(n), dp
2+
class Solution {
3+
public int integerBreak(int n) {
4+
int[] dp = new int[n + 1];
5+
dp[1] = 1;
6+
for (int i = 2; i <= n; i++) {
7+
for (int j = 1; j <= i - 1; j++) {
8+
dp[i] = Math.max(dp[i], Math.max(j * dp[i - j], j * (i - j)));
9+
}
10+
}
11+
return dp[n];
12+
}
13+
}
14+
15+
// 2.0 O(logn)或O(1)/O(logn)或O(1) 取决于幂运算pow
16+
public int integerBreak(int n) {
17+
if(n <= 3) return n - 1;
18+
if(n % 3 == 0)
19+
return (int)Math.pow(3, n / 3);
20+
else if(n % 3 == 1)
21+
return 4 * (int)Math.pow(3, (n - 4) / 3);
22+
else // (n % 3 == 2)
23+
return 2 * (int)Math.pow(3, n / 3);
24+
}

413_Arithmetic Slices.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// 1.0 dp, O(n)/O(n)
2+
3+
class Solution {
4+
public int numberOfArithmeticSlices(int[] A) {
5+
if (A == null || A.length == 0) {
6+
return 0;
7+
}
8+
int n = A.length;
9+
int[] dp = new int[n];
10+
for (int i = 2; i < n; i++) {
11+
if (A[i] - A[i - 1] == A[i - 1] - A[i - 2]) {
12+
dp[i] = dp[i - 1] + 1;
13+
}
14+
}
15+
int total = 0;
16+
for (int cnt : dp) {
17+
total += cnt;
18+
}
19+
return total;
20+
}
21+
}
22+
23+
// 1.1 dp, O(n)/O(1)
24+
public int numberOfArithmeticSlices(int[] A) {
25+
if (A == null || A.length == 0) {
26+
return 0;
27+
}
28+
int n = A.length, cur = 0, res = 0;
29+
for (int i = 2; i < n; i++) {
30+
if (A[i] - A[i - 1] == A[i - 1] - A[i - 2]) {
31+
cur++;
32+
res += cur;
33+
} else {
34+
cur = 0;
35+
}
36+
}
37+
return res;
38+
}
39+
40+
// 2.0 math, O(n)/O(1)
41+
public int numberOfArithmeticSlices(int[] A) {
42+
if (A == null || A.length == 0) {
43+
return 0;
44+
}
45+
int res = 0, len = 2, n = A.length;
46+
for (int i = 2; i < n; ++i) {
47+
if (A[i] - A[i - 1] == A[i - 1] - A[i - 2]) {
48+
++len;
49+
} else {
50+
if (len > 2) res += (len - 1) * (len - 2) * 0.5;
51+
len = 2;
52+
}
53+
}
54+
if (len > 2) res += (len - 1) * (len - 2) * 0.5;
55+
return res;
56+
}

0 commit comments

Comments
 (0)