leetcode11 裝最多水的容器(要學會簡化問題)
阿新 • • 發佈:2018-04-29
AR nbsp where represent ++ with ner 一個 oge
- 簡化問題後的代碼
-
/* 原問題: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. 解析:這是一道考察算法的題目,如果你沒有get到那個點,就會搞得很復雜,但是簡化了問題之後就會恍然大悟,非常的簡單 簡化的代碼和原來的代碼如下
- 原來自己寫的復雜的代碼
-
struct structure { int height;// 表示高度 int sub;// 表示下標 structure(int height, int sub):height(height), sub(sub){} }; class Solution { public: int maxArea(vector<int>& height) {
- 1234213412
leetcode11 裝最多水的容器(要學會簡化問題)