Skip to content

Commit cf93800

Browse files
committed
新增5题,累积33题
1 parent 8491676 commit cf93800

File tree

6 files changed

+271
-1
lines changed

6 files changed

+271
-1
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.cjl.leetcode;
2+
3+
import com.cjl.common.TreeNode;
4+
5+
import java.util.LinkedList;
6+
import java.util.List;
7+
8+
/*
9+
103. 二叉树的锯齿形层序遍历
10+
问题描述:
11+
给定一个二叉树,返回其节点值的锯齿形层序遍历。
12+
(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。
13+
例如:
14+
给定二叉树[3,9,20,null,null,15,7],
15+
3
16+
/ \
17+
9 20
18+
/ \
19+
15 7
20+
返回锯齿形层序遍历如下:
21+
[
22+
[3],
23+
[20,9],
24+
[15,7]
25+
]
26+
*/
27+
public class Question_103 {
28+
29+
// 时间复杂度是O(N),空间复杂度是O(1)
30+
public List<List<Integer>> solution1(TreeNode root) {
31+
// LinkedList插入的时候时间复杂度O(1),ArrayList插入时间复杂度O(n-k)
32+
List<List<Integer>> res = new LinkedList<>();
33+
dfs(root, res, 0);
34+
return res;
35+
}
36+
37+
private void dfs(TreeNode root, List<List<Integer>> res, int level) {
38+
if(root == null) {
39+
return;
40+
}
41+
42+
if(res.size() == level) {
43+
res.add(new LinkedList<>());
44+
}
45+
46+
// 判断层数的奇偶数,奇数从右到左添加,偶数从左到右添加
47+
if((level & 1) == 1) {
48+
res.get(level).add(0,root.val);
49+
}else {
50+
res.get(level).add(root.val);
51+
}
52+
53+
dfs(root.left, res, level + 1);
54+
dfs(root.right, res, level + 1);
55+
}
56+
}

