Skip to content

Commit 5abd359

Browse files
committed
新增2题,累积11题
1 parent 733379b commit 5abd359

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.cjl.leetcode;
2+
3+
import com.cjl.common.ListNode;
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
/*
10+
15. 三数之和
11+
问题描述:
12+
给你一个包含 n 个整数的数组nums,判断nums中是否存在三个元素 a,b,c ,使得a + b + c = 0 ?
13+
请你找出所有和为 0 且不重复的三元组。
14+
注意:
15+
答案中不可以包含重复的三元组。
16+
示例 1:
17+
输入:nums = [-1,0,1,2,-1,-4]
18+
输出:[[-1,-1,2],[-1,0,1]]
19+
示例 2:
20+
输入:nums = []
21+
输出:[]
22+
示例 3:
23+
输入:nums = [0]
24+
输出:[]
25+
提示:
26+
0 <= nums.length <= 3000
27+
-10^5 <= nums[i] <= 10^5
28+
*/
29+
public class Question_15 {
30+
31+
// 时间复杂度O(N^2),空间复杂度O(1)
32+
public List<List<Integer>> solution1(int[] nums) {
33+
if(nums == null || nums.length < 3) {
34+
return new ArrayList<>();
35+
}
36+
List<List<Integer>> res = new ArrayList<>();
37+
int len = nums.length;
38+
Arrays.sort(nums);
39+
for (int i = 0; i < len; i++) {
40+
if(nums[i] > 0){
41+
break;
42+
}
43+
if(i > 0 && nums[i] == nums[i - 1]){
44+
continue;
45+
}
46+
int left = i + 1;
47+
int right = len - 1;
48+
while(left < right) {
49+
int sum = nums[i] + nums[left] + nums[right];
50+
if(sum == 0){
51+
res.add(Arrays.asList(nums[i],nums[left],nums[right]));
52+
while(left < right && nums[left] == nums[left + 1]){
53+
left++;
54+
}
55+
while(left < right && nums[right] == nums[right - 1]){
56+
right--;
57+
}
58+
left++;
59+
right--;
60+
}else if(sum < 0){
61+
left++;
62+
}else{
63+
right--;
64+
}
65+
}
66+
}
67+
return res;
68+
}
69+
}
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.ListNode;
4+
5+
/*
6+
21. 合并两个有序链表
7+
问题描述:
8+
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
9+
示例 1:
10+
输入:l1 = [1,2,4], l2 = [1,3,4]
11+
输出:[1,1,2,3,4,4]
12+
示例 2:
13+
输入:l1 = [], l2 = []
14+
输出:[]
15+
示例 3:
16+
输入:l1 = [], l2 = [0]
17+
输出:[0]
18+
提示:
19+
两个链表的节点数目范围是 [0, 50]
20+
-100 <= Node.val <= 100
21+
l1 和 l2 均按 非递减顺序 排列
22+
*/
23+
public class Question_21 {
24+
25+
// 时间复杂度O(M+N),空间复杂度O(1)
26+
public ListNode solution1(ListNode l1, ListNode l2) {
27+
ListNode res = new ListNode(0);
28+
ListNode cur = res;
29+
while(l1 != null && l2 != null){
30+
if(l1.val > l2.val){
31+
cur.next = l2;
32+
l2 = l2.next;
33+
}else {
34+
cur.next = l1;
35+
l1 = l1.next;
36+
}
37+
cur = cur.next;
38+
}
39+
cur.next = l1 != null ? l1 : l2;
40+
return res.next;
41+
}
42+
43+
// 递归法
44+
// 时间复杂度O(M+N),空间复杂度O(1)
45+
public ListNode solution2(ListNode l1, ListNode l2) {
46+
if(l1 == null || l2 == null){
47+
return l1 == null ? l2: l1;
48+
}
49+
if(l1.val <= l2.val){
50+
l1.next = solution2(l1.next, l2);
51+
return l1;
52+
}
53+
l2.next = solution2(l1, l2.next);
54+
return l2;
55+
}
56+
}

0 commit comments

Comments
 (0)