![[Leetcode] 动态规划类java题解,第1张 [Leetcode] 动态规划类java题解,第1张](/aiimages/%5BLeetcode%5D+%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%E7%B1%BBjava%E9%A2%98%E8%A7%A3.png)
139. 单词拆分
public class WordBreak {
public boolean wordBreak(String s, List wordDict) {
Set wordSet = new HashSet(wordDict);
boolean[] dp = new boolean[s.length() + 1]; //Boolean会抛空指针
dp[0] = true;
for (int i = 1; i <= s.length(); i++) {
for (int j = 0; j < i; j++) {
if (dp[j] && wordSet.contains(s.substring(j, i))) {
dp[i] = true;
break;
}
}
}
return dp[s.length()];
}
}
152. 乘积最大子数组
class Solution {
public int maxProduct(int[] nums) {
int n = nums.length;
int[] dp = new int[n];
dp[0]=nums[0];
int result = nums[0], nmax = nums[0], nmin = nums[0];
for(int i = 1;i
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)