随笔分类 -  Python

上一页 1 ··· 4 5 6 7 8 9 10 11 12 13 下一页
摘要:字典类型 1、有序 or 无序:无序 2、可变 or 不可变:可变类型 字典常用操作 1、取值 #用key取值 print(my_dict['name']) print(my_dict['hobbies'][0]) # 赋值,若字典中没有对应的key,则是添加;否则做修改 my_dict['sex' 阅读全文
posted @ 2020-04-29 11:38 人间烟火地三鲜 阅读(384) 评论(0) 推荐(0)
摘要:代码一:用两个list 1 class Solution(object): 2 # 用两个list 3 def countLargestGroup(self, n): 4 """ 5 :type n: int 6 :rtype: int 7 """ 8 # 存放不同的数位和 9 listkey = 阅读全文
posted @ 2020-04-29 10:30 人间烟火地三鲜 阅读(230) 评论(0) 推荐(0)
摘要:思路: 果然做题最难得还是读懂题意:遍历arr,遇到一个0则复制一个,最后arr长度不变;所以: 遍历arr,在下标为i处遇到0则insert(i,0),并将最后一个元素pop掉,以保证arr长度不变; 1 class Solution(object): 2 def duplicateZeros(s 阅读全文
posted @ 2020-04-29 10:23 人间烟火地三鲜 阅读(127) 评论(0) 推荐(0)
摘要:思路: 1、按字符串长度升序排列,以排序后的首字符串中的元素为目标;2、遍历首字符串,每一趟遍历前要统计当前字符ch分别在首字符串和返回值ans中的个数: numi和numans;3、若ch未在ans里出现过(即numans<1),且在剩余字符串中都出现过,则将ch追加到ans中; 遍历剩余字符串时 阅读全文
posted @ 2020-04-29 10:18 人间烟火地三鲜 阅读(184) 评论(0) 推荐(0)
摘要:代码一:暴力超时。 class Solution(object): # 暴力超时 def sumEvenAfterQueries(self, A, queries): """ :type A: List[int] :type queries: List[List[int]] :rtype: List 阅读全文
posted @ 2020-04-29 10:05 人间烟火地三鲜 阅读(164) 评论(0) 推荐(0)
摘要:代码一: 1 class Solution(object): 2 def sortedSquares(self, A): 3 """ 4 :type A: List[int] 5 :rtype: List[int] 6 """ 7 ans=[] 8 for i in range(len(A)): 9 阅读全文
posted @ 2020-04-29 10:02 人间烟火地三鲜 阅读(137) 评论(0) 推荐(0)
摘要:思路:用sorted() ,详见:https://www.cnblogs.com/panweiwei/p/12712756.html 1 class Solution(object): 2 def sortArrayByParity(self, A): 3 """ 4 :type A: List[i 阅读全文
posted @ 2020-04-29 09:59 人间烟火地三鲜 阅读(106) 评论(0) 推荐(0)
摘要:1 class Solution(object): 2 def isMonotonic(self, A): 3 """ 4 :type A: List[int] 5 :rtype: bool 6 """ 7 i = 1 8 while i < len(A): 9 if A[i] == A[i - 1 阅读全文
posted @ 2020-04-29 09:53 人间烟火地三鲜 阅读(233) 评论(0) 推荐(0)
摘要:1 class Solution(object): 2 def transpose(self, A): 3 """ 4 :type A: List[List[int]] 5 :rtype: List[List[int]] 6 """ 7 hang = len(A) 8 lie = len(A[0]) 阅读全文
posted @ 2020-04-29 09:51 人间烟火地三鲜 阅读(154) 评论(0) 推荐(0)
摘要:思路: 1、找到seats中第一、最后一个1的位置,分别为:low、high;2、求得最后一个1的后面1的个数recindex(因为要坐最后那么所隔距离就为recindex);3、遍历seats从low到high的元素,用res[]存放每一段连续的0的个数;4、求得res中的最大值leng,通过le 阅读全文
posted @ 2020-04-29 09:49 人间烟火地三鲜 阅读(188) 评论(0) 推荐(0)
摘要:1 class Solution(object): 2 def flipAndInvertImage(self, A): 3 """ 4 :type A: List[List[int]] 5 :rtype: List[List[int]] 6 """ 7 for i in range(len(A)) 阅读全文
posted @ 2020-04-29 09:47 人间烟火地三鲜 阅读(143) 评论(0) 推荐(0)
摘要:思路: 1、用空格截cpdomains[i],得到访问次数num和最低级域名IP;2、用‘.’截IP得到words,用domain[]统计顶级域名(即words[-1])和最低级域名(即IP);3、若len(words) == 3说明要统计二级域名(即words[1]+words[2]);4、遍历d 阅读全文
posted @ 2020-04-29 09:43 人间烟火地三鲜 阅读(387) 评论(0) 推荐(0)
摘要:1 class Solution(object): 2 def dominantIndex(self, nums): 3 """ 4 :type nums: List[int] 5 :rtype: int 6 """ 7 m = max(nums) 8 for i in nums: 9 if i = 阅读全文
posted @ 2020-04-29 09:40 人间烟火地三鲜 阅读(179) 评论(0) 推荐(0)
摘要:1 def minCostClimbingStairs2(self, cost): 2 """ 3 :type cost: List[int] 4 :rtype: int 5 """ 6 cost1 = cost2 = 0 7 for i in cost: 8 cost1, cost2 = i + 阅读全文
posted @ 2020-04-29 09:39 人间烟火地三鲜 阅读(163) 评论(0) 推荐(0)
摘要:方法一:运用数学技巧 def pivotIndex(self, nums): """ :type nums: List[int] :rtype: int """ sumi = 0 s = sum(nums) for i in range(len(nums)): if 2 * sumi + nums[ 阅读全文
posted @ 2020-04-29 09:35 人间烟火地三鲜 阅读(209) 评论(0) 推荐(0)
摘要:1 class Solution(object): 2 def isOneBitCharacter(self, bits): 3 """ 4 :type bits: List[int] 5 :rtype: bool 6 """ 7 if len(bits) < 2: 8 return True 9 阅读全文
posted @ 2020-04-29 09:29 人间烟火地三鲜 阅读(217) 评论(0) 推荐(0)
摘要:思路: 1、nums转set去重,为newlist;2、遍历newlist找出最大频数maxnum;3、遍历newlist针对满足最大频数的元素ch,求其代表的连续子数组: a) 从左往右遍历,找到第一个ch的下标low; b) 从右往左遍历,找到第一个ch的下标high; 将该子数组长度:high 阅读全文
posted @ 2020-04-29 09:27 人间烟火地三鲜 阅读(243) 评论(0) 推荐(0)
摘要:1 class Solution(object): 2 def findLengthOfLCIS(self, nums): 3 """ 4 :type nums: List[int] 5 :rtype: int 6 """ 7 # nums为空 8 if len(nums) == 0: 9 retu 阅读全文
posted @ 2020-04-29 09:17 人间烟火地三鲜 阅读(134) 评论(0) 推荐(0)
摘要:1 class Solution(object): 2 def checkPossibility(self, nums): 3 """ 4 :type nums: List[int] 5 :rtype: bool 6 """ 7 if len(nums) < 2: 8 return True 9 # 阅读全文
posted @ 2020-04-29 09:14 人间烟火地三鲜 阅读(173) 评论(0) 推荐(0)
摘要:1 class Solution(object): 2 def imageSmoother(self, M): 3 """ 4 :type M: List[List[int]] 5 :rtype: List[List[int]] 6 """ 7 # padding:在原矩阵周围加上一圈非有效数字:- 阅读全文
posted @ 2020-04-29 09:05 人间烟火地三鲜 阅读(191) 评论(0) 推荐(0)

上一页 1 ··· 4 5 6 7 8 9 10 11 12 13 下一页