Skip to content

Commit d38567f

Browse files
committed
萤火虫-添加其它之前做过的题
1 parent 55a2124 commit d38567f

39 files changed

+1079
-0
lines changed

src/main/java/N101_200/N101.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package N101_200;
2+
3+
/**
4+
* @author Yasin Shaw
5+
* @version v1.0
6+
*/
7+
public class N101 {
8+
9+
private class TreeNode {
10+
int val;
11+
TreeNode left;
12+
TreeNode right;
13+
14+
TreeNode(int x) {
15+
val = x;
16+
}
17+
}
18+
19+
public boolean isSymmetric(TreeNode root) {
20+
return root == null ? true : isSymmetric(root.left, root.right);
21+
}
22+
23+
private boolean isSymmetric(TreeNode left, TreeNode right) {
24+
if (left == null && right == null) {
25+
return true;
26+
} else if (left == null || right == null) {
27+
return false;
28+
} else {
29+
return left.val == right.val && isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left);
30+
}
31+
}
32+
}

src/main/java/N101_200/N104.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package N101_200;
2+
3+
/**
4+
* @author Yasin Shaw
5+
* @version v1.0
6+
*/
7+
public class N104 {
8+
9+
private class TreeNode {
10+
int val;
11+
TreeNode left;
12+
TreeNode right;
13+
14+
TreeNode(int x) {
15+
val = x;
16+
}
17+
}
18+
19+
public int maxDepth(TreeNode root) {
20+
return getNextDepth(root, 0);
21+
}
22+
23+
private int getNextDepth(TreeNode root, int currentDepth) {
24+
if (root == null) {
25+
return currentDepth;
26+
} else {
27+
return Math.max(getNextDepth(root.left, currentDepth + 1), getNextDepth(root.right, currentDepth + 1));
28+
}
29+
}
30+
}

src/main/java/N101_200/N107.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package N101_200;
2+
3+
import java.util.ArrayList;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
8+
/**
9+
* @author Yasin Shaw
10+
* @version v1.0
11+
*/
12+
public class N107 {
13+
private class TreeNode {
14+
int val;
15+
TreeNode left;
16+
TreeNode right;
17+
18+
TreeNode(int x) {
19+
val = x;
20+
}
21+
}
22+
23+
public List<List<Integer>> levelOrderBottom(TreeNode root) {
24+
List<TreeNode> treeNodes = new ArrayList<>(1);
25+
if (root == null) {
26+
return new LinkedList<>();
27+
}
28+
treeNodes.add(root);
29+
return getThisLevel(treeNodes);
30+
}
31+
32+
private List<List<Integer>> getThisLevel(List<TreeNode> treeNodes) {
33+
List<TreeNode> thisLevelTreeNodes = new LinkedList<>();
34+
List<Integer> thisLevelValues = treeNodes.stream().map(treeNode -> treeNode.val).collect(Collectors.toList());
35+
treeNodes.forEach(treeNode -> {
36+
if (treeNode.left != null) {
37+
thisLevelTreeNodes.add(treeNode.left);
38+
}
39+
if (treeNode.right != null) {
40+
thisLevelTreeNodes.add(treeNode.right);
41+
}
42+
});
43+
44+
List<List<Integer>> result = new LinkedList<>();
45+
if (treeNodes.size() > 0) {
46+
List<List<Integer>> nextLevel = getThisLevel(thisLevelTreeNodes);
47+
if (nextLevel.get(0).size() > 0) {
48+
result.addAll(nextLevel);
49+
}
50+
}
51+
result.add(thisLevelValues);
52+
return result;
53+
}
54+
}

src/main/java/N101_200/N108.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package N101_200;
2+
3+
/**
4+
* @author Yasin Shaw
5+
* @version v1.0
6+
*/
7+
public class N108 {
8+
9+
private class TreeNode {
10+
int val;
11+
TreeNode left;
12+
TreeNode right;
13+
14+
TreeNode(int x) {
15+
val = x;
16+
}
17+
}
18+
19+
public TreeNode sortedArrayToBST(int[] nums) {
20+
if (nums == null || nums.length == 0) {
21+
return null;
22+
}
23+
return helper(nums, 0, nums.length - 1);
24+
}
25+
26+
private TreeNode helper(int[] nums, int start, int end) {
27+
if (start > end) {
28+
return null;
29+
}
30+
int mid = start + (end - start) / 2;
31+
TreeNode root = new TreeNode(nums[mid]);
32+
root.left = helper(nums, start, mid - 1);
33+
root.right = helper(nums, mid + 1, end);
34+
return root;
35+
}
36+
}

src/main/java/N101_200/N110.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package N101_200;
2+
3+
import common.TreeNode;
4+
5+
/**
6+
* @author Yasin Shaw
7+
* @version v1.0
8+
*/
9+
public class N110 {
10+
public boolean isBalanced(TreeNode root) {
11+
if (root == null) {
12+
return true;
13+
}
14+
int left = search(root.left, 0);
15+
int right = search(root.right, 0);
16+
if (Math.abs(left - right) > 1) {
17+
return false;
18+
}
19+
return isBalanced(root.left) && isBalanced(root.right);
20+
}
21+
22+
private int search(TreeNode treeNode, int count) {
23+
if (treeNode == null) {
24+
return count;
25+
}
26+
return Math.max(search(treeNode.left, count + 1), search(treeNode.right, count + 1));
27+
}
28+
}

