目录

​一、加一​

​1、题目描述​

​2、题解​

​3、源码​

​二、二进制求和​

​1、题目描述​

​2、题解​

​3、源码​

​三、x的平方根​

​1、题目描述​

​2、题解​

​3、源码​

​四、爬楼梯​

​1、题目描述​

​2、题解​

​3、源码​

​五、简化路径​

​1、题目描述​

​2、题解​

​3、源码​


一、加一

1、题目描述

LeetCode刷题(66题、67、69、70、71)_git

2、题解

LeetCode刷题(66题、67、69、70、71)_python_02

3、源码

class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
idx = len(digits) - 1
while idx >= 0 and digits[idx] == 9:
digits[idx] = 0
idx -= 1
if idx >= 0:
digits[idx] +=1
else:
digits = [1] + digits
return digits

二、二进制求和

1、题目描述

LeetCode刷题(66题、67、69、70、71)_python_03

2、题解

LeetCode刷题(66题、67、69、70、71)_git_04

3、源码

class Solution:
def addBinary(self, a: str, b: str) -> str:
return bin(int(a, 2)+int(b, 2))[2:]


三、x的平方根

1、题目描述

LeetCode刷题(66题、67、69、70、71)_python_05

2、题解

LeetCode刷题(66题、67、69、70、71)_python_06

3、源码

class Solution:
def mySqrt(self, x: int) -> int:
return int(x ** 0.5)

四、爬楼梯

1、题目描述

LeetCode刷题(66题、67、69、70、71)_python_07

2、题解

LeetCode刷题(66题、67、69、70、71)_python_08

3、源码

class Solution:
def climbStairs(self, n: int) -> int:
dp = [0] * (n + 1)
dp[0] = dp[1] = 1
for i in range(2, n + 1):
dp[i] = dp[i - 1] + dp[i - 2]
return dp[-1]

五、简化路径

1、题目描述

LeetCode刷题(66题、67、69、70、71)_git_09

2、题解

LeetCode刷题(66题、67、69、70、71)_python_10

3、源码

class Solution:
def simplifyPath(self, path: str) -> str:
names = path.split("/")
stack = list()
for name in names:
if name == "..":
if stack:
stack.pop()
elif name and name != ".":
stack.append(name)
return "/" + "/".join(stack)