列表类型不支持索引?好吧 调试了大半天,也没有找到办法解决。就这样吧。反正本地能跑()
加班加点的完成任务,今天第四天
日常刷题。
LeetCode #29 两数相除
没有AC,过了十个点,仅仅十个点,暂时的 没有思路,先挖个坑,以后有想法了 再回来补。。
判断是否为0或者 除数比被除数大,即return 0
不满足上面条件,每次减被除数,这里要注意正数和负数的区别。
class Solution: def divide(self, dividend, divisor): """ :type dividend: int :type divisor: int :rtype: int """ count,i = True,0 CopyDiviend = dividend if dividend == 0 or abs(dividend) < abs(divisor) : return 0 else : while count : CopyDiviend = abs(CopyDiviend) - abs(divisor) i += 1 if abs(CopyDiviend) <= abs(divisor) : count = False if dividend > 0 and divisor > 0 or dividend < 0 and divisor < 0 : return i else : return -i
LeetCode #67 二进制求和
很水的题,进制转化 + 二进制的处理即可
class Solution: def addBinary(self, a, b): """ :type a: str :type b: str :rtype: str """ a = int(a,2) b = int(b,2) return str(bin(a+b)).replace('0b','')
LeetCode #66 加一
数组转化为字符串,字符串转化为数字,处理数字,数字转化为字符串,然后再转化为数组,即可。
class Solution: def plusOne(self, digits): """ :type digits: List[int] :rtype: List[int] """ new_num = '' for i in range(len(digits)): new_num = new_num + str(digits[i]) new_num = int(new_num) + 1 str_digits = str(new_num) re_digits = [] for i in range(len(str_digits)): re_digits.append(int(str_digits[i])) return re_digits
LeetCode #149 直线上最多的点
分别储存x,y,x-y,x+y,并计算其中出现最多的元素的次数,即再同一直线上面的点,return 四个数字中间最大的点
这里没有AC,报错: 'Point' object does not support indexing,但是points 是list,怎么可能不支持索引???
明天白天问问大佬怎么解决。
class Solution: def maxPoints(self, Points): """ :type points: List[Point] :rtype: int """ Points_x = [] Points_y = [] Points_x_add_y =[] Points_x_minus_y = [] for i in range(len(Points)): Points_x.append(Points[i][0]) # x轴坐标 Points_y.append(Points[i][1]) # y轴坐标 Points_x_add_y.append(Points[i][0] + Points[i][1]) Points_x_minus_y.append(Points[i][0] - Points[i][1]) return max(Solution.handlePoints(Points_x),Solution.handlePoints(Points_y),Solution.handlePoints(Points_x_add_y),Solution.handlePoints(Points_x_minus_y)) def handlePoints(getlist): new_getlist = set(getlist) pointsTime = 0 for i in new_getlist: if getlist.count(i) > pointsTime: pointsTime = getlist.count(i) return pointsTime