摘要: #平方根的值一定在1,x之间,是一个有序数组 #那么就满足二分查找的条件 class Solution: def mySqrt(self, x: int) -> int: if x==0 or x==1: return x #0和1的算术平方根为1 low,high,res = 1,x,-1 #初始 阅读全文
posted @ 2022-08-30 14:05 是冰美式诶 阅读(16) 评论(0) 推荐(0) 编辑
摘要: -< bin() 返回一个整数 int 或者长整数 long int 的二进制表示 ->: class Solution: def addBinary(self, a: str, b: str) -> str: return bin(int(a,2)+int(b,2))[2:]#先将a,b转为2进制 阅读全文
posted @ 2022-08-30 14:04 是冰美式诶 阅读(14) 评论(0) 推荐(0) 编辑
摘要: 66. 加一 -< map(function, iterable, ...) 现对map函数就行如下的解释: map()返回一个与传入可迭代对象大小一样的map对象 经常与Python中的lambda函数搭配 test = map(lambda n :n*n,range(1,11)) print(l 阅读全文
posted @ 2022-07-31 15:34 是冰美式诶 阅读(14) 评论(0) 推荐(0) 编辑
摘要: 67. 二进制求和 -< bin() 返回一个整数 int 或者长整数 long int 的二进制表示 ->: class Solution: def addBinary(self, a: str, b: str) -> str: return bin(int(a,2)+int(b,2))[2:]# 阅读全文
posted @ 2022-07-31 15:34 是冰美式诶 阅读(14) 评论(0) 推荐(0) 编辑
摘要: 63. 不同路径 II -> 就前一题而言,在其基础上添加判断条件,当遇到障碍时,置为0并在边界条件下也需要做判断,话不多说 -<: class Solution: def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) - 阅读全文
posted @ 2022-07-31 15:32 是冰美式诶 阅读(13) 评论(0) 推荐(0) 编辑
摘要: 64. 最小路径和 -< 动态规划,dp[i][j]=dp[i-1][j]+dp[i][j-1],但是其中边界等于grid[i][0]和grid[0][j] 代码: class Solution: def minPathSum(self, grid: List[List[int]]) -> int: 阅读全文
posted @ 2022-07-31 15:32 是冰美式诶 阅读(16) 评论(0) 推荐(0) 编辑
摘要: 65. 有效数字 个人感觉这种题目没太有意思 class Solution: def isNumber(self, s: str) -> bool: if s in ["inf", "-inf", "+inf", "Infinity", "+Infinity", "-Infinity"]: retu 阅读全文
posted @ 2022-07-31 15:32 是冰美式诶 阅读(10) 评论(0) 推荐(0) 编辑
摘要: 62.不同路径 -< 往下n次往右m次,从起点开始需移动n-1和m-1次 因此在n+m-2中选取n-1次,其余向右走 demo1: class Solution: def uniquePaths(self, m: int, n: int) -> int: return comb(n+m-2,n-1) 阅读全文
posted @ 2022-07-31 15:30 是冰美式诶 阅读(17) 评论(0) 推荐(0) 编辑
摘要: 60. 排列序列 -> 方式一:找规律 eg:n=3,k=3 '123' '132' '213' '231' '312' '321' 每个首位1 2种 即找到3/2即可 -< 代码: class Solution: def getPermutation(self, n: int, k: int) - 阅读全文
posted @ 2022-07-31 15:29 是冰美式诶 阅读(22) 评论(0) 推荐(0) 编辑
摘要: 61. 旋转链表 ->每个节点都要移动,因此想到连接到一起 1.找尾节点,形成环形链表 2.尾节点移动length-k步(又移k步==左移length-k步) 3.找到头节点,断开头尾连接 -<代码: # Definition for singly-linked list. # class List 阅读全文
posted @ 2022-07-31 15:29 是冰美式诶 阅读(11) 评论(0) 推荐(0) 编辑
点击右上角即可分享
微信分享提示