前言
本文隶属于专栏《LeetCode 刷题汇总》,该专栏为笔者原创,引用请注明来源,不足和错误之处请在评论区帮忙指出,谢谢!
本专栏目录结构请见LeetCode 刷题汇总
Github 配套工程
正文
幕布
121. 买卖股票的最佳时机
题解
官方题解
price < cur,price - cur > max
package com.shockang.study.algorithm.java.leetcode.leetcode101_200.leetcode121.solution1;
/**
* 比我小,那你就成为我,差值比我大,那最大利润就找到了~
*
* @author Shockang
*/
public class Solution {
public int maxProfit(int[] prices) {
if (prices.length == 1) return 0;
int cur = prices[0], max = 0;
for (int price : prices) {
if (price < cur) {
cur = price;
}
if (price - cur > max) {
max = price - cur;
}
}
return max;
}
}
122. 买卖股票的最佳时机 II
题解
官方题解
动态规划
package com.shockang.study.algorithm.java.leetcode.leetcode101_200.leetcode122.solution1;
/**
* 动态规划
*
* @author Shockang
*/
public class Solution {
public int maxProfit(int[] prices) {
int n = prices.length;
int[][] dp = new int[n][2];
dp[0][1] = -prices[0];
for (int i = 1; i < n; i++) {
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
dp[i][1] = Math.max(dp[i - 1][0] - prices[i], dp[i - 1][1]);
}
return dp[n - 1][0];
}
}
res += prices[i] - prices[i - 1];
package com.shockang.study.algorithm.java.leetcode.leetcode101_200.leetcode122.solution2;
/**
* @author Shockang
*/
public class Solution {
public int maxProfit(int[] prices) {
int res = 0, n = prices.length;
for (int i = 1; i < n; i++) {
if (prices[i] > prices[i - 1]) {
res += prices[i] - prices[i - 1];
}
}
return res;
}
}
123. 买卖股票的最佳时机 III
题解
动态规划,3 种状态
package com.shockang.study.algorithm.java.leetcode.leetcode101_200.leetcode123.solution1;
/**
* @author Shockang
*/
public class Solution {
public int maxProfit(int[] prices) {
int n = prices.length;
int[][][] dp = new int[n][2][3];
dp[0][1][2] = dp[0][1][1] = dp[0][1][0] = -prices[0];
for (int i = 1; i < n; i++) {
dp[i][0][2] = dp[i - 1][0][2];
dp[i][0][1] = Math.max(dp[i - 1][0][1], dp[i - 1][1][2] + prices[i]);
dp[i][0][0] = Math.max(dp[i - 1][0][0], dp[i - 1][1][1] + prices[i]);
dp[i][1][2] = Math.max(dp[i - 1][0][2] - prices[i], dp[i - 1][1][2]);
dp[i][1][1] = Math.max(dp[i - 1][0][1] - prices[i], dp[i - 1][1][1]);
dp[i][1][0] = -1;
}
return Math.max(Math.max(dp[n - 1][0][0], dp[n - 1][0][1]), dp[n - 1][0][2]);
}
}
动态规划,买卖 2 次
package com.shockang.study.algorithm.java.leetcode.leetcode101_200.leetcode123.solution2;
/**
* @author Shockang
*/
public class Solution {
public int maxProfit(int[] prices) {
int n = prices.length;
int buy1 = -prices[0], sell1 = 0;
int buy2 = -prices[0], sell2 = 0;
for (int i = 1; i < n; ++i) {
buy1 = Math.max(buy1, -prices[i]);
sell1 = Math.max(sell1, buy1 + prices[i]);
buy2 = Math.max(buy2, sell1 - prices[i]);
sell2 = Math.max(sell2, buy2 + prices[i]);
}
return sell2;
}
}
124. 二叉树中的最大路径和
题解
全局变量max,左中右、左中、右中取大,左右和 0 比较
package com.shockang.study.algorithm.java.leetcode.leetcode101_200.leetcode124.solution1;
import com.shockang.study.algorithm.java.leetcode.common.TreeNode;
/**
* 全局变量max,左中右、左中、右中取大,左右和 0 比较
*
* @author Shockang
*/
public class Solution {
private int max = Integer.MIN_VALUE;
public int maxPathSum(TreeNode root) {
if (root == null) {
return 0;
}
helper(root);
return max;
}
private int helper(TreeNode root) {
if (root == null) {
return 0;
}
int left = Math.max(0, helper(root.left));
int right = Math.max(0, helper(root.right));
int lmr = left + root.val + right;
max = Math.max(max, lmr);
int ret = Math.max(left, right) + root.val;
max = Math.max(max, ret);
return ret;
}
}
125. 验证回文串
题解
官方题解
从两侧往中间收缩
package com.shockang.study.algorithm.java.leetcode.leetcode101_200.leetcode125.solution1;
/**
* @author Shockang
*/
public class Solution {
private char[] c;
private int n;
public boolean isPalindrome(String s) {
s = s.toLowerCase();
c = s.toCharArray();
n = c.length;
return expand(0, n - 1);
}
private boolean expand(int i, int j) {
while (i < j) {
if (!valid(c[i])) {
i++;
} else if (!valid(c[j])) {
j--;
} else if (c[i] == c[j]) {
i++;
j--;
} else {
return false;
}
}
return true;
}
private boolean valid(char ch) {
return ('a' <= ch && ch <= 'z') || ('0' <= ch && ch <= '9');
}
}