leetcode hot100 easy
Posted HardyDragon_CC
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode hot100 easy相关的知识,希望对你有一定的参考价值。
两数之和 two-sum
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
你可以按任意顺序返回答案。
链接:https://leetcode.cn/problems/two-sum
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
dic =
for index, num in enumerate(nums):
if target - num in dic:
return [index, dic[target - num]]
dic[num] = index
return []
time: O n
size: O 1
Valid Parentheses
https://leetcode.cn/problems/valid-parentheses/
class Solution:
def isValid(self, s: str) -> bool:
stack = []
mapping = ')':'(',']':'[','':''
for char in s:
if char in mapping:
if not stack or stack.pop() != mapping[char]:
return False
else:
stack.append(char)
return not stack
time: O n
size: O m (m = 左括号的数量,一般来说比n小)
Merge Two Sorted Lists
https://leetcode.cn/problems/merge-two-sorted-lists/
- 链表需要自定义
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
# if not list1:
# return list2
# if not list2:
# return list1
dummy = curr = ListNode(0)
while list1 and list2:
if list1.val < list2.val:
curr.next = list1
list1 = list1.next
else:
curr.next = list2
list2 = list2.next
curr = curr.next
curr.next = list1 or list2
return dummy.next
time: O n
size: O 1
Climbing Stairs
https://leetcode.cn/problems/climbing-stairs/
class Solution:
def climbStairs(self, n: int) -> int:
if n == 1:
return 1
dp = [0] * (n + 1)
dp[1] = 1
dp[2] = 2
for i in range(3,n+1):
dp[i] = dp[i-2] + dp[i-1]
return dp[n]
time: O n
size: O n
Binary Tree Inorder Traversal
https://leetcode.cn/problems/binary-tree-inorder-traversal/?favorite=2cktkvj
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
res = []
def inorder(root):
if root == None:
return
inorder(root.left)
res.append(root.val)
inorder(root.right)
inorder(root)
return res
time: O n
size: O h (树的高度)
Symmetric Tree
https://leetcode.cn/problems/symmetric-tree/
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isSymmetric(self, root: Optional[TreeNode]) -> bool:
def isMirror(left,right):
if not left and not right:
return True
if not left or not right:
return False
return left.val == right.val and isMirror(left.left,right.right) and isMirror(left.right,right.left)
return isMirror(root,root)
time: O n
size: O h (树的高度)
Maximum Depth of Binary Tree
https://leetcode.cn/problems/maximum-depth-of-binary-tree/
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1
time: O n
size: O h (树的高度) 最大可能为 n
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
queue = []
queue.append((1,root))
depth = 1
while queue:
cur_depth,cur_node = queue.pop(0)
depth = max(depth,cur_depth)
if cur_node.left:
queue.append((depth+1,cur_node.left))
if cur_node.right:
queue.append((depth+1,cur_node.right))
return depth
time: O n
size: O n (节点个数)
Best Time to Buy and Sell Stock
https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/
class Solution:
def maxProfit(self, prices: List[int]) -> int:
if len(prices) < 2:
return 0
min_price = prices[0]
max_profit = 0
for cur_price in prices:
if cur_price < min_price:
min_price = cur_price
else:
max_profit = max(max_profit, cur_price - min_price)
return max_profit
贪心 greedy
time: O n
size: O 1
class Solution:
def maxProfit(self, prices: List[int]) -> int:
if len(prices) < 2:
return 0
dp = [0] * len(prices)
max_profit = 0
for i in range(1,len(prices)):
dp[i] = max(dp[i-1] + prices[i] - prices[i-1],0)
# print(dp)
return max(dp)
dp[i] 定义: 在 i 天卖出的最大利润.相当于 prices[i] - prices[0-i区间的最小值index]
time: O n
size: O n
Single Number
class Solution:
def singleNumber(self, nums: List[int]) -> int:
result = 0
for num in nums:
result ^= num
return result
异或符号为:^ 。 0与任何数异或都为任何数本身。 相同为0,不同为1。
class Solution:
def singleNumber(self, nums: List[int]) -> int:
res = nums[0]
for i in range(1,len(nums)):
res ^= nums[i]
return res
time: O n
size: O 1
以上是关于leetcode hot100 easy的主要内容,如果未能解决你的问题,请参考以下文章
#yyds干货盘点# LeetCode 热题 HOT 100:四数之和
#yyds干货盘点# LeetCode 热题 HOT 100:组合总和