Skip to content

Commit 406fa4a

Browse files
committed
20181027
1 parent 07fd25a commit 406fa4a

40 files changed

+798
-359
lines changed

algorithm.iml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<component name="NewModuleRootManager" inherit-compiler-output="true">
44
<exclude-output />
55
<content url="file://$MODULE_DIR$">
6-
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
6+
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
77
</content>
88
<orderEntry type="inheritedJdk" />
99
<orderEntry type="sourceFolder" forTests="false" />
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,39 @@
11
package leetcode.array;
22

3+
/**
4+
* 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。
5+
* 在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。
6+
* 找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
7+
* 说明:你不能倾斜容器,且 n 的值至少为 2。
8+
*/
39
public class _11_MaxArea {
10+
//暴力求解1
11+
public int maxArea(int[] height) {
12+
int max = Integer.MIN_VALUE;
13+
for (int i = 0; i < height.length; i++) {
14+
for (int j = i + 1; j < height.length; j++) {
15+
int h = height[i] > height[j] ? height[j] : height[i];
16+
if (h * (j - i) > max)
17+
max = h * (j - i);
18+
}
19+
}
20+
return max;
21+
}
22+
23+
//
24+
public int maxArea1(int[] height) {
25+
int max = Integer.MIN_VALUE;
26+
for (int i = 0, j = height.length - 1; i < j; ) {
27+
if (height[i] > height[j]) {
28+
max = Math.max(max, height[j] * (j - i));
29+
j--;
30+
31+
} else {
32+
max = Math.max(max, height[i] * (j - i));
33+
i++;
34+
}
35+
}
36+
return max;
37+
}
438
}
39+

