Skip to content

Commit 699a5ad

Browse files
authored
Add files via upload
1 parent 5ddea12 commit 699a5ad

12 files changed

+313
-0
lines changed

热题HOT100_Java/Solution001.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package HOT100;
2+
3+
import java.util.HashMap;
4+
5+
// 两数之和
6+
public class Solution001 {
7+
public int[] twoSum(int[] nums, int target) {
8+
HashMap<Integer, Integer> map = new HashMap<>();
9+
int[] res = new int[2];
10+
11+
for (int i = 0; i < nums.length; i++) {
12+
if (map.containsKey(target - nums[i])) {
13+
res[0] = i;
14+
res[1] = map.get(target - nums[i]);
15+
return res;
16+
} else {
17+
map.put(nums[i], i);
18+
}
19+
}
20+
return res;
21+
}
22+
}

热题HOT100_Java/Solution002.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package HOT100;
2+
3+
4+
/*
5+
class ListNode {
6+
int val;
7+
ListNode next;
8+
ListNode(int x) { val = x; }
9+
}
10+
*/
11+
12+
13+
// 两数相加
14+
public class Solution002 {
15+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
16+
ListNode root = new ListNode(0);
17+
ListNode cursor = root;
18+
int carry = 0;
19+
20+
while (l1 != null || l2 != null || carry != 0) {
21+
int l1Val = l1 != null ? l1.val : 0;
22+
int l2Val = l2 != null ? l2.val : 0;
23+
int sumVal = l1Val + l2Val + carry;
24+
carry = sumVal / 10;
25+
26+
ListNode sumNode = new ListNode(sumVal % 10);
27+
cursor.next = sumNode;
28+
cursor = sumNode;
29+
30+
if (l1 != null) l1 = l1.next;
31+
if (l2 != null) l2 = l2.next;
32+
}
33+
return root.next;
34+
}
35+
}

热题HOT100_Java/Solution003.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package HOT100;
2+
3+
import java.util.HashMap;
4+
5+
// 无重复字符的最长子串
6+
public class Solution003 {
7+
public int lengthOfLongestSubstring(String s) {
8+
HashMap<Character, Integer> map = new HashMap<>();
9+
int start = 0, res = 0;
10+
11+
for (int i = 0; i < s.length(); i++) {
12+
char c = s.charAt(i);
13+
// 如果字符重复,则更新起点
14+
if (map.containsKey(c))
15+
start = Math.max(start, map.get(c) + 1);
16+
// 更新当前字符索引
17+
map.put(c, i);
18+
// 计算当前最长子串
19+
res = Math.max(res, i - start + 1);
20+
}
21+
return res;
22+
}
23+
}

热题HOT100_Java/Solution004.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package HOT100;
2+
3+
import java.util.Arrays;
4+
5+
// 寻找两个有序数组的中位数
6+
public class Solution004 {
7+
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
8+
// Arrays.copyOf(源数组, 复制长度)
9+
int[] nums = Arrays.copyOf(nums1, nums1.length + nums2.length);
10+
// System.arraycopy(源数组, 源数组起始索引, 目标数组, 目标数组起始索引, 复制长度)
11+
System.arraycopy(nums2, 0, nums, nums1.length, nums2.length);
12+
Arrays.sort(nums);
13+
14+
if (nums.length % 2 == 0)
15+
return ((float) nums[nums.length / 2 - 1] + nums[nums.length / 2]) / 2;
16+
else
17+
return (float) nums[nums.length / 2];
18+
}
19+
}

热题HOT100_Java/Solution011.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package HOT100;
2+
3+
// 盛最多水的容器
4+
public class Solution011 {
5+
public int maxArea(int[] height) {
6+
int l = 0, r = height.length - 1, res = 0;
7+
while (l < r) {
8+
if (height[l] <= height[r]) {
9+
res = Math.max(res, height[l] * (r - l));
10+
l++;
11+
} else {
12+
res = Math.max(res, height[r] * (r - l));
13+
r--;
14+
}
15+
}
16+
return res;
17+
}
18+
}

热题HOT100_Java/Solution015.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package HOT100;
2+
3+
import java.util.*;
4+
5+
// 三数之和
6+
public class Solution015 {
7+
public List<List<Integer>> threeSum(int[] nums) {
8+
Set<List<Integer>> set = new HashSet<>();
9+
Arrays.sort(nums);
10+
int n = nums.length;
11+
12+
for (int i = 0; i < n - 2; i++) {
13+
int l = i + 1, r = n - 1;
14+
while (l < r) {
15+
if (nums[l] + nums[r] + nums[i] == 0) {
16+
set.add(Arrays.asList(nums[l], nums[r], nums[i]));
17+
l++;
18+
r--;
19+
} else if (nums[l] + nums[r] + nums[i] < 0) {
20+
l++;
21+
} else {
22+
r--;
23+
}
24+
}
25+
}
26+
return new ArrayList<>(set);
27+
}
28+
}

