新闻动态

行业新闻企业新闻安博电竞

leetcode hot100 easy gz2zvMp8

安博电竞

两数之和 two-sum

给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。

你可以假设每种输入只会对应一个答案 。但是,数组中同一个元素在答案里不能重复出现。

leetcode hot100 easy gz2zvMp8

你可以按任意顺序返回答案。

链接:https://leetcode.cn/problems/two-sum

class Solution:def twoSum(self, nums: List[int], target: int) -> List[int]:dic = {')':'(',']':'[','}':'{'}for char in s:if char in mapping:if not stack or stack.pop() != mapping[char]:return Falseelse: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 = nextclass Solution:def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:# if not list1:#     return list2# if not list2:#     return list1dummy = curr = ListNode(0)while list1 and list2:if list1.val < list2.val:curr.next = list1list1 = list1.nextelse:curr.next = list2list2 = list2.nextcurr = curr.nextcurr.next = list1 or list2return 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] = 1dp[2] = 2for 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 = rightclass 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 = rightclass Solution:def isSymmetric(self, root: Optional[TreeNode]) -> bool:def isMirror(left,right):if not left and not right:return Trueif not left or not right:return Falsereturn 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 = rightclass Solution:def maxDepth(self, root: Optional[TreeNode]) -> int:if not root:return 0return 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 = rightclass Solution:def maxDepth(self, root: Optional[TreeNode]) -> int:if not root:return 0queue = []queue.append((1,root))depth = 1while 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 0min_price  = prices[0]max_profit = 0for cur_price in prices:if cur_price < min_price:min_price = cur_priceelse: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 0dp = [0] * len(prices)max_profit = 0for 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 = 0for num in nums:result ^= numreturn 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

chatgpt免费软件,chatgpt api 免费接口,chatgpt 聊天机器人教程,chatgpt 指令大全,chatgpt app

leetcode hot100 easy

人工智能的巅峰之作:CHATGPT与百度的融合

CHATGPT的出现标志着人工智能领域的一个重要突破 ,而其与百度的融合则带来了更多可能。通过百度的强大技术支持  ,CHATGPT的智能生成能力得以进一步提升,为用户提供更高质量、更多样化的内容。