Skip to content

Commit 438d907

Browse files
authored
Merge pull request #1 from hhy1995/hhy1995-patch-1
Create OCEAN_he95.md
2 parents c31faf3 + 41996b1 commit 438d907

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

2019.11.24/OCEAN_he95.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int maxProfit(int[] prices) {
3+
int max = 0;
4+
//如果只有一个元素,直接返回0
5+
if(prices.length==1){
6+
return 0;
7+
}
8+
//如果有两个元素,并且第二个比第一个大的话,返回第二个减去第一个的值
9+
if(prices.length==2 && (prices[1]>prices[0])){
10+
return prices[1]-prices[0];
11+
}
12+
//三个以上的元素就要考虑对于max的更新
13+
for(int i=0;i<prices.length;i++){
14+
for(int j=i+1;j<prices.length;j++){
15+
if((prices[j]-prices[i])>max){
16+
max = prices[j]- prices[i];
17+
}
18+
}
19+
}
20+
return max;
21+
}
22+
}

0 commit comments

Comments
 (0)