src/main/java/leetcode/array/_121_BestTimeToBuyAndSellStock.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
package leetcode.array;
22
/*
3-
Say you have an array for which the i th element is the price of a given stock
4-
on day i.
5-
If you were only permitted to complete at most one transaction
6-
(ie, buy one and sell one share of the stock),
7-
design an algorithm to find the maximum profit.
3+
给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。
4+
5+
如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润。
6+
7+
注意你不能在买入股票前卖出股票。
8+
9+
示例 1:
10+
11+
输入: [7,1,5,3,6,4]
12+
输出: 5
13+
解释: 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
14+
注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格。
15+
示例 2:
16+
17+
输入: [7,6,4,3,1]
18+
输出: 0
19+
解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。
820
*/
9-
public class BestTimeToBuyAndSellStock {
21+
public class _121_BestTimeToBuyAndSellStock {
1022

1123
public int maxProfit(int[] prices){
1224
if(prices==null||prices.length==0){

src/main/java/leetcode/array/_122_BestTimeToBuyAndSellStock2.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
11
package leetcode.array;
22
/*
3-
Say you have an array for which the i th element is the price of a given stock
4-
on day i.
5-
Design an algorithm to find the maximum profit. You may complete as many
6-
transactions as you like (ie, buy one and sell one share of the stock multiple
7-
times). However, you may not engage in multiple transactions at the same time
8-
(ie, you must sell the stock before you buy again).
3+
给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。
4+
5+
设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。
6+
7+
注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
8+
9+
示例 1:
10+
11+
输入: [7,1,5,3,6,4]
12+
输出: 7
13+
解释: 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
14+
随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。
15+
示例 2:
16+
17+
输入: [1,2,3,4,5]
18+
输出: 4
19+
解释: 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
20+
注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。
21+
因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。
22+
示例 3:
23+
24+
输入: [7,6,4,3,1]
25+
输出: 0
26+
解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。
27+
928
*/
1029

11-
public class BestTimeToBuyAndSellStock2 {
30+
public class _122_BestTimeToBuyAndSellStock2 {
31+
//贪心
1232
public int maxProfit(int[] prices) {
1333
if(prices==null||prices.length==0){
1434
return 0;

src/main/java/leetcode/array/_154_FindMinmumInRotateSortedArray2.java

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
package leetcode.array;
22

33
/**
4-
* Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
5-
* <p>
6-
* (i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).
7-
* <p>
8-
* Find the minimum element.
9-
* <p>
10-
* The array may contain duplicates.
11-
* <p>
12-
* Example 1:
13-
* <p>
14-
* Input: [1,3,5]
15-
* Output: 1
16-
* Example 2:
17-
* <p>
18-
* Input: [2,2,2,0,1]
19-
* Output: 0
20-
* Note:
21-
* <p>
22-
* This is a follow up problem to Find Minimum in Rotated Sorted Array.
23-
* Would allow duplicates affect the run-time complexity? How and why?
4+
假设按照升序排序的数组在预先未知的某个点上进行了旋转。
5+
6+
( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。
7+
8+
请找出其中最小的元素。
9+
10+
注意数组中可能存在重复的元素。
11+
12+
示例 1:
13+
14+
输入: [1,3,5]
15+
输出: 1
16+
示例 2:
17+
18+
输入: [2,2,2,0,1]
19+
输出: 0
2420
*/
2521
public class _154_FindMinmumInRotateSortedArray2 {
22+
2623
public int findMin(int[] nums) {
2724
if (nums == null || nums.length == 0) return -1;
2825
int mid = 0;

src/main/java/leetcode/array/_15_Sum3.java

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,73 +2,75 @@
22

33
import java.util.ArrayList;
44
import java.util.Arrays;
5+
import java.util.List;
56

6-
/* Given an array S of n integers, are there elements a, b, c in S such that a +
7-
b + c = 0? Find all unique triplets in the array which gives the sum of zero.
7+
/**
8+
* 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,
9+
* 使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。
10+
* <p>
11+
* 注意:答案中不可以包含重复的三元组。
12+
* <p>
13+
* 例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],
14+
* <p>
15+
* 满足要求的三元组集合为:
16+
* [
17+
* [-1, 0, 1],
18+
* [-1, -1, 2]
19+
* ]
20+
*/
21+
public class _15_Sum3 {
822

9-
Note:
10-
11-
Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
12-
The solution set must not contain _51_duplicate triplets.
13-
14-
15-
For example, given array S = {-1 0 1 2 -1 -4},
16-
17-
A solution set is:
18-
(-1, 0, 1)
19-
(-1, -1, 2)
20-
*/
21-
public class Sum3 {
22-
public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
23-
ArrayList<ArrayList<Integer>> result=new ArrayList<>();
24-
Arrays.sort(num);
25-
for(int k=0;k<num.length;k++){
26-
int target=0-num[k];
27-
int i=k+1,j=num.length-1;
28-
if (k != 0 && num[k] == num[k - 1]) {
23+
public List<List<Integer>> threeSum(int[] nums) {
24+
List<List<Integer>> result = new ArrayList<>();
25+
Arrays.sort(nums);
26+
for (int k = 0; k < nums.length; k++) {
27+
int target = 0 - nums[k];
28+
int i = k + 1, j = nums.length - 1;
29+
if (k != 0 && nums[k] == nums[k - 1]) {
2930
continue;
3031
}
31-
while(i<j){
32-
if((num[i]+num[j])==target){
33-
ArrayList<Integer> res=new ArrayList<>();
34-
res.add(num[k]);
35-
res.add(num[i]);
36-
res.add(num[j]);
32+
while (i < j) {
33+
if ((nums[i] + nums[j]) == target) {
34+
ArrayList<Integer> res = new ArrayList<>();
35+
res.add(nums[k]);
36+
res.add(nums[i]);
37+
res.add(nums[j]);
3738
result.add(res);
38-
while(i<j&&num[i]==num[i+1]){
39+
while (i < j && nums[i] == nums[i + 1]) {
3940
i++;
4041
}
41-
while(i<j&&num[j]==num[j-1]){
42+
while (i < j && nums[j] == nums[j - 1]) {
4243
j--;
4344
}
4445
i++;
4546
j--;
46-
}else if((num[i]+num[j])<target){
47+
} else if ((nums[i] + nums[j]) < target) {
4748
i++;
48-
}else{
49+
} else {
4950
j--;
5051
}
5152

5253
}
5354

5455
}
55-
return result;
56+
return result;
5657
}
57-
public static void main(String[] args){
58-
Sum3 test=new Sum3();
59-
int[] num={-1, 0 ,1 ,2 ,-1 ,-4};
60-
int[] num1={0,0,0,0};
61-
int[] num2={-2,0,1,1,2};
62-
ArrayList<ArrayList<Integer>> result=test.threeSum(num);
58+
59+
public static void main(String[] args) {
60+
_15_Sum3 test = new _15_Sum3();
61+
int[] num = {-1, 0, 1, 2, -1, -4};
62+
int[] num1 = {0, 0, 0, 0};
63+
int[] num2 = {-2, 0, 1, 1, 2};
64+
List<List<Integer>> result = test.threeSum(num);
6365
/*for(ArrayList<Integer> res:result){
6466
for(int a:res){
6567
System.out.print(a);
6668
}
6769
System.out.println();
6870
}*/
69-
ArrayList<ArrayList<Integer>> result1=test.threeSum(num2);
70-
for(ArrayList<Integer> res:result1){
71-
for(int a:res){
71+
List<List<Integer>> result1 = test.threeSum(num2);
72+
for (List<Integer> res : result1) {
73+
for (int a : res) {
7274
System.out.print(a);
7375
}
7476
System.out.println();

src/main/java/leetcode/array/_16_SumClosest.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,34 @@
22

33
import java.util.Arrays;
44

5-
/* Given an array S of n integers, find three integers in S such that the sum is
6-
closest to a given number, target. Return the sum of the three integers. You may
7-
assume that each input would have exactly one solution.
5+
/**
6+
* 给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。
7+
* 返回这三个数的和。假定每组输入只存在唯一答案。
88
9-
For example, given array S = {-1 2 1 -4}, and target = 1.
9+
例如,给定数组 nums = [-1,2,1,-4], 和 target = 1.
1010
11-
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
12-
*/
13-
public class SumClosest {
14-
public int threeSumClosest(int[] num, int target) {
15-
int closet=num[0]+num[1]+num[2];
11+
与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2).
12+
*/
13+
public class _16_SumClosest {
14+
15+
public int threeSumClosest(int[] nums, int target) {
16+
int closet=nums[0]+nums[1]+nums[2];
1617
int diff=Math.abs(closet-target);
1718
int sum=0;
18-
Arrays.sort(num);
19-
for(int i=0;i<num.length-2;i++){
19+
Arrays.sort(nums);
20+
for(int i=0;i<nums.length-2;i++){
2021
int left=i+1;
21-
int right=num.length-1;
22+
int right=nums.length-1;
2223
while(left<right){
23-
sum=num[i]+num[left]+num[right];
24+
sum=nums[i]+nums[left]+nums[right];
2425
int newDiff=Math.abs(sum-target);
2526
if(newDiff<diff){
2627
diff=newDiff;
2728
closet=sum;
2829
}
29-
if(sum<closet){
30+
if(sum==closet){
31+
return closet;
32+
}else if(sum<closet){
3033
left++;
3134
}else{
3235
right--;

0 commit comments

Comments
 (0)