File tree Expand file tree Collapse file tree 4 files changed +45
-23
lines changed Expand file tree Collapse file tree 4 files changed +45
-23
lines changed Original file line number Diff line number Diff line change 11
2-
32👉 推荐 [ 在线阅读] ( http://programmercarl.com/ ) (Github在国内访问经常不稳定)
43👉 推荐 [ Gitee同步] ( https://gitee.com/programmercarl/leetcode-master )
54
Original file line number Diff line number Diff line change @@ -1287,23 +1287,23 @@ java代码:
12871287``` java
12881288class Solution {
12891289 public List<Integer > largestValues (TreeNode root ) {
1290- List< Integer > retVal = new ArrayList< Integer > ();
1291- Queue< TreeNode > tmpQueue = new LinkedList< TreeNode > ();
1292- if (root != null ) tmpQueue . add(root);
1293-
1294- while (tmpQueue . size() != 0 ){
1295- int size = tmpQueue . size( );
1296- List< Integer > lvlVals = new ArrayList< Integer > ();
1297- for ( int index = 0 ; index < size; index ++ ){
1298- TreeNode node = tmpQueue . poll ();
1299- lvlVals . add( node. val );
1300- if (node . left != null ) tmpQueue . add( node. left );
1301- if (node. right != null ) tmpQueue . add (node. right );
1302- }
1303- retVal . add( Collections . max(lvlVals));
1304- }
1305-
1306- return retVal ;
1290+ if (root == null ){
1291+ return Collections . emptyList ();
1292+ }
1293+ List< Integer > result = new ArrayList ();
1294+ Queue< TreeNode > queue = new LinkedList ();
1295+ queue . offer(root );
1296+ while ( ! queue . isEmpty()){
1297+ int max = Integer . MIN_VALUE ;
1298+ for ( int i = queue . size (); i > 0 ; i -- ){
1299+ TreeNode node = queue . poll( );
1300+ max = Math . max(max, node. val );
1301+ if (node. left != null ) queue . offer (node. left );
1302+ if (node . right != null ) queue . offer(node . right);
1303+ }
1304+ result . add(max);
1305+ }
1306+ return result ;
13071307 }
13081308}
13091309```
Original file line number Diff line number Diff line change @@ -205,6 +205,29 @@ class Solution {
205205 }
206206}
207207```
208+ ``` java
209+ // 另一种解题思路
210+ class Solution {
211+ public int maxProfit (int [] prices ) {
212+ int [][] dp = new int [prices. length + 1 ][2 ];
213+ dp[1 ][0 ] = - prices[0 ];
214+
215+ for (int i = 2 ; i <= prices. length; i++ ) {
216+ /*
217+ dp[i][0] 第i天未持有股票收益;
218+ dp[i][1] 第i天持有股票收益;
219+ 情况一:第i天是冷静期,不能以dp[i-1][1]购买股票,所以以dp[i - 2][1]买股票,没问题
220+ 情况二:第i天不是冷静期,理论上应该以dp[i-1][1]购买股票,但是第i天不是冷静期说明,第i-1天没有卖出股票,
221+ 则dp[i-1][1]=dp[i-2][1],所以可以用dp[i-2][1]买股票,没问题
222+ */
223+ dp[i][0 ] = Math . max(dp[i - 1 ][0 ], dp[i - 2 ][1 ] - prices[i - 1 ]);
224+ dp[i][1 ] = Math . max(dp[i - 1 ][1 ], dp[i - 1 ][0 ] + prices[i - 1 ]);
225+ }
226+
227+ return dp[prices. length][1 ];
228+ }
229+ }
230+ ```
208231
209232Python:
210233
Original file line number Diff line number Diff line change @@ -251,14 +251,14 @@ Python:
251251``` python
252252class Solution :
253253 def canPartition (self , nums : List[int ]) -> bool :
254- taraget = sum (nums)
255- if taraget % 2 == 1 : return False
256- taraget //= 2
254+ target = sum (nums)
255+ if target % 2 == 1 : return False
256+ target //= 2
257257 dp = [0 ] * 10001
258258 for i in range (len (nums)):
259- for j in range (taraget , nums[i] - 1 , - 1 ):
259+ for j in range (target , nums[i] - 1 , - 1 ):
260260 dp[j] = max (dp[j], dp[j - nums[i]] + nums[i])
261- return taraget == dp[taraget ]
261+ return target == dp[target ]
262262```
263263Go:
264264``` go
You can’t perform that action at this time.
0 commit comments