刷刷刷
Skye要一直努力
这个作者很懒,什么都没留下…
展开
-
Leetcode Python N叉树的层序遍历
"""# Definition for a Node.class Node(object): def __init__(self, val, children): self.val = val self.children = children"""def levelOrder(root): if not root: ret...转载 2018-08-03 20:33:31 · 1122 阅读 · 1 评论 -
LintCode&Python A+B 问题
想要用位运算的方法来做,a和b的异或等于不进位的加法,a与b左移一位等于进位,有进位的情况下循环将进位和不进位的和相加。def aplusb(self, a, b): # write your code here while b != 0: s = a^b c = (a&b) << 1...原创 2018-07-30 23:04:28 · 610 阅读 · 0 评论 -
LintCode Python 第k大元素
想到的第一个方法,遍历list,将第k大元素及比他大的存在单独的list中, 没有ACdef kthLargestElement(self, k, A): if len(A) < k: return None tmp = [-float('inf')] * k for i in range(len(A)): j = 0 whil...原创 2018-08-01 23:38:58 · 563 阅读 · 0 评论 -
LintCode Python 序列化和反序列化二叉树
"""Definition of TreeNode:class TreeNode: def __init__(self, val): self.val = val self.left, self.right = None, None"""from collections import dequeclass solution: def...原创 2018-08-02 15:50:46 · 935 阅读 · 0 评论 -
LeetCode 501 二叉搜索树中的众数
基础版,利用hash,即python中的字典,key存节点,value存出现的次数class solution: def findMode(self, root): tmp = {} ans = [] if not root: return ans def countNode(r): ...原创 2018-08-08 18:49:17 · 1333 阅读 · 0 评论 -
LeetCode 506 相对名次
class solution: def findRelativeRanks(self, nums): sorted_nums = sorted(nums) rank = ['Gold Medal', 'Silver Medal', 'Bronze Medal']+[str(i) for i in range(4, len(nums)+1)] ...原创 2018-08-08 19:50:25 · 259 阅读 · 0 评论 -
LeetCode 完美数
常规操作会超时,提升速度的方法:class solution: def checkPerfectNumber(self, num): ans = 1 if num%2 != 0: return False for i in range(1, num): if num%(2**i) == 0...原创 2018-08-08 19:54:32 · 340 阅读 · 0 评论 -
LeetCode 538 python 把二叉搜索树转换为累加树
思路:root.val += root.right root.left += root.val# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# ...原创 2018-08-08 22:08:19 · 658 阅读 · 0 评论