Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6003d88

Browse files
author
haotf
committed
三数之和
1 parentf73d6c8 commit6003d88

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

‎11.盛最多水的容器.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* @lc app=leetcode.cn id=11 lang=java
3+
*
4+
* [11] 盛最多水的容器
5+
*/
6+
7+
// @lc code=start
8+
classSolution {
9+
publicintmaxArea(int[]height) {
10+
intmax =0;
11+
intleft =0;
12+
intright =height.length -1;
13+
14+
while (left <right) {
15+
max =Math.max(max, (right -left) *Math.min(height[left],height[right]));
16+
if (height[left] <height[right]) {
17+
left++;
18+
}else {
19+
right--;
20+
}
21+
}
22+
returnmax;
23+
}
24+
}
25+
// @lc code=end

‎15.三数之和.java‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
importjava.util.ArrayList;
2+
importjava.util.Arrays;
3+
importjava.util.Comparator;
4+
importjava.util.List;
5+
6+
/*
7+
* @lc app=leetcode.cn id=15 lang=java
8+
*
9+
* [15] 三数之和
10+
*/
11+
12+
// @lc code=start
13+
classSolution {
14+
publicList<List<Integer>>threeSum(int[]nums) {
15+
Arrays.sort(nums);
16+
returnnSum(nums,0,3,0);
17+
}
18+
19+
privateList<List<Integer>>nSum(int[]nums,intstart,intn,inttarget) {
20+
List<List<Integer>>res =newArrayList<>();
21+
if (nums.length <n ||n <2)
22+
returnres;
23+
24+
if (n ==2) {
25+
intleft =start;
26+
intright =nums.length -1;
27+
while (left <right) {
28+
intsum =nums[left] +nums[right];
29+
intlow =nums[left];
30+
inthigh =nums[right];
31+
if (sum <target) {
32+
while (nums[left] ==low &&left <right)
33+
left++;
34+
}elseif (sum >target) {
35+
while (nums[right] ==high &&left <right)
36+
right--;
37+
}else {
38+
List<Integer>rList =newArrayList<>();
39+
rList.add(nums[left]);
40+
rList.add(nums[right]);
41+
res.add(rList);
42+
while (nums[left] ==low &&left <right)
43+
left++;
44+
while (nums[right] ==high &&left <right)
45+
right--;
46+
}
47+
}
48+
}else {
49+
for (inti =start;i <nums.length;i++) {
50+
List<List<Integer>>result =nSum(nums,i +1,n -1,target -nums[i]);
51+
52+
for (List<Integer>list :result) {
53+
list.add(0,nums[i]);
54+
res.add(list);
55+
}
56+
while (i <nums.length -1 &&nums[i] ==nums[i +1])
57+
i++;
58+
}
59+
}
60+
returnres;
61+
}
62+
}
63+
// @lc code=end

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp