Skip to content

Commit 12d5709

Browse files
committed
File is Added
1 parent deb0675 commit 12d5709

File tree

24 files changed

+334
-0
lines changed

24 files changed

+334
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import java.util.Stack;
2+
3+
/**
4+
* Definition for a binary tree node.
5+
* public class TreeNode {
6+
* int val;
7+
* TreeNode left;
8+
* TreeNode right;
9+
* TreeNode() {}
10+
* TreeNode(int val) { this.val = val; }
11+
* TreeNode(int val, TreeNode left, TreeNode right) {
12+
* this.val = val;
13+
* this.left = left;
14+
* this.right = right;
15+
* }
16+
* }
17+
*/
18+
class BSTIterator {
19+
private final Stack<TreeNode> stack = new Stack<>();
20+
21+
private void pushAll(TreeNode node) {
22+
while (node != null) {
23+
stack.push(node);
24+
node = node.left;
25+
}
26+
}
27+
28+
public BSTIterator(TreeNode root) {
29+
pushAll(root);
30+
}
31+
32+
public int next() {
33+
TreeNode tempNode = stack.pop();
34+
pushAll(tempNode.right);
35+
return tempNode.val;
36+
}
37+
38+
public boolean hasNext() {
39+
return !stack.isEmpty();
40+
}
41+
}
42+
43+
/**
44+
* Your BSTIterator object will be instantiated and called as such:
45+
* BSTIterator obj = new BSTIterator(root);
46+
* int param_1 = obj.next();
47+
* boolean param_2 = obj.hasNext();
48+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [173. Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class BestTimeToBuyAndSellStockIv {
2+
public int maxProfit(int k, int[] prices) {
3+
int n = prices.length;
4+
if (n <= 1) return 0;
5+
if (k >= n / 2) {
6+
int maxPro = 0;
7+
for (int i = 1; i < n; i++) {
8+
if (prices[i] > prices[i - 1]) maxPro += prices[i] - prices[i - 1];
9+
}
10+
return maxPro;
11+
}
12+
13+
int[][] dp = new int[k + 1][n];
14+
for (int i = 1; i <= k; i++) {
15+
int localMax = dp[i - 1][0] - prices[0];
16+
for (int j = 1; j < n; j++) {
17+
dp[i][j] = Math.max(dp[i][j - 1], prices[j] + localMax);
18+
localMax = Math.max(localMax, dp[i - 1][j] - prices[j]);
19+
}
20+
}
21+
return dp[k][n - 1];
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [188. Best Time to Buy and Sell Stock IV](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/)
2+

0189.rotate-array/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [189. Rotate Array](https://leetcode.com/problems/rotate-array/)

0189.rotate-array/RotateArray.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class RotateArray {
2+
private void reverse(int[] nums, int l, int r) {
3+
while (l < r) {
4+
int temp = nums[l];
5+
nums[l] = nums[r];
6+
nums[r] = temp;
7+
l++;
8+
r--;
9+
}
10+
}
11+
12+
public void rotate(int[] nums, int k) {
13+
k = k % nums.length;
14+
// reverse array
15+
int l = 0, r = nums.length - 1;
16+
reverse(nums, l, r);
17+
18+
// reverse left part
19+
r = k - 1;
20+
reverse(nums, l, r);
21+
22+
// reverse right part
23+
l = k;
24+
r = nums.length - 1;
25+
reverse(nums, l, r);
26+
}
27+
}

0190.reverse-bits/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [190. Reverse Bits](https://leetcode.com/problems/reverse-bits/)
2+

0190.reverse-bits/ReverseBits.java

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class ReverseBits {
2+
// you need treat n as an unsigned value
3+
public int reverseBits(int n) {
4+
int ret = 0;
5+
for (int i = 0; i < 32; i++) {
6+
ret <<= 1;
7+
ret |= (n & 1);
8+
n >>= 1;
9+
}
10+
return ret;
11+
}
12+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class NumberOf1Bits {
2+
// you need to treat n as an unsigned value
3+
public int hammingWeight(int n) {
4+
int cnt = 0;
5+
// deal with sign part
6+
if (n < 0) {
7+
cnt++;
8+
n = n + 1 + Integer.MAX_VALUE;
9+
}
10+
while (n != 0) {
11+
if ((n & 1) == 1) cnt++;
12+
n = n >> 1;
13+
}
14+
return cnt;
15+
}
16+
}

0191.number-of-1-bits/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [191. Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/)
2+

0198.house-robber/HouseRobber.java

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class HouseRobber {
2+
public int rob(int[] nums) {
3+
int n = nums.length;
4+
if (n == 0) return 0;
5+
if (n == 1) return nums[0];
6+
int[] dp = new int[n];
7+
dp[0] = nums[0];
8+
dp[1] = Math.max(nums[0], nums[1]);
9+
for (int i = 2; i < n; i++)
10+
dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]);
11+
12+
return dp[n - 1];
13+
}
14+
}

0198.house-robber/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# [198. House Robber](https://leetcode.com/problems/house-robber/)
2+
3+
4+
Complexity Analysis:
5+
6+
- Time Complexity: $O(n)$. $n$ is the length of `nums`.
7+
- Space Complexity: $O(1)$.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
/**
5+
* Definition for a binary tree node.
6+
* public class TreeNode {
7+
* int val;
8+
* TreeNode left;
9+
* TreeNode right;
10+
* TreeNode() {}
11+
* TreeNode(int val) { this.val = val; }
12+
* TreeNode(int val, TreeNode left, TreeNode right) {
13+
* this.val = val;
14+
* this.left = left;
15+
* this.right = right;
16+
* }
17+
* }
18+
*/
19+
class BinaryTreeRightSideView {
20+
private void rightSideViewHelper(TreeNode root, int level, List<Integer> list) {
21+
if (root == null) {
22+
return;
23+
}
24+
if (level == list.size()) {
25+
list.add(root.val);
26+
}
27+
rightSideViewHelper(root.right, level + 1, list);
28+
rightSideViewHelper(root.left, level + 1, list);
29+
}
30+
31+
public List<Integer> rightSideView(TreeNode root) {
32+
List<Integer> list = new ArrayList<>();
33+
rightSideViewHelper(root, 0, list);
34+
return list;
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [199. Binary Tree Right Side View](https://leetcode.com/problems/binary-tree-right-side-view/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class NumberOfIslands {
2+
private int m;
3+
private int n;
4+
5+
private void DFSMarking(char[][] grid, int i, int j) {
6+
if (i < 0 || j < 0 || i >= m || j >= n || grid[i][j] == '0') return;
7+
grid[i][j] = '0';
8+
DFSMarking(grid, i + 1, j);
9+
DFSMarking(grid, i - 1, j);
10+
DFSMarking(grid, i, j + 1);
11+
DFSMarking(grid, i, j - 1);
12+
}
13+
14+
public int numIslands(char[][] grid) {
15+
int island = 0;
16+
m = grid.length;
17+
n = grid[0].length;
18+
for (int i = 0; i < m; i++) {
19+
for (int j = 0; j < n; j++) {
20+
if (grid[i][j] == '1') {
21+
DFSMarking(grid, i, j);
22+
island++;
23+
}
24+
}
25+
}
26+
return island;
27+
}
28+
}

0200.number-of-islands/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [200. Number of Islands](https://leetcode.com/problems/number-of-islands/)
2+

0202.happy-number/HappyNumber.java

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
class HappyNumber {
5+
private int happyNumber(int n) {
6+
int res = 0;
7+
while (n > 0) {
8+
int num = n % 10;
9+
res += num * num;
10+
n /= 10;
11+
}
12+
return res;
13+
}
14+
15+
public boolean isHappy(int n) {
16+
Map<Integer, Integer> map = new HashMap<>();
17+
int happyNumber = n;
18+
map.put(happyNumber, 1);
19+
while (happyNumber != 0 && happyNumber != 1) {
20+
happyNumber = happyNumber(happyNumber);
21+
if (map.containsKey(happyNumber)) {
22+
return false;
23+
} else {
24+
map.put(happyNumber, 1);
25+
}
26+
}
27+
return happyNumber == 1;
28+
}
29+
}

0202.happy-number/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [202. Happy Number](https://leetcode.com/problems/happy-number/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class IsomorphicStrings {
2+
public boolean isIsomorphic(String s, String t) {
3+
int[] preIndexOfs = new int[256];
4+
int[] preIndexOft = new int[256];
5+
for (int i = 0; i < s.length(); i++) {
6+
char sc = s.charAt(i), tc = t.charAt(i);
7+
if (preIndexOfs[sc] != preIndexOft[tc]) {
8+
return false;
9+
}
10+
preIndexOfs[sc] = i + 1;
11+
preIndexOft[tc] = i + 1;
12+
}
13+
return true;
14+
}
15+
}

0205.isomorphic-strings/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [205. Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/)

0206.reverse-linked-list/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [206. Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 ReverseLinkedList {
12+
public ListNode reverseList(ListNode head) {
13+
if (head == null || head.next == null) {
14+
return head;
15+
}
16+
ListNode next = head.next;
17+
ListNode newHead = reverseList(next);
18+
next.next = head;
19+
head.next = null;
20+
return newHead;
21+
}
22+
}
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
class CourseSchedule {
5+
private boolean hasCycle(boolean[] globalMarked, boolean[] localMarked, List<Integer>[] graphic, int curNode) {
6+
if (localMarked[curNode]) {
7+
return true;
8+
}
9+
if (globalMarked[curNode]) {
10+
return false;
11+
}
12+
globalMarked[curNode] = true;
13+
localMarked[curNode] = true;
14+
for (int nextNode : graphic[curNode]) {
15+
if (hasCycle(globalMarked, localMarked, graphic, nextNode)) {
16+
return true;
17+
}
18+
}
19+
localMarked[curNode] = false;
20+
return false;
21+
}
22+
23+
public boolean canFinish(int numCourses, int[][] prerequisites) {
24+
List<Integer>[] graphic = new List[numCourses];
25+
for (int i = 0; i < numCourses; i++) {
26+
graphic[i] = new ArrayList<>();
27+
}
28+
for (int[] pre : prerequisites) {
29+
graphic[pre[0]].add(pre[1]);
30+
}
31+
boolean[] globalMarked = new boolean[numCourses];
32+
boolean[] localMarked = new boolean[numCourses];
33+
for (int i = 0; i < numCourses; i++) {
34+
if (hasCycle(globalMarked, localMarked, graphic, i)) {
35+
return false;
36+
}
37+
}
38+
return true;
39+
}
40+
}

0207.course-schedule/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [207. Course Schedule](https://leetcode.com/problems/course-schedule/)

0 commit comments

Comments
 (0)