热题HOT100_Java/Solution017.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package HOT100;
2+
3+
import java.util.*;
4+
5+
// 电话号码的字母组合
6+
public class Solution017 {
7+
List<String> list = new ArrayList<>();
8+
Map<String, String> map = new HashMap<String, String>() {
9+
{
10+
put("2", "abc");
11+
put("3", "def");
12+
put("4", "ghi");
13+
put("5", "jkl");
14+
put("6", "mno");
15+
put("7", "pqrs");
16+
put("8", "tuv");
17+
put("9", "wxyz");
18+
}
19+
};
20+
21+
public List<String> letterCombinations(String digits) {
22+
if (digits.length() != 0)
23+
combine("", digits);
24+
return list;
25+
}
26+
27+
private void combine(String res, String digits) {
28+
if (digits.length() == 0) {
29+
list.add(res);
30+
} else {
31+
String str = map.get(digits.charAt(0) + "");
32+
for (int i = 0; i < str.length(); i++) {
33+
combine(res + str.charAt(i), digits.substring(1));
34+
}
35+
}
36+
}
37+
38+
}

热题HOT100_Java/Solution200.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package HOT100;
2+
3+
// 岛屿数量
4+
public class Solution200 {
5+
public int numIslands(char[][] grid) {
6+
if (grid.length == 0)
7+
return 0;
8+
9+
int m = grid.length, n = grid[0].length, res = 0;
10+
for (int i = 0; i < m; i++) {
11+
for (int j = 0; j < n; j++) {
12+
if (grid[i][j] == '1') {
13+
dfs(grid, i, j);
14+
res++;
15+
}
16+
}
17+
}
18+
return res;
19+
}
20+
21+
private void dfs(char[][] grid, int i, int j) {
22+
if (i >= 0 && i < grid.length && j >= 0 && j < grid[0].length && grid[i][j] == '1') {
23+
grid[i][j] = 0;
24+
dfs(grid, i + 1, j);
25+
dfs(grid, i - 1, j);
26+
dfs(grid, i, j + 1);
27+
dfs(grid, i, j - 1);
28+
}
29+
}
30+
31+
}

热题HOT100_Java/Solution236.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package HOT100;
2+
3+
4+
/*
5+
class TreeNode {
6+
int val;
7+
TreeNode left;
8+
TreeNode right;
9+
TreeNode(int x) { val = x; }
10+
}
11+
*/
12+
13+
14+
// 二叉树的最近公共祖先
15+
public class Solution236 {
16+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
17+
if (root == null || root == p || root == q)
18+
return root;
19+
TreeNode left = lowestCommonAncestor(root.left, p, q);
20+
TreeNode right = lowestCommonAncestor(root.right, p, q);
21+
if (left != null && right != null)
22+
return root;
23+
return left != null ? left : right;
24+
}
25+
}

热题HOT100_Java/Solution437.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package HOT100;
2+
3+
4+
/*
5+
class TreeNode {
6+
int val;
7+
TreeNode left;
8+
TreeNode right;
9+
TreeNode(int x) { val = x; }
10+
}
11+
*/
12+
13+
14+
// 路径总和
15+
public class Solution437 {
16+
public int pathSum(TreeNode root, int sum) {
17+
if (root == null)
18+
return 0;
19+
return dfs(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum);
20+
}
21+
22+
private int dfs(TreeNode root, int sum) {
23+
int res = 0;
24+
if (root == null)
25+
return res;
26+
if (root.val == sum)
27+
res += 1;
28+
res += dfs(root.left, sum - root.val);
29+
res += dfs(root.right, sum - root.val);
30+
return res;
31+
}
32+
}

热题HOT100_Java/Solution461.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package HOT100;
2+
3+
4+
// 汉明距离
5+
public class Solution461 {
6+
public int hammingDistance(int x, int y) {
7+
int num = x ^ y;
8+
int res = 0;
9+
while (num != 0) {
10+
res += num & 1;
11+
num >>= 1;
12+
}
13+
return res;
14+
}
15+
}

热题HOT100_Java/Solution617.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package HOT100;
2+
3+
4+
/*
5+
class TreeNode {
6+
int val;
7+
TreeNode left;
8+
TreeNode right;
9+
TreeNode(int x) { val = x; }
10+
}
11+
*/
12+
13+
14+
// 合并二叉树
15+
public class Solution617 {
16+
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
17+
if (t1 == null)
18+
return t2;
19+
if (t2 == null)
20+
return t1;
21+
return t1 != null ? t1 : t2;
22+
TreeNode root = new TreeNode(t1.val + t2.val);
23+
root.left = mergeTrees(t1.left, t2.left);
24+
root.right = mergeTrees(t1.right, t2.right);
25+
return root;
26+
}
27+
}

0 commit comments

Comments
 (0)