LeetCode刷题专题
1.
https://leetcode-cn.com/problems/container-with-most-water/
思想:左右边界 i,j 向中间收敛 ,左右夹逼
方法一:
一维数组的坐标变换 i,j
枚举:left bar,right bar. (x-y)*height_diff
class Solution {
public int maxArea(int[] height) {
int max = 0 ;
for(int i = 0 ; i < height.length -1; ++i ){
for(int j = i + 1;j < height.length; ++j){
int area = (j-i)* Math.min(height[i],height[j]);
max = Math.max(max,area);
}
}
return max;
}
}
方法二:
class Solution {
public int maxArea(int[] height) {
int max = 0 ;
for(int i = 0, j = height.length -1; i<j;) {
int minHeight = height[i]<height[j]? height[i++]:height[j--];
int area =(j-i+1)*minHeight;
max = Math.max(max,area);
}
return max;
}
}
2.
https://leetcode-cn.com/problems/climbing-stairs/
//1.暴力?2.基本情况 列举 看规律。递推
//找最近重复子问题 if else ;while,recursion
// n阶的走法。只能从n-1阶或者n-2阶上走上去
class Solution {
public int climbStairs(int n) {
int result = 0;
int f1 = 1;
int f2 = 2;
if (n == 1) {
return f1;
}
if (n == 2) {
return f2;
}
for (int i = 3; i <= n; i++) {
result = f1 + f2;
f1 = f2;
f2 = result;
}
return result;
}
}