Skip to content

Commit cee086a

Browse files
committed
20180206
2 parents 97c58b1 + 29e5412 commit cee086a

File tree

237 files changed

+12412
-9561
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

237 files changed

+12412
-9561
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
test/target/*
2+
test/.idea/*
3+
/.idea/*
4+
25
test/.idea/*
36
test/out/*
47
*.xml
58
*.iml
6-
/.idea/*
79

README.md

Whitespace-only changes.

algorithm.iml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<module type="JAVA_MODULE" version="4">
3-
<component name="NewModuleRootManager" inherit-compiler-output="true">
4-
<exclude-output />
5-
<content url="file://$MODULE_DIR$">
6-
<sourceFolder url="file://$MODULE_DIR$/test/src" isTestSource="false" />
7-
</content>
8-
<orderEntry type="inheritedJdk" />
9-
<orderEntry type="sourceFolder" forTests="false" />
10-
</component>
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/test/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
1111
</module>

test/pom.xml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5-
<modelVersion>4.0.0</modelVersion>
6-
7-
<groupId>com.fay.bcsx</groupId>
8-
<artifactId>test</artifactId>
9-
<version>1.0-SNAPSHOT</version>
10-
11-
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.fay.bcsx</groupId>
8+
<artifactId>test</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
1212
</project>
Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
package leetcode.array;
2-
/*
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.
8-
*/
9-
public class BestTimeToBuyAndSellStock {
10-
public int maxProfit(int[] prices){
11-
if(prices==null||prices.length==0){
12-
return 0;
13-
}
14-
int max=Integer.MIN_VALUE;
15-
int min=Integer.MAX_VALUE;
16-
for(int i=0;i<prices.length;i++){
17-
if(prices[i]<min){
18-
min=prices[i];
19-
}
20-
max=Math.max(max,prices[i]-min);
21-
}
22-
return max;
23-
}
24-
public static void main(String[] args){
25-
int[] arr={1,2,3,4,5,6,7,8,9,13};
26-
27-
}
28-
29-
}
1+
package leetcode.array;
2+
/*
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.
8+
*/
9+
public class BestTimeToBuyAndSellStock {
10+
public int maxProfit(int[] prices){
11+
if(prices==null||prices.length==0){
12+
return 0;
13+
}
14+
int max=Integer.MIN_VALUE;
15+
int min=Integer.MAX_VALUE;
16+
for(int i=0;i<prices.length;i++){
17+
if(prices[i]<min){
18+
min=prices[i];
19+
}
20+
max=Math.max(max,prices[i]-min);
21+
}
22+
return max;
23+
}
24+
public static void main(String[] args){
25+
int[] arr={1,2,3,4,5,6,7,8,9,13};
26+
27+
}
28+
29+
}
Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
package leetcode.array;
2-
/*
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).
9-
*/
10-
11-
public class BestTimeToBuyAndSellStock2 {
12-
public int maxProfit(int[] prices) {
13-
if(prices==null||prices.length==0){
14-
return 0;
15-
}
16-
int max = 0;
17-
int temp = 0;
18-
for (int i = 1; i < prices.length; i++) {
19-
if ((temp=(prices[i] - prices[i - 1])) > 0) {
20-
max += temp;
21-
}
22-
}
23-
return max;
24-
}
25-
}
1+
package leetcode.array;
2+
/*
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).
9+
*/
10+
11+
public class BestTimeToBuyAndSellStock2 {
12+
public int maxProfit(int[] prices) {
13+
if(prices==null||prices.length==0){
14+
return 0;
15+
}
16+
int max = 0;
17+
int temp = 0;
18+
for (int i = 1; i < prices.length; i++) {
19+
if ((temp=(prices[i] - prices[i - 1])) > 0) {
20+
max += temp;
21+
}
22+
}
23+
return max;
24+
}
25+
}
Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,65 @@
1-
package leetcode.array;
2-
/*
3-
4-
Say you have an array for which the i th element is the price of a given stock
5-
on day i.
6-
7-
Design an algorithm to find the maximum profit. You may complete at most two
8-
transactions.
9-
10-
Note:
11-
You may not engage in multiple transactions at the same time (ie, you must sell
12-
the stock before you buy again).
13-
http://blog.csdn.net/acema/article/details/27317687
14-
*/
15-
16-
/*
17-
if(prices==null||prices.length<2) return 0;
18-
int[]pre=new int[prices.length];
19-
int []post=new int[prices.length];
20-
int min=prices[0];
21-
for(int i=1;i<prices.length;i++){
22-
min=Math.min(min,prices[i]);
23-
pre[i]=Math.max(pre[i-1],prices[i]-min);
24-
}
25-
int max=prices[prices.length-1];
26-
for(int i=prices.length-2;i>=0;i--){
27-
max=Math.max(max,prices[i]);
28-
post[i]=Math.max(post[i+1],max-prices[i]);
29-
}
30-
int maxProfit=0;
31-
for(int i=0;i<prices.length;i++){
32-
maxProfit=Math.max(maxProfit,pre[i]+post[i]);
33-
}
34-
return maxProfit;
35-
*/
36-
public class BestTimeToBuyAndSellStock3 {
37-
public int maxProfit(int[] prices){
38-
if(prices==null||prices.length<=1){
39-
return 0;
40-
}
41-
int max=0;
42-
int n=prices.length;
43-
for(int i=0;i<n;i++){
44-
int p=profit(prices,0,i)+profit(prices,i,n-1);
45-
max=Math.max(p,max);
46-
}
47-
return max;
48-
}
49-
public int profit(int[] prices,int start,int end){
50-
51-
int max=Integer.MIN_VALUE;
52-
//System.out.println(start);
53-
int min=prices[start];
54-
for(int i=start;i<=end;i++){
55-
max=Math.max(max,prices[i]-min);
56-
min=Math.min(min,prices[i]);
57-
}
58-
return max;
59-
}
60-
public static void main(String[] args){
61-
int[] prices={1,2};
62-
BestTimeToBuyAndSellStock3 test=new BestTimeToBuyAndSellStock3();
63-
System.out.println(test.maxProfit(prices));
64-
}
65-
}
1+
package leetcode.array;
2+
/*
3+
4+
Say you have an array for which the i th element is the price of a given stock
5+
on day i.
6+
7+
Design an algorithm to find the maximum profit. You may complete at most two
8+
transactions.
9+
10+
Note:
11+
You may not engage in multiple transactions at the same time (ie, you must sell
12+
the stock before you buy again).
13+
http://blog.csdn.net/acema/article/details/27317687
14+
*/
15+
16+
/*
17+
if(prices==null||prices.length<2) return 0;
18+
int[]pre=new int[prices.length];
19+
int []post=new int[prices.length];
20+
int min=prices[0];
21+
for(int i=1;i<prices.length;i++){
22+
min=Math.min(min,prices[i]);
23+
pre[i]=Math.max(pre[i-1],prices[i]-min);
24+
}
25+
int max=prices[prices.length-1];
26+
for(int i=prices.length-2;i>=0;i--){
27+
max=Math.max(max,prices[i]);
28+
post[i]=Math.max(post[i+1],max-prices[i]);
29+
}
30+
int maxProfit=0;
31+
for(int i=0;i<prices.length;i++){
32+
maxProfit=Math.max(maxProfit,pre[i]+post[i]);
33+
}
34+
return maxProfit;
35+
*/
36+
public class BestTimeToBuyAndSellStock3 {
37+
public int maxProfit(int[] prices){
38+
if(prices==null||prices.length<=1){
39+
return 0;
40+
}
41+
int max=0;
42+
int n=prices.length;
43+
for(int i=0;i<n;i++){
44+
int p=profit(prices,0,i)+profit(prices,i,n-1);
45+
max=Math.max(p,max);
46+
}
47+
return max;
48+
}
49+
public int profit(int[] prices,int start,int end){
50+
51+
int max=Integer.MIN_VALUE;
52+
//System.out.println(start);
53+
int min=prices[start];
54+
for(int i=start;i<=end;i++){
55+
max=Math.max(max,prices[i]-min);
56+
min=Math.min(min,prices[i]);
57+
}
58+
return max;
59+
}
60+
public static void main(String[] args){
61+
int[] prices={1,2};
62+
BestTimeToBuyAndSellStock3 test=new BestTimeToBuyAndSellStock3();
63+
System.out.println(test.maxProfit(prices));
64+
}
65+
}
Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
1-
package leetcode.array;
2-
3-
/**
4-
* Created by fay on 2017/12/8.
5-
* Given an unsorted integer array, find the first missing positive integer.
6-
For example,
7-
Given[1,2,0]return3,
8-
and[3,4,-1,1]return2.
9-
Your algorithm should run in O(n) time and uses constant space.
10-
11-
*/
12-
public class FirstMissingPositive {
13-
public int firstMissingPositive(int[] A) {
14-
if(A.length==0){
15-
return 1;
16-
}
17-
int n=A.length,i=0;
18-
while(i<n){
19-
if(A[i]!=i+1&&(A[i]>0&&A[i]<=n)&&A[i]!=A[A[i]-1]){
20-
int temp=A[A[i]-1];
21-
A[A[i]-1]=A[i];
22-
A[i]=temp;
23-
}else{
24-
i++;
25-
}
26-
27-
}
28-
for(i=0;i<n;i++){
29-
if(A[i]!=i+1){
30-
return i+1;
31-
}
32-
}
33-
return n+1;
34-
}
35-
public static void main(String[] args){
36-
FirstMissingPositive test=new FirstMissingPositive();
37-
int[] a={3,4,-1,1};
38-
test.firstMissingPositive(a);
39-
}
40-
}
1+
package leetcode.array;
2+
3+
/**
4+
* Created by fay on 2017/12/8.
5+
* Given an unsorted integer array, find the first missing positive integer.
6+
For example,
7+
Given[1,2,0]return3,
8+
and[3,4,-1,1]return2.
9+
Your algorithm should run in O(n) time and uses constant space.
10+
11+
*/
12+
public class FirstMissingPositive {
13+
public int firstMissingPositive(int[] A) {
14+
if(A.length==0){
15+
return 1;
16+
}
17+
int n=A.length,i=0;
18+
while(i<n){
19+
if(A[i]!=i+1&&(A[i]>0&&A[i]<=n)&&A[i]!=A[A[i]-1]){
20+
int temp=A[A[i]-1];
21+
A[A[i]-1]=A[i];
22+
A[i]=temp;
23+
}else{
24+
i++;
25+
}
26+
27+
}
28+
for(i=0;i<n;i++){
29+
if(A[i]!=i+1){
30+
return i+1;
31+
}
32+
}
33+
return n+1;
34+
}
35+
public static void main(String[] args){
36+
FirstMissingPositive test=new FirstMissingPositive();
37+
int[] a={3,4,-1,1};
38+
test.firstMissingPositive(a);
39+
}
40+
}

0 commit comments

Comments
 (0)