随笔分类 - LeetCode
刷过的题记录一下,思路仅供参考,并非最优解。
摘要:class MagicDictionary(object): def __init__(self): """ Initialize your data structure here. """ self.mydict = {} def buildDict(self, dictionary): """
阅读全文
摘要:class Solution(object): def displayTable(self, orders): """ :type orders: List[List[str]] :rtype: List[List[str]] """ # 返回值 ans = [] # 第一趟遍历,listFood统
阅读全文
摘要:class Solution(object): def numRabbits(self, answers): """ :type answers: List[int] :rtype: int """ if not answers: return 0 mydict = {} for item in a
阅读全文
摘要:思路:将单词拆分出来的字符作为dict的key即可。 注意:python中key不能是list,需要转成tuple类型。 class Solution(object): def groupAnagrams(self, strs): """ :type strs: List[str] :rtype:
阅读全文
摘要:class Solution(object): def topKFrequent(self, words, k): """ :type words: List[str] :type k: int :rtype: List[str] """ # 用字典统计每个单词的数量 mydict = {} for
阅读全文
摘要:class FindElements(object): def getFolderNames(self, names): """ :type names: List[str] :rtype: List[str] """ mydict = {} res = [] for item in names:
阅读全文
摘要:class FindElements(object): # treeNode.left.val == 2 * x + 1 # treeNode.right.val == 2 * x + 2 def __init__(self, root): """ :type root: TreeNode """
阅读全文
摘要:class Solution(object): def __init__(self): self.mydict = {} # 1、dfs求每个节点的子树和,并用字典统计 # 2、遍历字典,按values域的大小降序排:mylist = sorted(mydict.items(), key = lam
阅读全文
摘要:class Solution(object): def largestValsFromLabels(self, values, labels, num_wanted, use_limit): """ :type values: List[int] :type labels: List[int] :t
阅读全文
摘要:代码思路:横纵坐标的平方和,放到新list中,然后将新list升序排,取前K个即可。 class Solution(object): def kClosest(self, points, K): """ :type points: List[List[int]] :type K: int :rtyp
阅读全文
摘要:代码思路:用文件内容作key域,value域需要拼接,是文件全名。 import re class Solution(object): def findDuplicate(self, paths): """ :type paths: List[str] :rtype: List[List[str]]
阅读全文
摘要:class Solution(object): def frequencySort(self, s): """ :type s: str :rtype: str """ mydict = {} for item in s: if item in mydict.keys(): mydict[item]
阅读全文
摘要:注:sorted()可以排字符串。 class Solution(object): def groupAnagrams(self, strs): """ :type strs: List[str] :rtype: List[List[str]] """ mydict = {} for item in
阅读全文
摘要:代码一:快慢指针 class Solution(object): # 快慢指针 def detectCycle(self, head): """ :type head: ListNode :rtype: ListNode """ slow, fast = head, head finder = he
阅读全文
摘要:代码一:双指针 class Solution(object): # 双指针 def pairSums(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[List[int]] """ if not
阅读全文
摘要:class Solution(object): def reverseWords(self, s): """ :type s: str :rtype: str """ words = s.split() return ' '.join(words[::-1]) if __name__ == '__m
阅读全文
摘要:class Solution(object): def uniqueOccurrences(self, arr): """ :type arr: List[int] :rtype: bool """ mydict = {} for item in arr: if item in mydict.key
阅读全文
摘要:代码一思路: 设两个指针cur、i,cur是有效元素下标,初值为0,i是从第一个元素开始的遍历指针;另外用count记录当前相同元素的个数; i和cur指向的元素相同且count值小于2,说明cur指向的元素出现第二次,为有效元素,cur、i右移,count计数器加1; i和cur指向的元素相同且c
阅读全文
摘要:代码一:用字典。 class Solution(object): def smallerNumbersThanCurrent(self, nums): """ :type nums: List[int] :rtype: List[int] """ mydict = {} temp = [] for
阅读全文
摘要:class Solution(object): # 思路: # 从左往右遍历A,分别设置计数器记录上坡up和下坡down的长度; # 当不满足上坡了,就计数下坡; # 当下坡也不满足了,记录当前山脉的长度=up+down+1 # 遍历时若遇到两个元素相等,则跳过。 def longestMounta
阅读全文