- 博客(9)
- 收藏
- 关注
原创 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 648
原创 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 327
原创 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 243
原创 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 1307
转载 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 1103 1
原创 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 926
原创 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 548
原创 LeetCode Python 第n个丑数
挨个找的方法, 效率比较低def findKthUgly(k): count = 0 n = 1 while True: if isUgly(n): count += 1 if count == k: return n else: n += 1...
2018-07-31 23:12:35 1690
原创 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 602
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人