leetcode -- Rectangle Area -- 看看计算公式
来源:互联网 发布:spark mongodb python 编辑:程序博客网 时间:2024/06/06 03:31
https://leetcode.com/problems/rectangle-area/
思路1
ref:http://bookshadow.com/weblog/2015/06/08/leetcode-rectangle-area/
用最小的右端点减去最大的左端点. 例如对于[A,C] 与[E,G], 求相交长度公式为max(min(C, G) - max(A, E), 0), 这里用嘴简单的那种AECG这种相交的case来记这个公式。右端点的min减去左端点的max,再与0取max
class Solution: # @param {integer} A # @param {integer} B # @param {integer} C # @param {integer} D # @param {integer} E # @param {integer} F # @param {integer} G # @param {integer} H # @return {integer} def computeArea(self, A, B, C, D, E, F, G, H): sums = (C - A) * (D - B) + (G - E) * (H - F) return sums - max(min(C, G) - max(A, E), 0) * max(min(D, H) - max(B, F), 0)
other method
知道计算公式就行。自己的公式也可以.最好是用ref的公式
my code
class Solution(object): def computeArea(self, A, B, C, D, E, F, G, H): """ :type A: int :type B: int :type C: int :type D: int :type E: int :type F: int :type G: int :type H: int :rtype: int """ #这里两个线段是[A,C], [E,G], 为了求相交线段的长度,我们先求左端点的最大值,然后,分别用右端点减去这个最大值,然后求这两者的min。G跟C地位相同,play the same role 所以也要减去max(A,E) a = min(C - max(A,E), G - max(A,E)) b = min(D - max(B,F), H - max(B,F)) sum_area = (C - A) * (D - B) + (G - E) * (H - F) if a > 0 and b > 0: return sum_area - a * b else: return sum_area
0 0
- leetcode -- Rectangle Area -- 看看计算公式
- LeetCode Rectangle Area 简单计算
- leetcode 223. Rectangle Area 矩形面积计算
- leetcode Rectangle Area
- [LeetCode] Rectangle Area
- leetcode--Rectangle Area
- [LeetCode]Rectangle Area
- [leetcode] Rectangle Area
- Rectangle Area - LeetCode 223
- leetcode-Rectangle Area[math]
- leetCode 之 Rectangle Area
- leetcode 223: Rectangle Area
- [leetcode]Rectangle Area
- leetcode[223]:Rectangle Area
- LeetCode Rectangle Area
- [leetcode][math] Rectangle Area
- #leetcode#Rectangle Area
- [LeetCode] Rectangle Area
- LT8911 市面上兼容性最好的EDP桥接IC,LVDS转EDP,mipi转EDP
- Callback Hell-Javascript异步编程指导
- #学习笔记#(4)输入框提示信息不能为空--JavaScript改变CSS样式
- Xcode 和Interface Builder
- 为什么Maven会更改Eclipse JDK设置
- leetcode -- Rectangle Area -- 看看计算公式
- grep在文本中查找内容
- javaday03-语言基础
- LeetCode(217)Contains Duplicate
- jsp技术
- 五大常用算法之二:动态规划算法
- 对【委托的执行方法】的测试与理解
- CodeForces - 27E Number With The Given Amount Of Divisors (反素数入门)
- ubuntu下Qt cannot find -lGL错误的解决方法