165-171 我直接送一道 好家伙
166: 比较版本号
用split分割 用int比较
class Solution: def compareVersion(self, version1: str, version2: str) -> int: v1 = version1.split('.') v2 = version2.split('.') n1 = len(v1) n2 = len(v2) n = min(n1,n2) i = 0 while i < n : if int(v1[i]) > int(v2[i]): return 1 elif int(v1[i]) < int(v2[i]): return -1 i += 1 if i == n1: for i in range(n,n2): if int(v2[i]) != 0: return -1 if i == n2: for i in range(n,n1): if int(v1[i]) != 0: return 1 return 0 作者:yizhu-jia 链接:https://leetcode-cn.com/problems/compare-version-numbers/solution/yong-splitfen-ge-yong-intbi-jiao-by-yizh-jzre/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
166 :分数到小数 :
我直接CV工程师 因为不知道半途开始的循环咋办 原来是模仿除法
class Solution: def fractionToDecimal(self, numerator: int, denominator: int) -> str: if numerator % denominator == 0: return str(numerator // denominator) s = [] if (numerator < 0) != (denominator < 0): s.append('-') # 整数部分 numerator = abs(numerator) denominator = abs(denominator) integerPart = numerator // denominator s.append(str(integerPart)) s.append('.') # 小数部分 indexMap = {} remainder = numerator % denominator while remainder and remainder not in indexMap: indexMap[remainder] = len(s) remainder *= 10 s.append(str(remainder // denominator)) remainder %= denominator if remainder: # 有循环节 insertIndex = indexMap[remainder] s.insert(insertIndex, '(') s.append(')') return ''.join(s) 作者:yizhu-jia 链接:https://leetcode-cn.com/problems/fraction-to-recurring-decimal/solution/cvgong-cheng-shi-by-yizhu-jia-umwa/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
167: 输入有序数组
好像二分更好 但是双指针 你值得拥有
class Solution: def twoSum(self, numbers: List[int], target: int) -> List[int]: left = 0 right = len(numbers) -1 while left < right: if numbers[left] + numbers[right] == target: return [left+1,right+1] elif numbers[left] + numbers[right] > target: right -= 1 else: left += 1 作者:yizhu-jia 链接:https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/solution/you-er-fen-fa-ma-wo-zen-mo-bu-zhi-dao-by-rsct/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
168 :excel 列表名称
简单题
class Solution: def convertToTitle(self, columnNumber: int) -> str: rel = '' while columnNumber != 0 : columnNumber -= 1 rel = ''.join((chr(columnNumber%26+65),rel)) columnNumber = columnNumber//26 return rel 作者:yizhu-jia 链接:https://leetcode-cn.com/problems/excel-sheet-column-title/solution/jian-1jian-de-jing-sui-a-by-yizhu-jia-krly/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
169 :
多数元素 因为之前见过 所以直接写了 计数到0就换数
class Solution: def majorityElement(self, nums: List[int]) -> int: curnum = nums[0] count = 0 for each in nums: if curnum == each: count += 1 else: if count == 0: curnum = each else: count -= 1 return curnum 作者:yizhu-jia 链接:https://leetcode-cn.com/problems/majority-element/solution/ji-shu-dao-0huan-shu-by-yizhu-jia-7ufh/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
171: excel表列序号
简单题
class Solution: def titleToNumber(self, columnTitle: str) -> int: rel = 0 for each in columnTitle: rel = rel * 26 + ord(each) - ord('A') + 1 return rel 作者:yizhu-jia 链接:https://leetcode-cn.com/problems/excel-sheet-column-number/solution/zhe-ti-da-jia-fang-fa-ying-gai-du-yi-yan-lmak/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。