1. 换礼物,a,b,c任意两个可以换一个,求最大换取礼物
二分查找
class Solution: def numberofprize(self , a , b , c ): # write code here high = (a+b+c) * 2 // 3 low = 0 while low <= high: mid = (high + low) // 2 higher = 0 lower = 0 for letter in [a,b,c]: if letter >= mid: higher += letter - mid else: lower += mid - letter if higher == 2 * lower: return mid elif higher < 2 * lower: high = mid - 1 else: low = mid + 1 return high2. 建房子,要建一座房子宽度为t,已建房子xa,每两个分别表示房子中心和宽度,求建房子方法数
class Solution: def getHouses(self , t , xa ): # write code here n = len(xa) count = 2 temp = [] for i in range(0,n,2): tmp = [xa[i]-xa[i+1]/2, xa[i]+xa[i+1]/2] temp.append(tmp) for i in range(len(temp)-1): if temp[i+1][0] - temp[i][1] == t: count += 1 elif temp[i+1][0] - temp[i][1] > t: count += 2 return count3. 写测试用例,长度小于10的非空字符串,消除后结果不出现'ab'和'c'
abdcab d
dddd dddd
ababab NULL
acb NULL
aaabbb NULL
accccccbbb bb
dddccabccc ddd
aaaaabbbbb NULL
accccccccb NULL
adbadbadba adbadbadba
dddd dddd
ababab NULL
acb NULL
aaabbb NULL
accccccbbb bb
dddccabccc ddd
aaaaabbbbb NULL
accccccccb NULL
adbadbadba adbadbadba
全部评论
(2) 回帖