上一页 1 2 3 4 5 6 7 ··· 15 下一页
摘要: [题目链接](207. 课程表 - 力扣(LeetCode)) 解题思路:拓扑排序。有一个可以完成的课程集合set1,有一个需要完成的课程集合set2。每次从set1中拿出一个课程,然后把其影响的节点的入度减减,如果减成了0,则该节点,变成了可以完成的课程,加入set1。依次做下去,如果set2空了 阅读全文
posted @ 2025-01-09 16:21 ouyangxx 阅读(50) 评论(0) 推荐(0)
摘要: [题目链接](202. 快乐数 - 力扣(LeetCode)) 解题思路:不是快乐数,那么就会一直循环。所以只需要按照题目意思,每次计算一个数就放入set中,如果遇到1了,直接返回True,如果之前已经存在过set中了,那么直接返回False,否则加入set中 代码 class Solution: 阅读全文
posted @ 2025-01-09 15:58 ouyangxx 阅读(16) 评论(0) 推荐(0)
摘要: [题目链接](200. 岛屿数量 - 力扣(LeetCode)) 解题思路:用感染函数,遇到1,岛屿数目就加1,然后递归把上下左右变成2(以免下次遇到相同的岛屿) 代码 class Solution: # 感染函数,将[i, j]感染 def process(self, grid, i, j): i 阅读全文
posted @ 2025-01-09 15:43 ouyangxx 阅读(15) 评论(0) 推荐(0)
摘要: [题目链接](198. 打家劫舍 - 力扣(LeetCode)) 解题思路:比较经典的动态规划。从左往右尝试。来到index位置,有两种选择,不偷,那么就去index+1位置做决策,偷,那么就去index + 2做决策。直接加dp表即可。 代码 class Solution: def process 阅读全文
posted @ 2025-01-09 15:37 ouyangxx 阅读(22) 评论(0) 推荐(0)
摘要: [题目链接](191. 位1的个数 - 力扣(LeetCode)) 解题思路:当成二进制处理即可 代码 class Solution: def hammingWeight(self, n: int) -> int: ans = 0 while n != 0: ans += n % 2 n //= 2 阅读全文
posted @ 2025-01-09 15:33 ouyangxx 阅读(6) 评论(0) 推荐(0)
摘要: [题目链接](189. 轮转数组 - 力扣(LeetCode)) 解题思路:直接举例子,nums = [1,2,3,4,5,6,7],k=3,步骤:先把5,6,7安排好,即5,6,7,4,1,2,3这是怎么来的?其实可以想象成两部分,一部分是1,2,3,4,另一部分是5,6,7先分别交换,1和5,2 阅读全文
posted @ 2025-01-09 15:30 ouyangxx 阅读(22) 评论(0) 推荐(0)
摘要: [题目链接](188. 买卖股票的最佳时机 IV - 力扣(LeetCode)) 解题思路:来到i位置,决定买或者不卖,动态规划,直接加dp表即可。 需要多一个状态位,来到i时,手中是否有股票 代码 class Solution: # 当前来到index位置,还可以买k次,state为0,则手里没有 阅读全文
posted @ 2025-01-09 14:07 ouyangxx 阅读(8) 评论(0) 推荐(0)
摘要: [题目链接](179. 最大数 - 力扣(LeetCode)) 解题思路:x拼接y大于y拼接x后,那么x就应该放前面。自定义排序就行了。 还要注意把前导0给去掉 代码 class Solution: def myCompare(self, x, y): # 比较两个字符串拼接后的结果 if str( 阅读全文
posted @ 2025-01-09 11:15 ouyangxx 阅读(13) 评论(0) 推荐(0)
摘要: [题目链接](172. 阶乘后的零 - 力扣(LeetCode)) 解题思路:首先得知道,n!一共有多少个0,取决于什么。取决于1~n所有2的因数的个数和5的因数的个数。因为0,其实就是2*5得到的。 怎么用对数时间计算1~n,2因子的个数? 一种特殊的是2的幂次的,比如2、4、8、16,含有2因子 阅读全文
posted @ 2025-01-09 10:58 ouyangxx 阅读(15) 评论(0) 推荐(0)
摘要: [题目链接](171. Excel 表列序号 - 力扣(LeetCode)) 解题思路:其实就是26进制 代码 class Solution: def titleToNumber(self, columnTitle: str) -> int: ans = 0 for s in columnTitle 阅读全文
posted @ 2025-01-09 10:34 ouyangxx 阅读(21) 评论(0) 推荐(0)
上一页 1 2 3 4 5 6 7 ··· 15 下一页