src/com/cjl/leetcode/Question_124.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class Question_124 {
2525

2626
// 时间复杂度是O(N),空间复杂度是O(N)
2727
int result = Integer.MIN_VALUE;
28-
public int maxPathSum(TreeNode root) {
28+
public int solution1(TreeNode root) {
2929
dfs(root);
3030
return result;
3131
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.cjl.leetcode;
2+
3+
import com.cjl.common.TreeNode;
4+
5+
import java.util.Arrays;
6+
7+
/*
8+
135. 分发糖果
9+
问题描述:
10+
老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分。
11+
你需要按照以下要求,帮助老师给这些孩子分发糖果:
12+
每个孩子至少分配到 1 个糖果。
13+
评分更高的孩子必须比他两侧的邻位孩子获得更多的糖果。
14+
那么这样下来,老师至少需要准备多少颗糖果呢?
15+
示例1:
16+
输入:[1,0,2]
17+
输出:5
18+
解释:你可以分别给这三个孩子分发 2、1、2 颗糖果。
19+
示例2:
20+
输入:[1,2,2]
21+
输出:4
22+
解释:你可以分别给这三个孩子分发 1、2、1 颗糖果。
23+
第三个孩子只得到 1 颗糖果,这已满足上述两个条件。
24+
*/
25+
public class Question_135 {
26+
27+
28+
public int candy(int[] ratings) {
29+
int[] res = new int[ratings.length];
30+
31+
// 赋值
32+
Arrays.fill(res, 1);
33+
34+
// 从左到右
35+
for (int i = 1; i < ratings.length; i++) {
36+
if(ratings[i] > ratings[i-1]) {
37+
res[i] = res[i-1] + 1;
38+
}
39+
}
40+
41+
// 从右到左
42+
for (int i = ratings.length - 2; i >= 0; i--) {
43+
if(ratings[i] > ratings[i + 1]) {
44+
res[i] = Math.max(res[i], res[i + 1] + 1);
45+
}
46+
}
47+
48+
int count = 0;
49+
for (int re : res) {
50+
count += re;
51+
}
52+
53+
return count;
54+
}
55+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.cjl.leetcode;
2+
3+
import com.sun.jmx.remote.internal.ArrayQueue;
4+
5+
import java.util.*;
6+
7+
/*
8+
32. 最长有效括号
9+
问题描述:
10+
给你一个只包含 '(' 和 ')' 的字符串,找出最长有效(格式正确且连续)括号子串的长度。
11+
示例 1:
12+
输入:s = "(()"
13+
输出:2
14+
解释:最长有效括号子串是 "()"
15+
示例 2:
16+
输入:s = ")()())"
17+
输出:4
18+
解释:最长有效括号子串是 "()()"
19+
示例 3:
20+
输入:s = ""
21+
输出:0
22+
提示:
23+
0 <= s.length <= 3 * 10^4
24+
s[i] 为 '(' 或 ')'
25+
*/
26+
public class Question_32 {
27+
28+
// 时间复杂度是O(N),空间复杂度是O(1)
29+
public int solution1(String s) {
30+
if (s == null || s.length() == 0) {
31+
return 0;
32+
}
33+
Deque<Integer> stack = new ArrayDeque<>();
34+
stack.push(-1);
35+
int res = 0;
36+
for (int i = 0; i < s.length(); i++) {
37+
if(s.charAt(i) == '('){
38+
stack.push(i);
39+
}else{
40+
stack.pop();
41+
if(stack.isEmpty()) {
42+
stack.push(i);
43+
}else{
44+
res = Math.max(res, i - stack.peek());
45+
}
46+
}
47+
}
48+
49+
return res;
50+
}
51+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.cjl.leetcode;
2+
3+
/*
4+
72. 编辑距离
5+
问题描述:
6+
给你两个单词word1 和word2,请你计算出将word1转换成word2 所使用的最少操作数。
7+
你可以对一个单词进行如下三种操作:
8+
插入一个字符
9+
删除一个字符
10+
替换一个字符
11+
示例1:
12+
输入:word1 = "horse", word2 = "ros"
13+
输出:3
14+
解释:
15+
horse -> rorse (将 'h' 替换为 'r')
16+
rorse -> rose (删除 'r')
17+
rose -> ros (删除 'e')
18+
示例2:
19+
输入:word1 = "intention", word2 = "execution"
20+
输出:5
21+
解释:
22+
intention -> inention (删除 't')
23+
inention -> enention (将 'i' 替换为 'e')
24+
enention -> exention (将 'n' 替换为 'x')
25+
exention -> exection (将 'n' 替换为 'c')
26+
exection -> execution (插入 'u')
27+
*/
28+
public class Question_72 {
29+
30+
public int solution1(String word1, String word2) {
31+
int len1 = word1.length();
32+
int len2 = word2.length();
33+
34+
int[][] dp = new int[len1 + 1][len2 + 1];
35+
36+
// 插入操作
37+
for (int i = 1; i <= len1; i++) {
38+
dp[i][0] = dp[i - 1][0] + 1;
39+
}
40+
41+
// 删除操作
42+
for (int i = 1; i <= len2; i++) {
43+
dp[0][i] = dp[0][i - 1] + 1;
44+
}
45+
46+
for (int i = 1; i <= len1; i++) {
47+
for (int j = 1; j <= len2; j++) {
48+
if(word1.charAt(i - 1) == word2.charAt(j - 1)) {
49+
dp[i][j] = dp[i - 1][j - 1];
50+
}else{
51+
// 删除操作:dp[i - 1][j]
52+
// 增加操作:dp[i][j - 1]
53+
// 替换操作:dp[i - 1][j - 1]
54+
dp[i][j] = Math.min(dp[i - 1][j], Math.min(dp[i - 1][j - 1], dp[i][j - 1])) + 1;
55+
}
56+
}
57+
}
58+
59+
return dp[len1][len2];
60+
}
61+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.cjl.leetcode;
2+
3+
import com.cjl.common.ListNode;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
/*
9+
92. 反转链表 II
10+
问题描述:
11+
给你单链表的头指针 head 和两个整数 left 和 right ,其中 left <= right 。
12+
请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表 。
13+
示例 1:
14+
输入:head = [1,2,3,4,5], left = 2, right = 4
15+
输出:[1,4,3,2,5]
16+
示例 2:
17+
输入:head = [5], left = 1, right = 1
18+
输出:[5]
19+
提示:
20+
链表中节点数目为 n
21+
1 <= n <= 500
22+
-500 <= Node.val <= 500
23+
1 <= left <= right <= n
24+
*/
25+
public class Question_92 {
26+
27+
// 时间复杂度是O(N),空间复杂度是O(1)
28+
public ListNode solution1(ListNode head, int left, int right) {
29+
ListNode result = new ListNode(0);
30+
result.next = head;
31+
ListNode pre = result;
32+
for (int i = 1; i < left; i++) {
33+
pre = pre.next;
34+
}
35+
36+
head = pre.next;
37+
38+
for (int i = left; i < right; i++) {
39+
ListNode next = head.next;
40+
head.next = next.next;
41+
next.next = pre.next;
42+
pre.next = next;
43+
}
44+
45+
return result.next;
46+
}
47+
}

0 commit comments

Comments
 (0)