zoukankan      html  css  js  c++  java
  • 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;
        
        }
    }



  • 相关阅读:
    SiteMesh入门(1-1)SiteMesh是什么?
    接口和抽象类有什么区别
    StringUtils工具类常用方法汇总(判空、转换、移除、替换、反转)
    StringUtils工具类常用方法汇总(截取、去除空白、包含、查询索引)
    加密方法与HTTPS 原理详解
    String.split()与StringUtils.split()
    自动生成注释
    linux下安装与部署redis
    mybatis批量保存的两种方式(高效插入)
    pagehelper的使用
  • 原文地址:https://www.cnblogs.com/terrycode/p/12362505.html
Copyright © 2011-2022 走看看