
LeetCode-Python
小刷怡情,大刷伤身 (>﹏<)
JNingWei
工作后比较忙,不怎么看账号和消息。回复不及时望见谅。
展开
-
【leetcode】遍历二叉树
转载自:python实现二叉树和它的七种遍历Summary递归实现先序遍历、中序遍历、后序遍历堆栈实现先序遍历、中序遍历、后序遍历队列实现层次遍历Code#coding=utf-8class Node(object): """节点类""" def __init__(self, elem=-1, lchild=None, rchild=None): self.e原创 2018-01-02 20:59:01 · 1098 阅读 · 0 评论 -
【leetcode】分治/动态规划/贪心/递归/迭代
算法策略分治:自顶向下,分而治之。常用递归。动态规划(DP):类似于分治,但会存储每个子问题的解,避免重复计算。常用迭代。贪心:类似于DP,但每步都求局部最优。计算次数往往会比DP少。凡是用贪心能解决的,DP都能解决。实现手段递归:A调用A自身。所有的递归都可以转化成迭代。迭代:A循环调用B,并不断更新变量的旧值。...原创 2020-05-31 17:15:35 · 589 阅读 · 0 评论 -
【leetcode】八皇后
代码实现:class solution(object): def solveNQueens(self, n): self.helper([-1]*n, 0, n) def helper(self, columnPosition, rowindex, n):#ding # print(rowindex) if rowindex == n: self.printSolution(columnPosition, n)原创 2020-05-31 17:07:12 · 503 阅读 · 0 评论 -
【leetcode】排序算法
冒泡排序def bubble_sort(x): for i in range(len(x)): for j in range(1, len(x)-i): if x[j-1] > x[j]: x[j-1], x[j] = x[j], x[j-1] return x插入排序def insert_sor...原创 2018-11-08 11:22:11 · 564 阅读 · 0 评论 -
【leetcode】剪绳子
动态规划def cut(n): if n < 2: return 0 elif n == 2: return 1 elif n == 3: return 2 else: lst = [0, 1, 2, 3] for i in range(4, n+1): max = 0 for j in range(1, i//2+1):原创 2020-05-31 16:37:58 · 344 阅读 · 0 评论 -
【leetcode】背包问题
思路一般用动态规划(DP)。输入参数W:各个物品的重量;V:各个物品的价值;carry:最大承重为carry的背包;N:物品件数。0-1背包每件物品只能选一次。def bag_01(V, W, carry, N): res = [0] * (carry+1) for i in range(N): for j in range(carry, W[i]-1, -1): res[j] = max(res[j], res[j-W[i]原创 2020-05-31 16:35:29 · 449 阅读 · 0 评论 -
leetcode: 449. Serialize and Deserialize BST
DifficultyMedium.ProblemSerialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a ne...原创 2018-11-08 11:17:44 · 380 阅读 · 0 评论 -
leetcode: 215. Kth Largest Element in an Array
DifficultyMedium.ProblemFind the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.Example 1:Input: [3,2,1,5,6...原创 2018-11-08 11:16:00 · 355 阅读 · 0 评论 -
leetcode: 206. Reverse Linked List
DifficultyEasy.ProblemReverse a singly linked list.Example:Input: 1->2->3->4->5->NULLOutput: 5->4->3->2->1->NULLFollow up:A linked list can be reversed either ...原创 2018-11-08 11:14:21 · 347 阅读 · 0 评论 -
leetcode: 144. Binary Tree Preorder Traversal
DifficultyMedium.ProblemGiven a binary tree, return the preorder traversal of its nodes' values.Example:Input: [1,null,2,3] 1 \ 2 / 3Output: [1,2,3]Follow up: Recursive so...原创 2018-11-08 11:12:15 · 239 阅读 · 0 评论 -
leetcode: 141. Linked List Cycle
DifficultyEasy.ProblemGiven a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?AC# Definition for singly-linked list.# class ListNode(objec...原创 2018-11-08 11:09:57 · 239 阅读 · 0 评论 -
leetcode: 129. Sum Root to Leaf Numbers
DifficultyEasy.ProblemSay you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions...原创 2018-11-08 11:01:33 · 255 阅读 · 0 评论 -
leetcode: 122. Best Time to Buy and Sell Stock II
DifficultyEasy.ProblemSay you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions ...原创 2018-11-08 11:07:36 · 323 阅读 · 0 评论 -
leetcode: 121. Best Time to Buy and Sell Stock
DifficultyEasy.ProblemSay you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (i.e., buy one and sel...原创 2018-11-07 09:13:07 · 303 阅读 · 0 评论 -
leetcode: 120. Triangle
DifficultyMedium.ProblemGiven a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ ...原创 2018-11-07 09:11:22 · 267 阅读 · 0 评论 -
leetcode: 119. Pascal's Triangle II
DifficultyEasy.ProblemGiven a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle.Note that the row index starts from 0.In Pascal's triangle, each number is th...原创 2018-11-07 09:09:42 · 297 阅读 · 0 评论 -
leetcode: 118. Pascal's Triangle
DifficultyEasy.ProblemGiven a non-negative integer numRows, generate the first numRows of Pascal's triangle.In Pascal's triangle, each number is the sum of the two numbers directly above it.Ex...原创 2018-11-07 09:06:57 · 245 阅读 · 0 评论 -
leetcode: 117. Populating Next Right Pointers in Each Node II
DifficultyMedium.ProblemGiven a binary treestruct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next;}Populate each next pointer to point to its next right node. ...原创 2018-11-07 09:04:16 · 487 阅读 · 0 评论 -
leetcode: 116. Populating Next Right Pointers in Each Node
DifficultyMedium.ProblemGiven a binary treestruct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next;}Populate each next pointer to point to its next right node. I...原创 2018-11-07 09:02:28 · 380 阅读 · 0 评论 -
leetcode: 114. Flatten Binary Tree to Linked List [✗]
DifficultyMedium.ProblemGiven a binary tree, flatten it to a linked list in-place.For example, given the following tree: 1 / \ 2 5 / \ \3 4 6The flattened tree should look li...原创 2018-11-07 08:59:32 · 428 阅读 · 0 评论 -
leetcode: 113. Path Sum II
DifficultyMedium.ProblemGiven a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.Note: A leaf is a node with no children.Example:Given the below b...原创 2018-11-07 08:56:01 · 270 阅读 · 0 评论 -
leetcode: 112. Path Sum
DifficultyEasy.ProblemGiven a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.Note: A leaf is a node w...原创 2018-11-07 08:53:51 · 408 阅读 · 0 评论 -
leetcode: 111. Minimum Depth of Binary Tree
DifficultyEasy.ProblemGiven a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.Note: A leaf ...原创 2018-11-07 08:51:39 · 256 阅读 · 0 评论 -
leetcode: 110. Balanced Binary Tree
DifficultyEasy.ProblemGiven a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as:a binary tree in which the depth of the two subtrees ...原创 2018-11-05 17:13:29 · 258 阅读 · 0 评论 -
leetcode: 109. Convert Sorted List to Binary Search Tree
DifficultyMedium.ProblemGiven a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.For this problem, a height-balanced binary tree is defined as ...原创 2018-11-05 17:11:26 · 198 阅读 · 0 评论 -
leetcode: 108. Convert Sorted Array to Binary Search Tree
DifficultyEasy.ProblemGiven an array where elements are sorted in ascending order, convert it to a height balanced BST.For this problem, a height-balanced binary tree is defined as a binary tree ...原创 2018-11-05 17:09:17 · 204 阅读 · 0 评论 -
leetcode: 107. Binary Tree Level Order Traversal II
DifficultyEasy.ProblemGiven a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary t...原创 2018-11-05 17:07:14 · 198 阅读 · 0 评论 -
leetcode: 106. Construct Binary Tree from Inorder and Postorder Traversal
DifficultyMedium.ProblemGiven inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.For example, giveninorder = [9...原创 2018-11-05 17:03:30 · 206 阅读 · 0 评论 -
leetcode: 105. Construct Binary Tree from Preorder and Inorder Traversal
DifficultyMedium.ProblemGiven preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.For example, givenpreorder = [3...原创 2018-11-05 17:01:10 · 267 阅读 · 0 评论 -
leetcode: 104. Maximum Depth of Binary Tree
DifficultyEasy.ProblemGiven a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.Note: A leaf ...原创 2018-11-05 16:58:17 · 226 阅读 · 0 评论 -
leetcode: 103. Binary Tree Zigzag Level Order Traversal
DifficultyMedium.ProblemGiven a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).Fo...原创 2018-11-05 16:45:59 · 206 阅读 · 0 评论 -
leetcode: 102. Binary Tree Level Order Traversal
DifficultyMedium.ProblemGiven a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree [3,9,20,null,null,15,7...原创 2018-11-05 16:38:22 · 227 阅读 · 0 评论 -
leetcode: 101. Symmetric Tree
DifficultyEasy.Problem# Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).## For example, this binary tree [1,2,2,3,4,4,3] is symmetric:## 1# ...原创 2018-11-05 16:35:42 · 249 阅读 · 0 评论 -
leetcode: 100. Same Tree
QGiven two binary trees, write a function to check if they are the same or not.Two binary trees are considered the same if they are structurally identical and the nodes have the same value.Example 1:In原创 2017-11-23 09:02:15 · 552 阅读 · 0 评论 -
leetcode: 99. Recover Binary Search Tree
QTwo elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note: A solution using O(n) space is pretty straight forward. Could you devise a cons原创 2017-11-23 09:01:40 · 614 阅读 · 0 评论 -
leetcode: 98. Validate Binary Search Tree
QGiven a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the node’s key. The righ原创 2017-11-23 09:00:14 · 501 阅读 · 0 评论 -
leetcode: 97. Interleaving String
QGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.For example, Given: s1 = “aabcc”, s2 = “dbbca”,When s3 = “aadbbcbcac”, return true. When s3 = “aadbbbaccc”, return fals原创 2017-11-22 10:25:05 · 722 阅读 · 0 评论 -
leetcode: 96. Unique Binary Search Trees
QGiven n, how many structurally unique BST’s (binary search trees) that store values 1…n?For example, Given n = 3, there are a total of 5 unique BST’s. 1 3 3 2 1 \ /原创 2017-11-22 10:24:24 · 471 阅读 · 0 评论 -
leetcode: 95. Unique Binary Search Trees II [✗]
QGiven an integer n, generate all structurally unique BST’s (binary search trees) that store values 1…n.For example, Given n = 3, your program should return all 5 unique BST’s shown below. 1原创 2017-11-22 10:23:49 · 492 阅读 · 0 评论 -
leetcode: 94. Binary Tree Inorder Traversal
QGiven a binary tree, return the inorder traversal of its nodes’ values.For example: Given binary tree [1,null,2,3], 1 \ 2 / 3return [1,3,2].Note: Recursive solution is trivial, cou原创 2017-11-22 10:23:17 · 466 阅读 · 0 评论