算法
ykdsir
渣渣程序员,执着于算法却并不精通。没想过改变世界,只想着岁月
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
Leetcode Weekly Contest 196
5452. 判断能否形成等差数列思路:排序后取前两个数的差,依次检查是否满足等差。注意提示中给出的数组长度大于等于2,所以不需要考虑边界条件。class Solution: def canMakeArithmeticProgression(self, arr: List[int]) -> bool: arr = sorted(arr) num = arr[1] - arr[0] pre = arr[1] for x in原创 2020-07-05 17:40:15 · 251 阅读 · 0 评论 -
Leetcode Weekly Contest 195
1496. 判断路径是否相交思路:按照题目意思模拟即可。class Solution: def isPathCrossing(self, path: str) -> bool: path_set = set([(0,0)]) pre = (0,0) for p in path: x, y = pre if p == 'N': y += 1原创 2020-06-30 15:17:10 · 271 阅读 · 0 评论 -
Leetcode Weekly Contest 190
5416. 检查单词是否为句中其他单词的前缀思路:直接遍历数组,取前缀匹配满足的第一个字符串即可。前缀匹配可自己手写或直接用接口。class Solution: def isPrefixOfWord(self, sentence: str, searchWord: str) -> int: res = -1 for i, word in enumerate(sentence.split(' ')): if word.startswi原创 2020-05-24 14:36:39 · 220 阅读 · 0 评论 -
Leetcode Weekly Contest 189
通过这次周赛,对Python的喜爱又深了一层。5412. 在既定时间做作业的学生人数思路:因为数据量较小并且只查询一次,直接遍历比较即可。多次查询的话,考虑线段树。class Solution: def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int: res = 0 for i in range(len(startTime)):原创 2020-05-18 00:27:01 · 283 阅读 · 0 评论 -
Leetcode Bi-Weekly Contest 26
5396. 连续字符思路:用单独的变量记录前一个字符,判断当前字符和前一个字符是否相同,以此来进行计数。class Solution: def maxPower(self, s: str) -> int: pre = s[0] count = 1 res = 1 for x in s[1:]: if x == pre: count += 1 el原创 2020-05-17 23:36:49 · 145 阅读 · 0 评论 -
Leetcode Weekly Contest 188
今天的周赛题目挺有意思的。5404. 用栈操作构建数组思路:如果一个数在最后的数组里,只push;不在最后的数组里面,先push再popclass Solution: def buildArray(self, target: List[int], n: int) -> List[str]: res = [] cur = 1 for x in target: while cur != x:原创 2020-05-10 21:11:07 · 173 阅读 · 0 评论 -
Leetcode Weekly Contest 187
5400. 旅行终点站思路:直接使用dict记录出度即可from collections import defaultdictclass Solution: def destCity(self, paths: List[List[str]]) -> str: res_map = defaultdict(int) tmp = set() ...原创 2020-05-03 17:32:03 · 146 阅读 · 0 评论 -
Leetcode Weekly Contest 186
1422. 分割字符串的最大得分思路:先统计数组中所有为1的数量num,使用l记录左边0的数量,然后从第一个位置开始,依次往后遍历。当前位置为0,则l加1,为1则num减1。class Solution: def maxScore(self, s: str) -> int: num = 0 for x in s: if x...原创 2020-05-03 17:21:48 · 135 阅读 · 0 评论 -
Leetcode Weekly Contest 185
5388. 重新格式化字符串思路:统计字母个数n1和数字个数n2, 如果abs(n1-n2) > 1,不存在满足要求的字符串,返回空串。否则,相邻间隔一次插入就行。Python代码如下:class Solution: def reformat(self, s: str) -> str: res = "" ch="" num...原创 2020-04-19 16:38:31 · 175 阅读 · 0 评论 -
从集合划分角度分析动态规划
动态规划,有限集中的最优化问题。原创 2020-03-26 19:39:39 · 405 阅读 · 0 评论
分享