随笔分类 - LeetCode
摘要:1. 题目描述 2. 代码 1 class MinStack: 2 def __init__(self): 3 self.stack = [] 4 self.min_stack = [] 5 6 def push(self, x: int) -> None: 7 self.stack.append(
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def isValid(self, s: str) -> bool: 3 n = len(s)#获取字符串的长度 4 if n == 0:#如果长度为0,则表示空串,本题认为是合法的串 5 return True 6 if n %
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def reverseList(self, head: ListNode) -> ListNode: 3 temp = ListNode(-1) 4 while head != None: 5 nextnode = head.nex
阅读全文
摘要:1. 题目描述 2. 代码 1 # Definition for singly-linked list. 2 # class ListNode: 3 # def __init__(self, val=0, next=None): 4 # self.val = val 5 # self.next =
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def hammingWeight(self, n: int) -> int: 3 return bin(n).count('1') 思路: 输出的二进制, 直接统计1的个数即可. 3. 语法整理 bin() count()
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def missingNumber(self, nums: List[int]) -> int: 3 nums.sort() #对nums排序 4 prenum = 0 #默认从0开始 5 for x in nums:#遍历排序好的
阅读全文
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def reverseBits(self, n: int) -> int: 3 return int ( bin(n)[2:].zfill(32)[::-1], 2) 思路: 先把输入的整数转换为二进制, 若不够32位则填充上0,
阅读全文
摘要:1. 题目描述 https://leetcode-cn.com/problems/single-number/ 2. 代码 1 class Solution: 2 def singleNumber(self, nums: List[int]) -> int: 3 a = 0 4 for i in n
阅读全文
摘要:1. 题目描述 https://leetcode-cn.com/problems/hamming-distance/ 2. 代码 1 class Solution: 2 def hammingDistance(self, x: int, y: int) -> int: 3 return bin(x
阅读全文
摘要:1. 题目描述 https://leetcode-cn.com/problems/valid-palindrome/ 2. 代码 1 class Solution: 2 def isPalindrome(self, s: str) -> bool: 3 S = list()#定义一个列表,模拟栈 4
阅读全文
摘要:1. 题目描述 https://leetcode-cn.com/problems/longest-common-prefix/solution/ 2. 代码 class Solution: def longestCommonPrefix(self, strs: 'List[str]') -> str
阅读全文
摘要:1. 题目描述 https://leetcode-cn.com/problems/pascals-triangle/ 2. 代码 class Solution: def generate(self, numRows: int) -> 'List[List[int]]': result = []#定义
阅读全文