src/main/java/N101_200/N111.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package N101_200;
2+
3+
import common.TreeNode;
4+
5+
/**
6+
* @author Yasin Shaw
7+
* @version v1.0
8+
*/
9+
public class N111 {
10+
public int minDepth(TreeNode root) {
11+
return minDepth(root, 0);
12+
}
13+
14+
private int minDepth(TreeNode root, int count) {
15+
int depth = count;
16+
if (root != null) {
17+
depth++;
18+
if (root.left == null) {
19+
depth = minDepth(root.right, depth);
20+
} else if (root.right == null) {
21+
depth = minDepth(root.left, depth);
22+
} else {
23+
depth = Math.min(minDepth(root.left, depth), minDepth(root.right, depth));
24+
}
25+
}
26+
return depth;
27+
}
28+
}

src/main/java/N101_200/N112.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package N101_200;
2+
3+
import common.TreeNode;
4+
5+
/**
6+
* @author Yasin Shaw
7+
* @version v1.0
8+
*/
9+
public class N112 {
10+
public boolean hasPathSum(TreeNode root, int sum) {
11+
if (root == null) {
12+
return false;
13+
}
14+
15+
if (root.left == null && root.right == null) {
16+
return root.val == sum;
17+
} else {
18+
sum -= root.val;
19+
return hasPathSum(root.left, sum) || hasPathSum(root.right, sum);
20+
}
21+
}
22+
}

src/main/java/N101_200/N118.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package N101_200;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
6+
/**
7+
* @author Yasin Shaw
8+
* @version v1.0
9+
*/
10+
public class N118 {
11+
public List<List<Integer>> generate(int numRows) {
12+
List<List<Integer>> result = new LinkedList<>();
13+
List<Integer> first = new LinkedList<>();
14+
if (numRows >= 1) {
15+
first.add(1);
16+
List<Integer> previous = first;
17+
result.add(first);
18+
for (int i = 1; i < numRows; i++) {
19+
List<Integer> current = new LinkedList<>();
20+
current.add(1);
21+
for (int j = 0; j < previous.size() - 1; j++) {
22+
current.add(previous.get(j) + previous.get(j + 1));
23+
}
24+
current.add(1);
25+
previous = current;
26+
result.add(current);
27+
}
28+
}
29+
return result;
30+
}
31+
}

src/main/java/N101_200/N119.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package N101_200;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* @author Yasin Shaw
8+
* @version v1.0
9+
*/
10+
public class N119 {
11+
public List<Integer> getRow(int rowIndex) {
12+
List<Integer> row = new ArrayList<>(rowIndex + 1);
13+
for (int i = 0; i <= rowIndex; i++) {
14+
for (int j = i - 1; j >= 1; j--)
15+
row.set(j, row.get(j - 1) + row.get(j));
16+
row.add(1);
17+
}
18+
return row;
19+
}
20+
}

src/main/java/N101_200/N121.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package N101_200;
2+
3+
/**
4+
* @author Yasin Shaw
5+
* @version v1.0
6+
*/
7+
public class N121 {
8+
public int maxProfit(int[] prices) {
9+
if (prices.length < 2) return 0;
10+
int profit = 0;
11+
int low = prices[0];
12+
for (int i = 1; i < prices.length; i++) {
13+
low = Math.min(prices[i], low);
14+
profit = Math.max(prices[i] - low, profit);
15+
}
16+
return profit;
17+
}
18+
}

src/main/java/N101_200/N122.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package N101_200;
2+
3+
/**
4+
* @author Yasin Shaw
5+
* @version v1.0
6+
*/
7+
public class N122 {
8+
public int maxProfit(int[] prices) {
9+
if (prices == null || prices.length == 0)
10+
return 0;
11+
int profile = 0;
12+
for (int i = 0; i < prices.length - 1; i++)
13+
if (prices[i] < prices[i + 1])
14+
profile += prices[i + 1] - prices[i];
15+
return profile;
16+
}
17+
}

src/main/java/N101_200/N125.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package N101_200;
2+
3+
/**
4+
* @author Yasin Shaw
5+
* @version v1.0
6+
*/
7+
public class N125 {
8+
public boolean isPalindrome(String s) {
9+
char[] chars = s.toCharArray();
10+
for (int i = 0, j = chars.length - 1; i <= j; i++, j--) {
11+
while (i < j && isNotAlphanumeric(chars[i])) {
12+
i++;
13+
}
14+
while (i < j && isNotAlphanumeric(chars[j])) {
15+
j--;
16+
}
17+
if (!equalsIgnoreCase(chars[i], chars[j])) {
18+
return false;
19+
}
20+
}
21+
return true;
22+
}
23+
24+
private boolean isNotAlphanumeric(char c) {
25+
return !Character.isAlphabetic(c) && !Character.isDigit(c);
26+
}
27+
28+
private boolean equalsIgnoreCase(char a, char b) {
29+
return Character.toLowerCase(a) == Character.toLowerCase(b);
30+
}
31+
}

src/main/java/N101_200/N136.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package N101_200;
2+
3+
/**
4+
* @author Yasin Shaw
5+
* @version v1.0
6+
*/
7+
public class N136 {
8+
public int singleNumber(int[] nums) {
9+
int ans = 0;
10+
11+
int len = nums.length;
12+
for (int i = 0; i != len; i++)
13+
ans ^= nums[i];
14+
15+
return ans;
16+
}
17+
}

src/main/java/N101_200/N141.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package N101_200;
2+
3+
import common.ListNode;
4+
5+
/**
6+
* @author Yasin Shaw
7+
* @version v1.0
8+
*/
9+
public class N141 {
10+
public boolean hasCycle(ListNode head) {
11+
if (head == null) return false;
12+
ListNode walker = head;
13+
ListNode runner = head;
14+
while (runner.next != null && runner.next.next != null) {
15+
walker = walker.next;
16+
runner = runner.next.next;
17+
if (walker == runner) return true;
18+
}
19+
return false;
20+
}
21+
}

0 commit comments

Comments
 (0)