自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(49)
  • 收藏
  • 关注

原创 ​​KNN算法

下图为KNN算法的伪代码,截自《机器学习实战》 P19:           import numpy as npimport operatorfrom os import listdirdef CreatDataSet(): group = np.array([[1.0, 1.1], [1.0, 1.0], [0, 0], [0, 0.1]]) labels...

2018-09-04 14:11:29 155

原创 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 283

原创 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 161

原创 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 210

原创 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 224

原创 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 130

原创 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 239

原创 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 214

原创 LeetCode292

水题,不过挺有意思的QAQ。class Solution(object): def canWinNim(self, n): """ :type n: int :rtype: bool """ return n%4 != 0 

2018-08-31 21:24:44 123

原创 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 201

原创 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 208

原创 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 317

原创 LeetCode637

 本题注意点:二叉树的逐层遍历,孩子结点入队的同时统计该层孩子结点的总数n,然后出队时将n个结点出队。# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# ...

2018-08-31 09:28:54 208

原创 LeetCode104

水题 # 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-31 08:53:39 120

原创 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 116

原创 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 157

原创 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 189

原创 LeetCode575

本题注意点:set()函数可以将list集合化,而不需要一个一个添加。class Solution(object): def distributeCandies(self, candies): """ :type candies: List[int] :rtype: int """ return min(...

2018-08-29 20:49:28 131

原创 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 214

原创 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 201

原创 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 211

原创 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 168

原创 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 185

原创 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 133

原创 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 151

原创 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 194

原创 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 181

原创 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 164

原创 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 192

原创 LeetCode884

本题注意点:先化简(合并A、B) ,再求值import numpy as npdef uncommonFromSentences(A, B): """ :type A: str :type B: str :rtype: List[str] """ C = (A + ' ' + B).split(' ') ans = [] f...

2018-08-28 14:14:37 238

原创 LeetCode344

 本题注意点:串的reversedef reverseString(self, s): """ :type s: str :rtype: str """ return s[::-1] 

2018-08-28 13:57:34 114

原创 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 192

原创 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 144

原创 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 400

原创 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 131

原创 LeetCode876

辣鸡题目,最后发现并没有头结点,注意快慢双指针法(还可用于判环)def middleNode(self, head): """ :type head: ListNode :rtype: ListNode """ L = 0 p = head while(p): ...

2018-08-28 09:14:41 151

原创 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 144

原创 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 126

原创 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 138

原创 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 165

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除