python
liff_lee
一个拼死拼活想学计算机编程掉了很多头发的文艺菜鸟
展开
-
【leetcode每日刷题】46. Permutations
https://leetcode.com/problems/permutations/对于数组进行全排列,使用回溯法或者深度遍历的方法。1、回溯法使用temp list表示当前排列的数组,回溯法对temp list的大小进行判断,如果和nums的大小一致,加入到结果列表中。回溯法对新加入的元素按位插入。package leetcode;import java.util.Arra...原创 2020-03-29 13:23:17 · 182 阅读 · 0 评论 -
【leetcode每日刷题】889. Construct Binary Tree from Preorder and Postorder Traversal
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self....原创 2020-01-22 23:34:37 · 199 阅读 · 0 评论 -
【leetcode每日刷题】106. Construct Binary Tree from Inorder and Postorder Traversal
https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.v...原创 2020-01-22 22:34:38 · 76 阅读 · 0 评论 -
【leetcode每日刷题】1008. Construct Binary Search Tree from Preorder Traversal
https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/使用递归的方式。# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# se...原创 2020-01-22 21:04:48 · 241 阅读 · 0 评论 -
【leetcode每日刷题】Largest Rectangle in Histogram
https://leetcode.com/problems/largest-rectangle-in-histogram/求直方图中的最大矩形从左到右找到最高的矩形,然后从右向左计算最大的面积。class Solution(object): def largestRectangleArea(self, heights): """ :type h...原创 2019-12-11 17:53:17 · 95 阅读 · 0 评论 -
【leetcode每日刷题】162. Find Peak Element
https://leetcode.com/problems/find-peak-element/class Solution(object): def findPeakElement(self, nums): """ :type nums: List[int] :rtype: int """ for in...原创 2019-12-11 17:24:54 · 96 阅读 · 0 评论 -
【leetcode每日刷题】350. Intersection of Two Arrays II
https://leetcode.com/problems/intersection-of-two-arrays-ii/class Solution(object): def intersect(self, nums1, nums2): """ :type nums1: List[int] :type nums2: List[int]...原创 2019-12-11 16:52:37 · 103 阅读 · 0 评论 -
【leetcode每日刷题】234. Palindrome Linked List
https://leetcode.com/problems/palindrome-linked-list/Given a singly linked list, determine if it is a palindrome.Example 1:Input: 1->2Output: falseExample 2:Input: 1->2->2->1...原创 2019-11-27 10:46:13 · 121 阅读 · 0 评论 -
【leetcode每日刷题】279. Perfect Squares
https://leetcode.com/problems/perfect-squares/Given a positive integern, find the least number of perfect square numbers (for example,1, 4, 9, 16, ...) which sum ton.Example 1:Input: n = 12...原创 2019-11-19 22:47:27 · 100 阅读 · 0 评论 -
【leetcode每日刷题】【DP】494. Target Sum
https://leetcode.com/problems/target-sum/You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols+and-. For each integer, you should choose one from...原创 2019-11-19 17:52:03 · 133 阅读 · 0 评论 -
【leetcode每日刷题】621. Task Scheduler
https://leetcode.com/problems/task-scheduler/使用填槽的方式实现:先排序,根据具有最大数量的任务确定空槽的数量,即只执行最大数量的任务需要的空闲间隔,然后按照任务数量递减的顺序对每个任务进行遍历,使用任务数量进行填槽,最后根据空槽的数量确定返回结果。class Solution(object): def leastInterval(...原创 2019-11-19 17:11:15 · 121 阅读 · 0 评论 -
python实现的二叉树创建和打印
class TreeNode(object): def __init__(self, x): self.val = x self.left = None self.right = Noneclass Tree(): def creatTree(self, nums): if len(nums) == 0: ...原创 2019-11-19 15:27:41 · 1064 阅读 · 0 评论 -
【leetcode每日刷题】617. Merge Two Binary Trees
https://leetcode.com/problems/merge-two-binary-trees/# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# ...原创 2019-11-19 15:26:04 · 85 阅读 · 0 评论 -
【leetcode每日刷题】128. Longest Consecutive Sequence
https://leetcode.com/problems/longest-consecutive-sequence/使用先排序,然后统计最长连续序列的方法。class Solution(object): def longestConsecutive(self, nums): """ :type nums: List[int] :r...原创 2019-11-19 15:21:41 · 85 阅读 · 0 评论 -
【leetcode每日刷题】142. Linked List Cycle II
https://leetcode.com/problems/linked-list-cycle-ii/Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.To represent a cycle in the given linked list, we ...原创 2019-11-19 11:01:13 · 100 阅读 · 0 评论 -
【leetcode每日刷题】【图/拓扑排序/判断环】207. Course Schedule
https://leetcode.com/problems/course-schedule/判断有向图中是否存在环。使用graph表示邻接图,使用preNum表示没门课的先导课的数量,为0表示没有先导课。使用队列存放先导课数量为0的课程,即可以直接上的课程,遍历队列中的元素,然后将以当前课程为先导课的课程的先导课程数量preNum减1,直到队列中的元素为空。最后遍历preNum中的所有元素,如...原创 2019-11-18 17:26:21 · 152 阅读 · 0 评论 -
【leetcode每日刷题】238. Product of Array Except Self
https://leetcode.com/problems/product-of-array-except-self/Given an arraynumsofnintegers wheren> 1, return an arrayoutputsuch thatoutput[i]is equal to the product of all the elements o...原创 2019-11-17 00:19:11 · 75 阅读 · 0 评论 -
【leetcode每日刷题】152. Maximum Product Subarray
https://leetcode.com/problems/maximum-product-subarray/最大乘积子数组# 解法一,使用两个动态数组def maxProduct(self, nums): """ :type nums: List[int] :rtype: int """ res = nu...原创 2019-11-16 20:45:36 · 152 阅读 · 0 评论 -
【leetcode每日刷题】96. Unique Binary Search Trees
https://leetcode.com/problems/unique-binary-search-trees/假设n个数字的BST个数为G(n),n个数字且第i个数字为根的BST个数为F(n,i),递推公式为F(n,i) = G(i-1) * G(n-i),那么从前往后求出G(n) = sum(G(i-1) * G(n-i))class Solution(object): ...原创 2019-11-16 18:10:53 · 76 阅读 · 0 评论 -
【leetcode每日刷题】114. Flatten Binary Tree to Linked List
https://leetcode.com/problems/flatten-binary-tree-to-linked-list/Given a binary tree, flatten it to a linked list in-place.For example, given the following tree: 1 / \ 2 5 / \ \3 ...原创 2019-11-14 12:50:36 · 74 阅读 · 0 评论 -
【leetcode每日刷题】287. Find the Duplicate Number
https://leetcode.com/problems/find-the-duplicate-number/Given an arraynumscontainingn+ 1 integers where each integer is between 1 andn(inclusive), prove that at least one duplicate number must...原创 2019-11-13 21:54:20 · 98 阅读 · 0 评论 -
【leetcode每日刷题】438. Find All Anagrams in a String
https://leetcode.com/problems/find-all-anagrams-in-a-string/Given a stringsand anon-emptystringp, find all the start indices ofp's anagrams ins.Strings consists of lowercase English letters...原创 2019-11-11 22:51:05 · 155 阅读 · 0 评论 -
【leetcode每日刷题】543. Diameter of Binary Tree
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of thelongestpath between any two nodes in a tree. This path may or may n...原创 2019-11-10 00:22:00 · 66 阅读 · 0 评论 -
【leetcode每日刷题】739. Daily Temperatures
Given a list of daily temperaturesT, return a list such that, for each day in the input, tells you how many days you would have to wait until a warmer temperature. If there is no future day for which...原创 2019-11-09 23:03:00 · 115 阅读 · 0 评论 -
【leetcode每日刷题】198. House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent house...原创 2019-11-08 22:12:12 · 88 阅读 · 0 评论 -
【leetcode每日刷题】1031. Maximum Sum of Two Non-Overlapping Subarrays
Given an arrayAof non-negative integers, return the maximum sum of elements in two non-overlapping (contiguous) subarrays, which have lengthsLandM. (For clarification, theL-length subarray coul...原创 2019-11-07 17:52:42 · 139 阅读 · 0 评论 -
【leetcode每日刷题】900. RLE Iterator
https://leetcode.com/problems/rle-iterator/实现迭代器The iterator is initialized byRLEIterator(int[] A), whereAis a run-length encoding of somesequence. More specifically,for all eveni,A[i]tel...原创 2019-11-07 16:06:24 · 82 阅读 · 0 评论 -
【leetcode每日刷题】887. Super Egg Drop
使用K个鸡蛋来确定F(0<=F<=N),F表示(鸡蛋若从高于F的地方掉下会摔坏,在低于F的地方不会摔坏),问最少需要丢几次鸡蛋可以确定F的值。You are givenKeggs, and you have access to a building withNfloors from1toN.Each egg is identical in function, a...原创 2019-11-07 11:20:35 · 146 阅读 · 0 评论 -
【leetcode每日刷题】283. Move Zeroes
Given an arraynums, write a function to move all0's to the end of it while maintaining the relative order of the non-zero elements.Example:Input: [0,1,0,3,12]Output: [1,3,12,0,0]Note:You m...原创 2019-11-05 00:09:54 · 64 阅读 · 0 评论 -
【leetcode每日刷题】240. Search a 2D Matrix II
Write an efficient algorithm that searches for a value in anmxnmatrix. This matrix has the following properties:Integers in each row are sorted in ascending from left to right. Integers in each...原创 2019-11-04 23:48:09 · 72 阅读 · 0 评论 -
【leetcode每日刷题】912. Sort an Array
Given an array of integersnums, sort the array in ascending order.Example 1:Input: nums = [5,2,3,1]Output: [1,2,3,5]Example 2:Input: nums = [5,1,1,2,0,0]Output: [0,0,1,1,2,5]Const...原创 2019-11-04 15:15:52 · 172 阅读 · 0 评论 -
【leetcode每日刷题】338. Counting Bits
Given a non negative integer numbernum. For every numbersiin the range0 ≤ i ≤ numcalculate the number of 1's in their binary representation and return them as an array.Example 1:Input: 2Out...原创 2019-11-03 20:12:20 · 96 阅读 · 1 评论 -
【leetcode每日刷题】【dp】312. Burst Balloons
Givennballoons, indexed from0ton-1. Each balloon is painted with a number on it represented by arraynums. You are asked to burst all the balloons. If the you burst ballooniyou will getnums[le...原创 2019-11-03 15:28:24 · 96 阅读 · 0 评论 -
【leetcode每日刷题】【DP】309. Best Time to Buy and Sell Stock with Cooldown
Say you have an array for which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one ...原创 2019-11-03 01:30:31 · 96 阅读 · 0 评论 -
【leetcode每日刷题】【树/递归】236. Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to thedefinition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p...原创 2019-10-31 10:11:48 · 78 阅读 · 0 评论 -
【leetcode每日刷题】【树/遍历】199. Binary Tree Right Side View
Given a binary tree, imagine yourself standing on therightside of it, return the values of the nodes you can see ordered from top to bottom.Example:Input:[1,2,3,null,5,null,4]Output:[1, 3, 4...原创 2019-10-30 22:48:05 · 91 阅读 · 0 评论 -
【leetcode每日刷题】【树/遍历】144. Binary Tree Preorder Traversal
Given a binary tree, return thepreordertraversal of its nodes' values.Example:Input:[1,null,2,3] 1 \ 2 / 3Output:[1,2,3]包括递归算法和使用栈的非递归算法# Definition for a binary tre...原创 2019-10-30 17:48:46 · 68 阅读 · 0 评论 -
【leetcode每日刷题】92. Reverse Linked List II
Reverse a linked list from positionmton. Do it in one-pass.Note:1 ≤m≤n≤ length of list.Example:Input: 1->2->3->4->5->NULL, m = 2, n = 4Output: 1->4->3->2->5-...原创 2019-10-30 17:15:21 · 84 阅读 · 0 评论 -
【leetcode每日刷题】150. Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression inReverse Polish Notation.Valid operators are+,-,*,/. Each operand may be an integer or another expression.Note:Division between two integers ...原创 2019-10-30 13:58:19 · 81 阅读 · 0 评论 -
【leetcode每日刷题】【dp】91. Decode Ways
A message containing letters fromA-Zis being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given anon-emptystring containing only digits, determine t...原创 2019-10-30 11:33:09 · 60 阅读 · 0 评论