摘要: 1. 题目描述 2. 代码 1 class Solution: 2 def twoSum(self, nums: 'List[int]', target: int) -> 'List[int]': 3 s = {} 4 for i in range(len(nums)): 5 n = nums[i] 阅读全文
posted @ 2020-10-09 15:25 vv_869 阅读(98) 评论(0) 推荐(0) 编辑
摘要: 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( 阅读全文
posted @ 2020-10-07 17:51 vv_869 阅读(123) 评论(0) 推荐(0) 编辑
摘要: 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 % 阅读全文
posted @ 2020-10-07 15:54 vv_869 阅读(169) 评论(0) 推荐(0) 编辑
摘要: 1. 题目描述 2. 代码 1 class Solution: 2 def reverseList(self, head: ListNode) -> ListNode: 3 temp = ListNode(-1) 4 while head != None: 5 nextnode = head.nex 阅读全文
posted @ 2020-10-06 15:55 vv_869 阅读(96) 评论(0) 推荐(0) 编辑
摘要: 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 = 阅读全文
posted @ 2020-10-04 20:34 vv_869 阅读(144) 评论(0) 推荐(0) 编辑
摘要: 1. 题目描述 2. 代码 1 class Solution: 2 def hammingWeight(self, n: int) -> int: 3 return bin(n).count('1') 思路: 输出的二进制, 直接统计1的个数即可. 3. 语法整理 bin() count() 阅读全文
posted @ 2020-09-30 09:03 vv_869 阅读(73) 评论(0) 推荐(0) 编辑
摘要: 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:#遍历排序好的 阅读全文
posted @ 2020-09-29 19:33 vv_869 阅读(95) 评论(0) 推荐(0) 编辑
摘要: 1. 题目描述 2. 代码 1 class Solution: 2 def reverseBits(self, n: int) -> int: 3 return int ( bin(n)[2:].zfill(32)[::-1], 2) 思路: 先把输入的整数转换为二进制, 若不够32位则填充上0, 阅读全文
posted @ 2020-09-29 17:33 vv_869 阅读(145) 评论(0) 推荐(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 阅读全文
posted @ 2020-09-26 10:27 vv_869 阅读(101) 评论(0) 推荐(0) 编辑
摘要: 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 阅读全文
posted @ 2020-09-25 21:47 vv_869 阅读(75) 评论(0) 推荐(0) 编辑