- 博客(51)
- 资源 (1)
- 收藏
- 关注
原创 MySQL
1、sql文件导入到MySQL数据库一、创建数据库 create database 数据库名;二、use 数据库名;三、source sql文件的地址;就可以把表导入到数据库中了。
2019-03-05 16:36:22
158
转载 python 多个分隔符 split
https://blog.youkuaiyun.com/lucky_ricky/article/details/78202572
2019-02-21 17:32:55
301
原创 979. Distribute Coins in Binary Tree——depth
题目分析: 每个节点需要移动的次数是 子节点需要移动的+当前的个数-1Then, the number of moves we make from this node to and from its children is abs(dfs(node.left)) + abs(dfs(node.right))class TreeNode(object): def __init...
2019-02-19 17:39:15
210
原创 108. Convert Sorted Array to Binary Search Tree——tree
题目分析:二叉树的高度差不能超过1class TreeNode(object): def __init__(self, x): self.val = x self.left = None self.right = Noneclass Solution(object): def sortedArrayToBST(self,...
2019-02-19 16:16:55
174
原创 python取整的方法
1、向下取整: int()>>> a = 14.38>>> int(a)142、向上取整:ceil()使用ceil()方法时需要导入math模块,例如>>> import math>>> math.ceil(3.33)4>>> math.ceil(3.88)43、四
2019-02-19 16:06:26
989
原创 894. All Possible Full Binary Trees——tree
题目分析:输出所有的完全二叉树class TreeNode(object): def __init__(self, x): self.val = x self.left = None self.right = Noneclass Solution(object): def __init__(self): ...
2019-02-19 15:41:03
187
原创 654. Maximum Binary Tree——tree
题目分析:思路一:采用递归,分为三步,分别为获取指定数组序列的最大值,构建子树,返回根节点。获取指定数组最大值findmax函数(Python自带max函数):参数为数组nums,起始位置start,终止位置end,最大值max。求出数组中,指定了起始位置和终止位置后的最大值,并返回最大值所在数组的下标index,如果起始位置等于终止位置,返回index=-1。构建子树getsub...
2019-02-18 20:21:03
165
原创 701. Insert into a Binary Search Tree——tree
题目分析:val值比当前值小向左查找,否则向右查找,否则插入# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = N...
2019-02-18 17:50:00
121
原创 617. Merge Two Binary Trees——tree
class Solution(object): def mergeTrees(self, t1, t2): """ :type t1: TreeNode :type t2: TreeNode :rtype: TreeNode """ if not t1: return...
2019-02-18 17:34:02
146
原创 983. Minimum Cost For Tickets——dp
题目分析:可以买一天票,七天票,30天票 class Solution(object): def generateParenthesis(self, days, costs): """ :type n: int :rtype: List[str] """ dp = [0]*366 ...
2019-02-15 16:07:59
134
原创 877. Stone Game——dp
题目分析:dp[i][j]为i到j Alex赢lee的个数class Solution(object): def stoneGame(self, piles): """ :type piles: List[int] :rtype: bool """ length = len(piles) ...
2019-02-14 18:31:06
179
原创 338. Counting Bits——dp
0-num中每个二进制数中1的个数class Solution(object): def countBits(self, num): """ :type num: int :rtype: List[int] """ dp = [0] for i in range(1, num + 1):...
2019-02-10 21:15:19
141
原创 12. Integer to Roman——string
class Solution(object): def generateParenthesis(self, num): """ :type n: int :rtype: List[str] """ rtn = "" n1=["","I","II","III
2019-02-10 18:43:47
117
原创 22. Generate Parentheses——string——回溯
题目分析:有左括号可以随便加,右括号数要小于等于左括号class Solution(object): def generateParenthesis(self, n): """ :type n: int :rtype: List[str] """ res = [] self.gener...
2019-02-10 17:08:14
149
原创 856. Score of Parentheses——string
class Solution(object): def repeatedNTimes(self, S): num = 0 tmp = S[0] a = [] for i in range(len(S)): if S[i] == "(": tmp = "(" ...
2019-02-08 17:23:47
155
原创 647. Palindromic Substrings——string
题目分析:以每个字母为中间位置,向外拓展,找到所有的回文序列class Solution(object): def repeatedNTimes(self, s): count = 0 for i in range(len(s)): count += 1 # 回文长度是奇数的情况 ...
2019-02-08 11:22:14
123
原创 791. Custom Sort String——string
题目分析:S中先出现的T中先出现class Solution(object): def repeatedNTimes(self, S, T): count = collections.Counter(T) answer = "" for s in S: answer += s*count[s] ...
2019-02-07 20:18:45
140
原创 55. Jump Game——python——greedy
题目分析:每次记录到达的最远位置,只是当前情况下的最优解。属于贪心算法。class Solution(object): def repeatedNTimes(self, nums): # 贪心算法 reach = 0 for i, num in enumerate(nums): if i > re...
2019-02-02 11:31:46
275
原创 不懂---206. Reverse Linked List
题目分析:相当于在头部前插class Solution(object): def reverseList(self, head): """ :type head: ListNode :rtype: ListNode """ new_head = None while head: ...
2019-01-22 17:24:02
95
原创 19. Remove Nth Node From End of List——linklist
class Solution(object): def repeatedNTimes(self, head, n): left = right = head for i in range(n): right = right.next if right is None: return he...
2019-01-22 16:48:41
202
转载 python heapq 堆
堆是一个二叉树,其中每个父节点的值都小于或等于其所有子节点的值。整个堆的最小元素总是位于二叉树的根节点。python的heapq模块提供了对堆的支持。 堆数据结构最重要的特征是heap[0]永远是最小的元素 heapq.heappush(heap,item) 注:heap为定义堆,item增加的元素heapq.heapify(list) 注:将列表转换为堆heapq.h...
2019-01-22 11:52:04
252
原创 前序中序后序的递归和非递归方法
递归方法#前序遍历 def pre_order(tree): if tree==None: return print tree.data pre_order(tree.left) pre_order(tree.right)#中序遍历 def mid_order(tree): if tree==None: re...
2019-01-21 17:10:57
170
原创 451. Sort Characters By Frequency——hash
class Solution: def numSubarrayProductLessThanK(self, s): dic = collections.Counter(s).most_common() res = "" print(dic) # sorted(dic.items(), key=lambda x:x[1])...
2019-01-21 16:36:10
151
原创 739. Daily Temperatures——hash
题目分析:1、用堆栈,存储降序的index,当前值比stack中最后一个值大则pop并输出距离,当前值比最后一个值小则append2、用字典,从最后一个开始,如果有重复的温度,那么记录的是靠前的一个class Solution: def numSubarrayProductLessThanK(self, T): # ans = [] # f...
2019-01-21 15:58:47
115
原创 713. Subarray Product Less Than K——array
题目分析:暴力方法时间超时,所以只能遍历一次class Solution: def numSubarrayProductLessThanK(self, nums, k): # count = 0 # for i in range(len(nums)): # product = 1 # for j in...
2019-01-21 11:48:14
243
原创 105. Construct Binary Tree from Preorder and Inorder Traversal——tree
题目分析:根据前序和中序遍历结果,生成二叉树,关键是找到中间的节点。from collections import dequeclass Solution: def buildTree(self, preorder, inorder): """ :type preorder: List[int] :type inorder: Li...
2019-01-21 11:16:12
97
原创 978. Longest Turbulent Subarray——array
题目分析:不是用动态规划,python3没有了cmp函数class Solution(object): def cmp(self, a, b): if a > b: return 1 if a < b: return -1 if a == b: re...
2019-01-21 11:02:05
250
原创 80. Remove Duplicates from Sorted Array II——array
题目分析:一种巧妙的解法。使用两个指针prev和curr,判断A[curr]是否和A[prev]、A[prev-1]相等,如果相等curr指针继续向后遍历,直到不相等时,将curr指针指向的值赋值给A[prev+1],这样多余的数就都被交换到后面去了。最后prev+1值就是数组的长度。 class Solution(object): def removeDuplicat...
2019-01-18 15:25:11
184
原创 670. Maximum Swap——array
题目分析:因为只交换一次,所以从高位开始,有比高位大且是高位之后最大的,就交换class Solution: def maximumSwap(self, num): A = map(int, str(num)) A = list(A) last = {x: i for i, x in enumerate(A)} pr...
2019-01-17 21:34:41
122
原创 没做对——40. Combination Sum II——array
没明白class Solution: def subsetsWithDup(self, candidates, target): self.resList = [] fl = [0] * len(candidates) candidates = sorted(candidates) # 排个序不影响复杂度 sel...
2019-01-17 16:27:06
100
原创 792. Number of Matching Subsequences——array
# count = 0 # 超时# for word in words:# i = 0# j = 0# while i < len(word) and j < len(S):# if word[i] == S[j]:# i += 1# j += 1# ...
2019-01-15 19:37:48
103
原创 162. Find Peak Element——array
题目分析:依次比较下去,第一个nums[i] > nums[i+1]的位置就是peak。时间复杂度是o(n)二分查找class Solution: def subsetsWithDup(self, nums): # for i in range(0, len(nums)-1): # if nums[i] > nums[i+1...
2019-01-15 11:08:04
156
原创 75. Sort Colors——array——two pointer
题目分析:可以用双指针red和blue来记录当前已经就位的0序列和2序列的边界位置。class Solution: def subsetsWithDup(self, nums): red = 0 blue = len(nums)-1 i = 0 while i <= blue: if n...
2019-01-14 21:41:25
90
原创 945. Minimum Increment to Make Array Unique——array
题目分析:暴力方法会超时,所以遍历一次,并记录当前的最大值,如果小于最大值,则需要增加到最大值加一。class Solution: def subsetsWithDup(self, A): # a = collections.Counter(A) # d1 = sorted(a.items(), key=lambda d: d[0], rever...
2019-01-14 20:52:30
178
原创 90. Subsets II-array
题目分析:以每个位置后边的所有位置与当前位置拼接,事件复杂的是阶乘的class Solution: # @param num, a list of integer # @return a list of lists of integer # a dfs problem def dfs(self, res, val, num, start): ...
2019-01-14 17:28:08
81
原创 560. Subarray Sum Equals K——array
一、暴力破解,for循环开始位置和结束位置,时间复杂度是n2.二、注意必须是连续的序列class Solution(object): def partitionDisjoint(self, nums, k): """ :type A: List[int] :rtype: int """ n = ...
2019-01-14 12:02:08
266
原创 119. Pascal's Triangle II——array
题目分析:因为只能用k的空间,所以考虑递归。但是递归超过了时间,所以考虑特性,每个值是前liang两个值得和,倒序计算。class Solution(object): def kthSmallest(self, rowIndex): # res = [0 for _ in range(rowIndex+1)] # res[0] = 1 # ...
2019-01-10 16:55:20
118
原创 11. Container With Most Water——array
题目分析:暴力破解会超过时间。class Solution(object): def kthSmallest(self, height): left = 0 right = len(height)-1 s = min(height[left], height[right])*(right-left) while...
2019-01-10 16:01:51
76
原创 795. Number of Subarrays with Bounded Maximum——python——dp
题目分析:连续子序列,出现连续子序列,解题思路一般是考虑每个被包含的情况。因为必须连续。class Solution(object): def kthSmallest(self, A, L, R): if not A: return 0 dp = [0 for _ in range(len(A))] dp[0] = 1 if L &...
2019-01-09 21:37:23
187
原创 153. Find Minimum in Rotated Sorted Array——python——dp
题目分析:可以用暴力的方法,也可以理解成是一个变种的二分查找。中间值比左边值小的话,最小值存在在左边,中间值比左边大的话,最小值存在在右边。最后返回的是right的值 class Solution(object): def kthSmallest(self, nums): # if nums[0]<=nums[len(nums)-1]: ...
2019-01-09 19:35:53
192
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人