视频教程:B站、网易云课堂、腾讯课堂
代码地址:Gitee、Github
存储地址:
百度云-提取码:
Google云
国内站
java版
/*
思路:
*/
/*
评价:
优点:
缺点:
*/
python版:
'''
思路:
'''
'''
评价:
优点:
缺点:
'''
c++版:
/*
思路:
*/
/*
评价:
优点:
缺点:
*/
国外站
java版
/*
思路:
*/
class Solution {
public int lengthOfLIS(int[] nums) {
int[] tails = new int[nums.length];
int size = 0;
for (int x : nums) {
int i = 0, j = size;
while (i != j) {
int m = (i + j) / 2;
if (tails[m] < x)
i = m + 1;
else
j = m;
}
tails[i] = x;
if (i == size) ++size;
}
return size;
}
}
/*
评价:
优点:
缺点:
*/
python版:
'''
思路:
'''
'''
评价:
优点:
缺点:
'''
c++版:
/*
思路:
*/
/*
评价:
优点:
缺点:
*/
国内站
java版
/*
思路:
*/
/*
评价:
优点:
缺点:
*/
python版:
'''
思路:
'''
'''
评价:
优点:
缺点:
'''
c++版:
/*
思路:
*/
/*
评价:
优点:
缺点:
*/
国外站
java版
/*
思路:
*/
public class Solution {
public int numDecodings(String s) {
if (s == null || s.length() == 0) {
return 0;
}
int n = s.length();
int[] dp = new int[n + 1];
dp[0] = 1;
dp[1] = s.charAt(0) != '0' ? 1 : 0;
for (int i = 2; i <= n; i++) {
int first = Integer.valueOf(s.substring(i - 1, i));
int second = Integer.valueOf(s.substring(i - 2, i));
if (first >= 1 && first <= 9) {
dp[i] += dp[i-1];
}
if (second >= 10 && second <= 26) {
dp[i] += dp[i-2];
}
}
return dp[n];
}
}
/*
评价:
优点:
缺点:
*/
python版:
'''
思路:
'''
'''
评价:
优点:
缺点:
'''
c++版:
/*
思路:
*/
/*
评价:
优点:
缺点:
*/
国内站
java版
/*
思路:
*/
/*
评价:
优点:
缺点:
*/
python版:
'''
思路:
'''
'''
评价:
优点:
缺点:
'''
c++版:
/*
思路:
*/
/*
评价:
优点:
缺点:
*/
国外站
java版
/*
思路:
*/
public class Solution {
public int longestValidParentheses(String s) {
Stack<Integer> stack = new Stack<Integer>();
int max=0;
int left = -1;
for(int j=0;j<s.length();j++){
if(s.charAt(j)=='(') stack.push(j);
else {
if (stack.isEmpty()) left=j;
else{
stack.pop();
if(stack.isEmpty()) max=Math.max(max,j-left);
else max=Math.max(max,j-stack.peek());
}
}
}
return max;
}
}
/*
评价:
优点:
缺点:
*/
python版:
'''
思路:
'''
'''
评价:
优点:
缺点:
'''
c++版:
/*
思路:
*/
/*
评价:
优点:
缺点:
*/
国内站
java版
/*
思路:
*/
/*
评价:
优点:
缺点:
*/
python版:
'''
思路:
'''
'''
评价:
优点:
缺点:
'''
c++版:
/*
思路:
*/
/*
评价:
优点:
缺点:
*/
国外站
java版
/*
思路:
*/
public class Solution {
public int maximalRectangle(char[][] matrix) {
if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return 0;
int[] height = new int[matrix[0].length];
for(int i = 0; i < matrix[0].length; i ++){
if(matrix[0][i] == '1') height[i] = 1;
}
int result = largestInLine(height);
for(int i = 1; i < matrix.length; i ++){
resetHeight(matrix, height, i);
result = Math.max(result, largestInLine(height));
}
return result;
}
private void resetHeight(char[][] matrix, int[] height, int idx){
for(int i = 0; i < matrix[0].length; i ++){
if(matrix[idx][i] == '1') height[i] += 1;
else height[i] = 0;
}
}
public int largestInLine(int[] height) {
if(height == null || height.length == 0) return 0;
int len = height.length;
Stack<Integer> s = new Stack<Integer>();
int maxArea = 0;
for(int i = 0; i <= len; i++){
int h = (i == len ? 0 : height[i]);
if(s.isEmpty() || h >= height[s.peek()]){
s.push(i);
}else{
int tp = s.pop();
maxArea = Math.max(maxArea, height[tp] * (s.isEmpty() ? i : i - 1 - s.peek()));
i--;
}
}
return maxArea;
}
}
/*
评价:
优点:
缺点:
*/
python版:
'''
思路:
'''
'''
评价:
优点:
缺点:
'''
c++版:
/*
思路:
*/
/*
评价:
优点:
缺点:
*/
国内站
java版
/*
思路:
*/
/*
评价:
优点:
缺点:
*/
python版:
'''
思路:
'''
'''
评价:
优点:
缺点:
'''
c++版:
/*
思路:
*/
/*
评价:
优点:
缺点:
*/
国外站
java版
/*
思路:
*/
class Solution {
public int numDistinct(String S, String T) {
// array creation
int[][] mem = new int[T.length()+1][S.length()+1];
// filling the first row: with 1s
for(int j=0; j<=S.length(); j++) {
mem[0][j] = 1;
}
// the first column is 0 by default in every other rows but the first, which we need.
for(int i=0; i<T.length(); i++) {
for(int j=0; j<S.length(); j++) {
if(T.charAt(i) == S.charAt(j)) {
mem[i+1][j+1] = mem[i][j] + mem[i+1][j];
} else {
mem[i+1][j+1] = mem[i+1][j];
}
}
}
return mem[T.length()][S.length()];
}
}
/*
评价:
优点:
缺点:
*/
python版:
'''
思路:
'''
'''
评价:
优点:
缺点:
'''
c++版:
/*
思路:
*/
/*
评价:
优点:
缺点:
*/
国内站
java版
/*
思路:
*/
/*
评价:
优点:
缺点:
*/
python版:
'''
思路:
'''
'''
评价:
优点:
缺点:
'''
c++版:
/*
思路:
*/
/*
评价:
优点:
缺点:
*/
国外站
java版
/*
思路:
*/
class Solution {
int[] dp = new int[10001];
public int racecar(int t) {
if (dp[t] > 0) return dp[t];
int n = (int)(Math.log(t) / Math.log(2)) + 1;
if (1 << n == t + 1) {
dp[t] = n;
} else {
dp[t] = racecar((1 << n) - 1 - t) + n + 1;
for (int m = 0; m < n - 1; ++m) {
dp[t] = Math.min(dp[t], racecar(t - (1 << (n - 1)) + (1 << m)) + n + m + 1);
}
}
return dp[t];
}
}
/*
评价:
优点:
缺点:
*/
python版:
'''
思路:
'''
'''
评价:
优点:
缺点:
'''
c++版:
/*
思路:
*/
/*
评价:
优点:
缺点:
*/
一 动态规划、状态转移方程串讲