zoukankan      html  css  js  c++  java
  • 【Leetcode】--数组篇--No.11--盛最多水的容器

    题目:

    给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。

    说明:你不能倾斜容器,且 n 的值至少为 2。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/container-with-most-water
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    第一次解答:

    class Solution {
        public int maxArea(int[] height) {
            int ans = 0, length = height.length, tmp = 0;
            for (int i = 0; i < length - 1; i++){
                for (int j = i + 1; j < length; j++){
                    tmp = (j - i) * Math.min(height[i], height[j]);
                    if (tmp > ans) {
                        ans = tmp;
                    }
                }
            }
            
            return ans;
        }
    }
  • 相关阅读:
    其他魔术方法
    类型约束和类的魔术常量
    PHP对象遍历、内置标准类与数据转对象
    类的自动加载与对象的克隆
    PHP重载与接口
    面向对象
    PHP基础
    地下城与勇士的项目整理
    mysql建表
    jQuery
  • 原文地址:https://www.cnblogs.com/chenyun-/p/12287317.html
Copyright © 2011-2022 走看看