223. Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane.
Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.
Example:
Input: A = -3, B = 0, C = 3, D = 4, E = 0, F = -1, G = 9, H = 2 Output: 45
矩形面积
在二维平面上计算出两个由直线构成的矩形重叠后形成的总面积。
思路:直接计算重叠部分面积,当不交叠时长度或宽度为零。
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
"""
w=max(0,min(C,G)-max(A,E))
h=max(0,min(D,H)-max(B,F))
return (C-A)*(D-B)+(G-E)*(H-F)-w*h
423. Reconstruct Original Digits from English
Given a non-empty string containing an out-of-order English representation of digits 0-9
, output the digits in ascending order.
Note:
- Input contains only lowercase English letters.
- Input is guaranteed to be valid and can be transformed to its original digits. That means invalid inputs such as "abc" or "zerone" are not permitted.
- Input length is less than 50,000.
Example 1:
Input: "owoztneoer" Output: "012"
Example 2:
Input: "fviefuro" Output: "45"
给定一个非空字符串,其中包含字母顺序打乱的英文单词表示的数字0-9
。按升序输出原始的数字。
思路:先要统计出各个字符出现的次数,然后算出每个单词出现的次数,然后就可以重建了。由于题目中限定了输入的字符串一定是有效的,那么不会出现无法成功重建的情况,这里需要用个trick。我们仔细观察这些表示数字的单词"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",我们可以发现有些的单词的字符是独一无二的,比如z,只出现在zero中,还有w,u,x,g这四个单词,分别只出现在two,four,six,eight中,那么这五个数字的个数就可以被确定了,由于含有o的单词有zero,two,four,one,其中前三个都被确定了,那么one的个数也就知道了;由于含有h的单词有eight,three,其中eight个数已知,那么three的个数就知道了;由于含有f的单词有four,five,其中four个数已知,那么five的个数就知道了;由于含有s的单词有six,seven,其中six个数已知,那么seven的个数就知道了;由于含有i的单词有six,eight,five,nine,其中前三个都被确定了,那么nine的个数就知道了,知道了这些问题就变的容易多了,我们按这个顺序"zero", "two", "four", "six", "eight", "one", "three", "five", "seven", "nine"就能找出所有的个数了
class Solution(object):
def originalDigits(self, s):
"""
:type s: str
:rtype: str
"""
L=[x*0 for x in range(10)]
for i in s:
if i=='z':
L[0]+=1
if i=='w':
L[2]+=1
if i=='u':
L[4]+=1
if i=='x':
L[6]+=1
if i=='g':
L[8]+=1
if i=='o':
L[1]+=1
if i=='h':
L[3]+=1
if i=='f':
L[5]+=1
if i=='s':
L[7]+=1
if i=='i':
L[9]+=1
L[1]=L[1]-L[0]-L[2]-L[4]
L[3]=L[3]-L[8]
L[5]=L[5]-L[4]
L[7]=L[7]-L[6]
L[9]=L[9]-L[6]-L[8]-L[5]
res=[]
for index,value in enumerate(L):
for j in range(value):
res.append(str(index))
return ''.join(res)
593. Valid Square
Given the coordinates of four points in 2D space, return whether the four points could construct a square.
The coordinate (x,y) of a point is represented by an integer array with two integers.
Example:
Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1] Output: True
思路:如果是正方形的话需要满足,4条相等的短边,以及两边相等的对角线边。所以我们计算一下边的长度,然后判断一下是否只有两类边即可。注意四边形没有长度为0的边。
class Solution(object):
def validSquare(self, p1, p2, p3, p4):
"""
:type p1: List[int]
:type p2: List[int]
:type p3: List[int]
:type p4: List[int]
:rtype: bool
"""
def d(po1,po2):
return (po1[0]-po2[0])**2+(po1[1]-po2[1])**2
s=set([d(p1,p2),d(p1,p3),d(p1,p4),d(p2,p3),d(p2,p4),d(p3,p4)])
if 0 not in s and len(s)==2:
return True
return False
633. Sum of Square Numbers
Given a non-negative integer c
, your task is to decide whether there're two integers a
and b
such that a2 + b2 = c.
Example 1:
Input: 5 Output: True Explanation: 1 * 1 + 2 * 2 = 5
Example 2:
Input: 3 Output: False
class Solution(object):
def judgeSquareSum(self, c):
"""
:type c: int
:rtype: bool
"""
for a in range(int(c**0.5)+1):
b=c-a**2
if (int(b**0.5))**2==b:
return True
return False