LeetCode
斯德哥尔摩人
这个作者很懒,什么都没留下…
展开
-
LeetCode824
本题注意点:字符串处理技巧,另见 https://blog.csdn.net/zuocuomiao/article/details/82146577 'a'*3可以得到'aaa'; join()可以实现字符串数组拼接成一个字符串。import numpy as npdef toGoatLatin(S): """ :type S: str :...原创 2018-09-01 14:34:03 · 137 阅读 · 0 评论 -
LeetCode892
本题注意点:要算底为正方形的立体的表面积,参考LeetCode463“作差”思想,所有cube*6再减去z方向上下块交界处*2,再减去xy平面上交界处*2。 https://blog.csdn.net/zuocuomiao/article/details/82191840import numpy as npdef surfaceArea(grid): """ ...原创 2018-09-01 14:15:58 · 245 阅读 · 0 评论 -
LeetCode888
水题,注意多次查找在不在列表时,可以先将列表集合化。 import numpy as npdef fairCandySwap(A, B): """ :type A: List[int] :type B: List[int] :rtype: List[int] """ t = (sum(A)+sum(B))//2-sum(A) b ...原创 2018-09-01 12:10:58 · 220 阅读 · 0 评论 -
LeetCode575
本题注意点:set()函数可以将list集合化,而不需要一个一个添加。class Solution(object): def distributeCandies(self, candies): """ :type candies: List[int] :rtype: int """ return min(...原创 2018-08-29 20:49:28 · 136 阅读 · 0 评论 -
LeetCode463
本题注意点:关键还是怎么求周长,我用的“求和”思想 ,即把周长分为如下两部分: ① 0、1交界处; ② 1与矩阵边界交界处。还可以用“作差”思想,即每个1先算4次,再减去1、1交界的情况*2。import numpy as npdef islandPerimeter(grid): """ :type grid: List[List[int]...原创 2018-08-29 20:13:23 · 223 阅读 · 0 评论 -
LeetCode811
本题注意点:字典,此题解决字符串计数。另外注意: 加入键值对:用d[key]=… 访问键值对:用d.get(key, default=None),其中default表示值不在字典中的返回值 所有键值对:用d.items()import numpy as npdef subdomainVisits(cpdomains): """ :type cpdoma...原创 2018-08-29 19:39:18 · 205 阅读 · 0 评论 -
LeetCode669
本题注意点:if root.val<L,直接return即可# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right ...原创 2018-08-29 13:57:29 · 215 阅读 · 0 评论 -
LeetCode682
水题import numpy as npdef calPoints(ops): """ :type ops: List[str] :rtype: int """ nums = [] for op in ops: if op == '+': nums.append(nums[-1] + nums[-2]) ...原创 2018-08-29 12:50:58 · 175 阅读 · 0 评论 -
LeetCode590
水题 """# Definition for a Node.class Node(object): def __init__(self, val, children): self.val = val self.children = children"""class Solution(object): def postorder(self...原创 2018-08-29 12:40:46 · 190 阅读 · 0 评论 -
LeetCode766
本题注意点: 刚从LeetCode500学会all的用法,现学现卖。 https://blog.csdn.net/zuocuomiao/article/details/82177557import numpy as npdef isToeplitzMatrix(matrix): """ :type matrix: List[List[int]] :rty...原创 2018-08-29 12:31:55 · 144 阅读 · 0 评论 -
LeetCode804
本题主要注意python中集合的定义、添加、取大小,以及单个字符转数值的处理。 import numpy as npdef uniqueMorseRepresentations(words): z = [".-","-...","-.-.","-..",".","..-.","--.","....",".."原创 2018-08-25 18:28:02 · 231 阅读 · 0 评论 -
LeetCode771
本题注意python中集合的使用即可,s = set('abc') 的结果是{'b','a','c'},python集合的元素顺序是随机的,而要得到{'abc'},则可以用s = set(['abc']),或者s = set() 再s.add('abc')。 总之,可以将set()理解为强制类型转换,set('abc')是将一个串集合化,而set(['abc'])是将一个列表集合化。...原创 2018-08-25 18:50:10 · 255 阅读 · 0 评论 -
LeetCode429
水题,参考LeetCode637 https://blog.csdn.net/zuocuomiao/article/details/82251164"""# Definition for a Node.class Node(object): def __init__(self, val, children): self.val = val ...原创 2018-09-02 09:53:08 · 286 阅读 · 0 评论 -
LeetCode566
本题注意点:list平整化+切片技巧实现reshape,或者借助numpy.array投机取巧,但是较慢。def matrixReshape(nums, r, c): """ :type nums: List[List[int]] :type r: int :type c: int :rtype: List[List[int]] """ ...原创 2018-08-30 10:35:22 · 124 阅读 · 0 评论 -
LeetCode412
水题 import numpy as npdef fizzBuzz(n): """ :type n: int :rtype: List[str] """ ans = [] for i in range(1, n+1): if i % 15 == 0: ans.append('FizzBuzz') ...原创 2018-08-30 09:24:53 · 165 阅读 · 0 评论 -
LeetCode700
水题 # Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution(object):...原创 2018-08-30 08:53:36 · 194 阅读 · 0 评论 -
LeetCode226
本题注意点:root.left, root.right = root.right, root.left # Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# ...原创 2018-09-01 19:31:37 · 171 阅读 · 0 评论 -
LeetCode521
题意有点绕,水题class Solution(object): def findLUSlength(self, a, b): """ :type a: str :type b: str :rtype: int """ if a == b: return -1 return...原创 2018-09-01 19:19:20 · 218 阅读 · 0 评论 -
LeetCode589
水题 """# Definition for a Node.class Node(object): def __init__(self, val, children): self.val = val self.children = children"""class Solution(object): def preorder(self,...原创 2018-09-01 19:06:30 · 231 阅读 · 0 评论 -
LeetCode461
本题注意点: 1、位运算及十进制转二级制:这个不难想到; 2、对字符串进行字符统计:str.count(),原来用循环实现,可读性差。import numpy as npdef hammingDistance(x, y): """ :type x: int :type y: int :rtype: int """ ...原创 2018-08-25 19:11:47 · 190 阅读 · 0 评论 -
LeetCode709
本题注意点:python中字符串大小写转换 import numpy as npdef toLowerCase(str): """ :type str: str :rtype: str """ return str.lower()# samples:print(toLowerCase('Hello'))print(toLowerCase...原创 2018-08-25 18:55:37 · 165 阅读 · 0 评论 -
LeetCode872
本题注意点:函数嵌套避免全局变量(leaves)、 或者直接返回list(version2)def leafSimilar(root1, root2): """ :type root1: TreeNode :type root2: TreeNode :rtype: bool """ def get_leaves(root): ...原创 2018-08-29 10:52:29 · 159 阅读 · 0 评论 -
LeetCode868
本题注意点: 类似于LeetCode821 https://blog.csdn.net/zuocuomiao/article/details/82150516 另外注意思维习惯的转变,以前用C++遇到max、∀问题都习惯设一个辅助变量MAX、ok,而在python中用列表生成式更简洁。import numpy as npdef binaryGap(N): """ ...原创 2018-08-29 10:01:26 · 200 阅读 · 0 评论 -
LeetCode883
本题注意点:二维list的骚操作, 包括① 列表生成式,② map() 理解: ① 类比数据库中的SELECT…FROM…WHERE,如生成100以内的完全平方数 [x * x for x in range(1, 11) if x % 2 == 0] 上方代码得到一个list,然后还可以继续套sum、max等 ② 参...原创 2018-08-28 10:52:10 · 403 阅读 · 0 评论 -
LeetCode806
本题注意点:字符转数值的处理,或者说ASCII码import numpy as npdef numberOfLines(widths, S): """ :type widths: List[int] :type S: str :rtype: List[int] """ cur = 0 lines = 1 for i in ...原创 2018-08-28 09:36:11 · 140 阅读 · 0 评论 -
LeetCode876
辣鸡题目,最后发现并没有头结点,注意快慢双指针法(还可用于判环)def middleNode(self, head): """ :type head: ListNode :rtype: ListNode """ L = 0 p = head while(p): ...原创 2018-08-28 09:14:41 · 160 阅读 · 0 评论 -
LeetCode561
本题注意点:list的骚操作 1、sum(list)求和; 2、list[::step] 得到子列表import numpy as npdef arrayPairSum(nums): """ :type nums: List[int] :rtype: int """ nums.sort() return sum(nums...原创 2018-08-27 21:09:42 · 152 阅读 · 0 评论 -
LeetCode728
本题注意点:整除用//import numpy as npdef selfDividingNumbers(left, right): """ :type left: int :type right: int :rtype: List[int] """ def self_divided(x): t = [] y...原创 2018-08-27 20:50:14 · 133 阅读 · 0 评论 -
LeetCode852
本题注意点:循环即能通过, 当然二分更快import numpy as npdef peakIndexInMountainArray(A): """ :type A: List[int] :rtype: int """ for i in range(1, len(A)): if A[i] > A[i-1] and A[i] &...原创 2018-08-27 20:30:24 · 144 阅读 · 0 评论 -
LeetCode617
本题注意点:python二叉树类实现、类递归函数(self.)import numpy as npclass TreeNode(object): def __init__(self, x): self.val = x self.left = None self.right = None def CreatTree(self,...原创 2018-08-27 20:21:43 · 175 阅读 · 0 评论 -
LeetCode136★★★
本题注意点:利用异或找出唯一一个出现奇数次的数(其余数都是偶数次),异或的性质参考链接: https://www.cnblogs.com/suoloveyou/archive/2012/04/25/2470292.htmlimport numpy as npdef singleNumber(nums): """ :type nums: List[int] ...原创 2018-08-31 09:30:20 · 324 阅读 · 0 评论 -
LeetCode657
本题注意点:字符统计str.count() import numpy as npdef judgeCircle(moves): """ :type moves: str :rtype: bool """ return moves.count('L') == moves.count('R') and moves.count('U') == moves...原创 2018-08-26 14:53:46 · 154 阅读 · 0 评论 -
LeetCode476
本题注意点:二/十进制互转、异或位运算实现0、1互转 import numpy as npdef findComplement(num): """ :type num: int :rtype: int """ t = bin(num)[2:] s = '' for i in range(len(t)): if t[i...原创 2018-08-28 13:32:56 · 148 阅读 · 0 评论 -
LeetCode557
本题注意点:str.split(' ')的逆操作,其中join() 用于将序列中的元素以指定的字符连接生成一个字符串,strip()用于删除头尾空格。import numpy as npdef reverseWords(s): """ :type s: str :rtype: str """ words = s.split(' ') rev...原创 2018-08-28 13:54:28 · 203 阅读 · 0 评论 -
LeetCode500
本题注意点:∀、∃、差集、空集判断,分别对应all、any、-、not,所以说离散数学还是很有用的。通过all、any,再也不需要立一个flag了。import numpy as npdef findWords(words): """ :type words: List[str] :rtype: List[str] """ keys = [ ...原创 2018-08-29 09:26:40 · 187 阅读 · 0 评论 -
LeetCode292
水题,不过挺有意思的QAQ。class Solution(object): def canWinNim(self, n): """ :type n: int :rtype: bool """ return n%4 != 0原创 2018-08-31 21:24:44 · 135 阅读 · 0 评论 -
LeetCode821
本题注意点: 1、利用index()查找,可能找不到时要用try语句; 2、查找所有值为x的indexes,可以用内置函数enumerate(),详见version2; 3、version3用动态规划,效率更高。import numpy as npdef shortestToChar(S, C): """ :type S: str :ty...原创 2018-08-28 16:48:16 · 176 阅读 · 0 评论 -
LeetCode559
本题注意点:not root与root == None的区别,参考链接 https://blog.csdn.net/johinieli/article/details/80141757class Solution(object): def maxDepth(self, root): """ :type root: Node :rtype...原创 2018-08-28 15:31:25 · 204 阅读 · 0 评论 -
LeetCode762
本题注意点:特殊之处在于二进制1的个数有限,先手动求出所有可能素数更快。import numpy as npimport mathdef countPrimeSetBits(L, R): """ :type L: int :type R: int :rtype: int """ def is_prime(x): if x...原创 2018-08-31 10:38:03 · 209 阅读 · 0 评论 -
LeetCode693
本题注意点:想到了位运算,但第一反应还是先转成二进制 ,其实可以用十进制进行位运算。import numpy as npdef hasAlternatingBits(n): """ :type n: int :rtype: bool """ # version1: (我) t = bin(n)[2:] for i in range(...原创 2018-08-31 09:45:26 · 216 阅读 · 0 评论