摘要:
``` #主要是考虑边界情况 def power(base, exponent): if base == 0 and exponent < 0: return 0 absexponent = exponent if exponent < 0: absexponent = -exponent result = powerWithExponent(base, absexponent) if expon 阅读全文
摘要:
``` # 动态规划 # 1.最优解;2.分解成子问题 # 比如算f(8) 就把 f(1)f(7) f(2)f(6) f(3)f(5) f(4)f(4) 之前算出来的数字 重新算一遍即可 def maxProductAfterCutting_solution1(lenth): if lenth == 0 or lenth == 1: return 0 if lenth == 2: return 1 阅读全文
摘要:
``` # 位运算 # 思路:把该number的左右边的1-1变为0,1后面的0变为1;在于number & 的话就消除一个1,循环消1 即可 def numberof_inbinary(number): count = 0 while number: count += 1 number = (number - 1) & number # 本质是在消除1的过程 return count ``` 阅读全文
摘要:
``` def moveCount(threshold, rows, cols): if threshold = 0 and row >= 0 and col 0: sum += number % 10 number //= 10 return sum #递归的思想 #1.row col 0 0 check(0,0)=True #赋值visited;递归-1 0 false return 0 递归 阅读全文
摘要:
```def haspathCore(martrix, rows, cols, row, col, visited, findstr, pathlenth): # cur_ch=findstr[pathlenth] if pathlenth == len(findstr): # 找到最后一个字符停止查找 return True hasPath = False ... 阅读全文
摘要:
``` def get_min(li): low = 0 high = len(li) - 1 mid = low while li[low] >= li[high]: # 一般low的元素>=high的元素 if high - low == 1: mid = high break if (li[low] == li[high] and li[mid] == li[low]): # 特殊情况:无法 阅读全文
摘要:
```# 二分查找def bin_search(li, val): # 有序列表 low = 0 high = len(li)-1 while low val: high = mid - 1 else: # li[mid]<val low = mid + 1 return -1``` 阅读全文
摘要:
``` def fibbonacci(n): if not isinstance(n, int): raise ValueError('n is int type') resulet = [0, 1] if n in resulet: return resulet[n] fibbo2 = 0 # n-2的项 fibbo1 = 1 # n-1的项 fibbon = 0 # n for i in ra 阅读全文