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;
        
        }
    }



  • 相关阅读:
    VS.NET 2005 常用的快捷键
    路径,文件,目录,I/O常见操作汇总
    c#中cookies的存取操作
    ASP.NET AJAX入门系列(1):概述
    RS2008中控件ID冲突问题
    [书名]各种计算机语言的经典书籍
    终于把课件做好了~~
    还是两个数的交换问题
    自制简易图片尺寸调整工具[源]
    被点名了~~~[游戏]
  • 原文地址:https://www.cnblogs.com/terrycode/p/12362505.html
Copyright © 2011-2022 走看看