149-153
149: 直线最多的点数
思路就是 对于每一个点 看后面能和他组成的直线数
相同的直线怎么判断呢 就用一个字典 标签是 '1' +str((Y2-Y1)/(X2-X1)) 就是用横坐标归一化
不过要注意 平行于X轴和Y轴的直线要单独标出来 我用的是X0 和 Y0
判断完一个数后 字典要重置
class Solution: def maxPoints(self, points: List[List[int]]) -> int: n = len(points) linedict = {'X0':0,'Y0':0} max = 0 for i in range(n): for j in range(i+1,n): if points[j][1] - points[i][1] == 0: linedict['X0'] += 1 if points[j][0] - points[i][0] == 0: linedict['Y0'] += 1 elif points[j][1] - points[i][1] != 0: strtemp = '1' + str((points[j][1] - points[i][1])/(points[j][0] - points[i][0])) if strtemp in linedict: linedict[strtemp] += 1 else: linedict[strtemp] = 1 for each in linedict: if linedict[each] > max: max = linedict[each] linedict = {'X0':0,'Y0':0} return max +1 作者:yizhu-jia 链接:https://leetcode-cn.com/problems/max-points-on-a-line/solution/bu-cuo-bu-cuo-yi-ci-ti-jiao-jiu-cheng-go-cm81/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
150 : 逆波兰式
出题人把答案都写出来了 看不起谁呢????
这道题麻烦的是正负整数的取整
class Solution: def evalRPN(self, tokens: List[str]) -> int: nums = [] ope = [] for each in tokens: if len(each) >=2 or ord(each) >=48 and ord(each) <=57 : nums.append(int(each)) else: temp1 = nums.pop() temp2 = nums.pop() if each == '+': nums.append(temp2+temp1) elif each == '-': nums.append(temp2-temp1) elif each == '*': nums.append(temp2*temp1) elif each == '/': if temp2*temp1 < 0: nums.append(-1*(abs(temp2)//abs(temp1))) else: nums.append(temp2//temp1) return nums[0] 作者:yizhu-jia 链接:https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/solution/kan-bu-qi-shui-ni-chu-ti-huan-ba-da-an-x-lhjp/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
151:
反转字符串里的单词
想了半天 被9成人击败
class Solution: def reverseWords(self, s: str) -> str: s = s[::-1] n = len(s) def findnextletter(index): n = len(s) while index < n and s[index] == ' ': index += 1 letterstart = index #找到下一個單詞開始 while index < n and s[index] != ' ': index += 1 if len(s) > n : index += 1 return letterstart,index #找到下一個單詞結束 start = 0 while start < n : letterstart,letterend = findnextletter(start) s = s[:start]+s[letterstart:letterend][::-1]+' '+s[letterend:] start = letterend - (letterstart-start) + 1 return s.rstrip(' ') 作者:yizhu-jia 链接:https://leetcode-cn.com/problems/reverse-words-in-a-string/solution/zen-yao-hui-shi-xiang-liao-ban-tian-bei-69i21/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
152:
乘积最大子数组
能用多少方法用多少種 我就是要过 好像提交了无数次
主要思路就是 乘到绝对值最大时 如果负数的个数是偶数个就不管 如果是奇数个 就除以绝对值最小的那个 。
class Solution: def maxProduct(self, nums: List[int]) -> int: if nums == [0]: return 0 negcount = 0 n = len(nums) maxmul = float('-inf') curmul = 1 gate = False firneg = 1 lastneg = 0 numcount = 0 for i,each in enumerate(nums): if each == 0 : if gate: if negcount % 2 == 1 and numcount != 1: curmul = curmul/max(firneg,lastneg) #这里是除以绝对值更小的那个 if curmul > maxmul: maxmul = curmul if 0 > maxmul: maxmul = 0 firneg = 1 lastneg = 0 curmul = 1 negcount = 0 numcount = 0 else: numcount += 1 gate = True if negcount == 0: firneg *= each curmul *= each lastneg *= each if each < 0: negcount += 1 lastneg = each if i == n-1: if negcount % 2 == 1 and numcount != 1: curmul = curmul/max(firneg,lastneg) #这里是除以绝对值更小的那个 if curmul > maxmul: maxmul = curmul return int(maxmul)
153:
寻找旋转排序中最小值
我用的是二分 结果被大部分击败 我怀疑他们min
class Solution: def findMin(self, nums: List[int]) -> int: left = 0 n = len(nums) right = n-1 while left <= right: mid = (left +right) //2 if mid == n-1 or mid == 0: return min(nums[left],nums[right]) if nums[mid] < nums[mid+1] and nums[mid] < nums[mid-1]: return nums[mid] if nums[mid] > nums[left]: if nums[mid] > nums[right]: left = mid + 1 else: return nums[left] else: #当小于左端点时。 if nums[mid] > nums[right]: left = mid + 1 else: right = mid -1 作者:yizhu-jia 链接:https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array/solution/wei-sha-zi-wo-zhe-yao-man-er-qie-kong-ji-ltks/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。