上一页 1 ··· 6 7 8 9 10 11 12 13 14 ··· 33 下一页
摘要: class Solution(object): def countSegments(self, s): """ :type s: str :rtype: int """ return len(s.split()) 阅读全文
posted @ 2019-03-21 07:59 周洋 阅读(146) 评论(0) 推荐(0) 编辑
摘要: class Solution(object): def topKFrequent(self, nums, k): """ :type nums: List[int] :type k: int :rtype: List[int] """ count=collections.Counter(num... 阅读全文
posted @ 2019-03-21 07:09 周洋 阅读(130) 评论(0) 推荐(0) 编辑
摘要: class Solution(object): def frequencySort(self, s): return ''.join([char * num for char, num in sorted(collections.Counter(s).items(), key=lambda x: x[1], reverse=True)]) 阅读全文
posted @ 2019-03-21 06:06 周洋 阅读(98) 评论(0) 推荐(0) 编辑
摘要: 这三个都是内置的常用高阶函数(Higher-order function),用法如下: reduce把一个函数作用在一个序列[x1, x2, x3, ...]上,这个函数必须接收两个参数,reduce把结果继续和序列的下一个元素做累积计算,其效果就是: 参考: 阅读全文
posted @ 2019-03-21 05:13 周洋 阅读(152) 评论(0) 推荐(0) 编辑
摘要: 用到一个Python知识点: 两个set可以比大小来表示是不是子集. 写的更Pythonic一点: 阅读全文
posted @ 2019-03-21 02:44 周洋 阅读(225) 评论(0) 推荐(0) 编辑
摘要: 两类函数: 找到了都返回下标. find找不到返回-1,index找不到抛出ValueError. 带r的表示从右向左找. 都可以使用第二个参数表示从哪个下标开始找. 阅读全文
posted @ 2019-03-21 00:15 周洋 阅读(109255) 评论(0) 推荐(3) 编辑
摘要: class Solution(object): def subdomainVisits(self, cpdomains): """ :type cpdomains: List[str] :rtype: List[str] """ count = {} for cpdom in cpdomain... 阅读全文
posted @ 2019-03-21 00:04 周洋 阅读(110) 评论(0) 推荐(0) 编辑
摘要: 递归,但是用Python3递归时可以加装饰器:@functools.lru_cache() 这是一个缓存工具,可以把之前算过的一些函数保存下来,加速程序运行. 本题不加这个装饰器会超时. 其实加了这个装饰器以后,是O(n)的时间复杂度,不加的话就由于函数会重复调用,时间开销更大. 阅读全文
posted @ 2019-03-20 22:42 周洋 阅读(123) 评论(0) 推荐(0) 编辑
摘要: class Solution(object): def fib(self, N): """ :type N: int :rtype: int """ F=[0]*31 F[1]=1 for i in range(2,31): F[i]=F[i-1]+F[... 阅读全文
posted @ 2019-03-20 08:07 周洋 阅读(90) 评论(0) 推荐(0) 编辑
摘要: class Solution(object): def numJewelsInStones(self, J, S): """ :type J: str :type S: str :rtype: int """ count=collections.Counter(S) ans=0... 阅读全文
posted @ 2019-03-20 07:50 周洋 阅读(84) 评论(0) 推荐(0) 编辑
上一页 1 ··· 6 7 8 9 10 11 12 13 14 ··· 33 下一页