![](https://img-blog.csdnimg.cn/20201014180756919.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
数学
BlackMan_阿伟
不积跬步无以至千里
展开
-
April——781.森林中的兔子
class Solution: def numRabbits(self, answers: List[int]) -> int: if not answers: return 0 #先统计每个兔子告诉你还有多少其他的兔子和自己有相同的颜色 #然后找规律,计算 #向上取整ceil(x/y+1)==(x+y)//(y+1) count = Counter(answers) ...原创 2021-04-06 10:14:28 · 61 阅读 · 0 评论 -
March——解完全平方数
给定整数,找到若干个完全平方数(比如1,4,9,16,...)使得它们的和等于n。你需要让组成和的完全平方数的个数最小。给你一个整数n,返回和为n的完全平方数的最小数量。输入:12输出:3解释:4+4+4---------------------------------------------------输入:13输出:2解释:4+9动态规划 最小数量,最大数量这种问题其实都可以考虑使用动态规划来做,类似于背包问题 dp[i]表示的是当n等于i的时候完全平方数的最少数量.原创 2021-03-30 12:28:59 · 220 阅读 · 0 评论 -
February——665.非递减数列
class Solution: def checkPossibility(self, nums: List[int]) -> bool: if not nums: return True N = len(nums) count = 0 for i in range(1, N): #如果出现前一个数字大于后一个数字,那就count+1 ...原创 2021-02-07 11:06:40 · 99 阅读 · 0 评论 -
LeetCode——888. 公平的糖果棒交换
class Solution: def fairCandySwap(self, A: List[int], B: List[int]) -> List[int]: Sum_A = sum(A) Sum_B = sum(B) differ = (Sum_A-Sum_B)//2 rec = set(A) ans = [] for y in B: x = y + diff...原创 2021-02-01 20:22:38 · 79 阅读 · 0 评论 -
LeetCode——231.2的幂
class Solution: def isPowerOfTwo(self, n: int) -> bool: # num = 0 # for i in range(100): # num = 2**i # if num==n: # return True # if num>n: # return False ...原创 2021-01-31 09:29:43 · 93 阅读 · 0 评论 -
LeetCode——2.两数相加
# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution: def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: ...原创 2021-01-22 11:35:02 · 90 阅读 · 0 评论 -
LeetCode——445.两数相加II
# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: pre1,pre2 = None,No...原创 2021-01-22 11:29:58 · 75 阅读 · 0 评论 -
LeetCode——43.字符串相乘
class Solution: def multiply(self, num1: str, num2: str) -> str: #return str(int(num1)*int(num2)) if num1=='0' or num2=='0': return '0' m,n = len(num1),len(num2) res = [0]*(m+n) for i ...原创 2021-01-22 11:08:29 · 102 阅读 · 0 评论 -
LeetCode——989. 数组形式的整数加法
class Solution: def addToArrayForm(self, A: List[int], K: int) -> List[int]: #奇技淫巧 A = [str(i) for i in A] num = int(''.join(A)) res = str(num+K) return [int(i) for i in res] #进位运算 res...原创 2021-01-22 10:10:53 · 101 阅读 · 0 评论 -
LeetCode——947.移除最多的同行或者同列的石头
class Solution: def removeStones(self, stones: List[List[int]]) -> int: if len(stones)==1 or len(stones)==0: return 0 #移除的石子数量=图中点个数-极大连通分量数 #建图也比较有意思,如果他两的横坐标或者纵坐标相同,那么他俩之间就有一条边存在 n = len(sto...原创 2021-01-15 10:24:22 · 153 阅读 · 0 评论 -
LeetCode——1018.可被5整除的二进制前缀
class Solution: def prefixesDivBy5(self, A: List[int]) -> List[bool]: if not A: return [] res = [] pre = 0 #使用位运算更方便一些,使用的内存也较少,程序的执行效率也比较高 for digit in A: pre = (pre<<1)+digit...原创 2021-01-14 10:54:18 · 98 阅读 · 0 评论 -
《Leetcode of December》135.分发糖果
class Solution: def candy(self, ratings: List[int]) -> int: nums = [1]*len(ratings) #从左至右遍历 for i in range(len(ratings)): if i>0 and ratings[i]>ratings[i-1]: nums[i]=nums[i-1]+...原创 2020-12-24 15:33:25 · 78 阅读 · 0 评论 -
《Leetcode of December》738.单调递增的数字
class Solution: def monotoneIncreasingDigits(self, N: int) -> int: if N<10: return N char = [int(i) for i in str(N)] flag = len(char) for i in range(len(char)-1,0,-1): if char[i]<ch...原创 2020-12-15 14:07:31 · 66 阅读 · 0 评论 -
《Leetcode of December》118.杨辉三角
class Solution: def generate(self, numRows: int) -> List[List[int]]: #官方解法 res = [] for i in range(numRows): tmp = [] for j in range(i+1): if j==0 or j==i: ...原创 2020-12-07 15:57:20 · 93 阅读 · 0 评论 -
《Leetcode of December》263.丑数
class Solution: def isUgly(self, num: int) -> bool: if num<=1: return True if num==1 else False while num%5==0: num/=5 while num%3==0: num/=3 while num%2==0: num/=...原创 2020-12-03 15:32:16 · 107 阅读 · 0 评论 -
《Leetcode of November》最大三角形周长(面积)
class Solution: def largestTriangleArea(self, points: List[List[int]]) -> float: #海伦公式 res=0 def get_length(A,B): return ((A[0]-B[0])**2+(A[1]-B[1])**2)**0.5 for i in range(len(points)): for...原创 2020-11-29 19:29:18 · 87 阅读 · 0 评论