题目描述: 第一次提交: class Solution: def addBinary(self, a: str, b: str) -> str: list_a,list_b=[],[] for s in a: list_a.append(int(s)) for s in b: list_b.append(int(s)) if len(list_a)>=len(list_b): for i in range(len(list_a)-len(list_b)): list_b.insert(0,0)…
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Assembly Language Step-By-Setp:Programming with Linux 3rd Edition>,非常感谢该书的作者和译者,谢谢你们提供了这么好的学习材料.…
笔记: python if not 判断是否为None的情况 if not x if x is None if not x is None if x is not None`是最好的写法,清晰,不会出现错误,以后坚持使用这种写法. 使用if not x这种写法的前提是:必须清楚x等于None, False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()时对你的判断没有影响才行 链接:https://www.cnblogs.com/chenya/p/4218761.…
题目描述: 第一次提交:(会超时) class Solution: def mySqrt(self, x: int) -> int: if x==0 or x==1: return x for i in range(1,x): if i*i<=x and (i+1)**2>x: return i 方法二:牛顿迭代法(最优解)(泰勒展开式) class Solution: def mySqrt(self, x): """ :type x: int :rtype…
题目描述: 第一次提交: class Solution: def spiralOrder(self, matrix: List[List[int]]) -> List[int]: j,x = 0,1 l = [] if matrix==[]: return [] m = len(matrix) n = len(matrix[0]) while x<=n*m: for i in range(j,n-j): l.append(matrix[j][i]) x += 1 for i in range(…
题目描述: 第一次提交;(超时): class Solution: def countPrimes(self, n: int) -> int: count = 0 for i in range(2,n): for j in range(2,i+1): if i%j == 0 and j!=i: break if j==i: count+=1 return count 别人家的: 这题搜到一个非常牛逼的算法,叫做厄拉多塞筛法. 比如说求20以内质数的个数,首先0,1不是质数.2是第一个质数,然后把…
题目 给你两个二进制字符串,返回它们的和(用二进制表示). 输入为 非空 字符串且只包含数字 1 和 0. 题解 两个字符串从低位开始加,前面位不够补0.维护进位,最后加上最后一个进位,最后反转结果字符串. 代码 class Solution { public String addBinary(String a, String b) { StringBuilder sb = new StringBuilder(); int ca = 0; for(int i = a.length() - 1,…
题目: 第一次提交; class Solution: def searchInsert(self, nums: List[int], target: int) -> int: for i in range(len(nums)): if nums[i] >= target : return i if i == (len(nums)-1) and nums[i]<target: return i+1 法二: class Solution: def searchInsert(self, num…
题目: 第一次提交: class Solution: def strStr(self, haystack: str, needle: str) -> int: if not len(needle): return 0 for i in range(len(haystack)): if i+len(needle)<=len(haystack): if haystack[i:(i+len(needle))]==needle: return i return -1 方法二: Sunday 平均O(N…
题目: 第一次提交: class Solution: def removeElement(self, nums, val: int) -> int: for i in range(len(nums)-1,-1, -1):#此处中间为range(,中间值为-1,) if nums[i] == val: nums.remove(nums[i])#或nums.pop(i) return len(nums) 方法二:正序 class Solution: def removeElement(self, n…
题目描述: 第一次提交: class Solution: def removeDuplicates(self, nums) -> int: for i in range(len(nums)-1,0,-1):#注意要倒序** if nums[i]==nums[i-1]: del(nums[i]) return len(nums) 另: class Solution: def removeDuplicates(self, nums: List[int]) -> int: i = 0 for num…
错误记录 class Solution: def romanToInt(self, s: str) -> int: d = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000} r=0 for i in range(len(s)): if d[s[i]]<d[s[i+1]] and i<len(s)-1: r-=d[s(i)] else: r+=d[s(i)] return r 会报错:TypeError: 'str' object i…
题目描述: 第一次提交: class Solution: def generate(self, numRows: int): l = [] for i in range(numRows): n = [1]*(i+1) if len(n)>2: #pre = [1,1] for j in range(1,len(n)-1): n[j] = pre[j-1]+pre[j] #l.append([n]) l+=[n] pre = n return l 方法二: def generate(self, n…
题目描述: 第一次提交: class Solution: def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None: """ Do not return anything, modify nums1 in-place instead. """ for i in range(n): nums1.remove(nums1[-1]) for i in r…