class Solution public int maxAreavectorint& height int i=0j=heightsize-1res=0; whileij res=heightiheightjmaxresj-iheighti++maxresj-iheightj--; return res; ;转换成TypeScript、Swi
TypeScript:
class Solution { maxArea(height: number[]): number { let i = 0, j = height.length - 1, res = 0; while (i < j) { res = height[i] < height[j] ? Math.max(res, (j - i) * height[i++]) : Math.max(res, (j - i) * height[j--]); } return res; } }
Swift:
class Solution { func maxArea(_ height: [Int]) -> Int { var i = 0, j = height.count - 1, res = 0 while i < j { res = height[i] < height[j] ? max(res, (j - i) * height[i]) : max(res, (j - i) * height[j]) if height[i] < height[j] { i += 1 } else { j -= 1 } } return res } }
Go:
func maxArea(height []int) int { i, j := 0, len(height) - 1 res := 0 for i < j { if height[i] < height[j] { res = max(res, (j - i) * height[i]) i += 1 } else { res = max(res, (j - i) * height[j]) j -= 1 } } return res }
func max(a, b int) int { if a > b { return a } else { return b }
原文地址: https://www.cveoy.top/t/topic/iv3c 著作权归作者所有。请勿转载和采集!