![](https://img-blog.csdnimg.cn/20201014180756923.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
Koliday
这个作者很懒,什么都没留下…
展开
-
House Robber
Leetcode整理之——House Robber系列House Robber系列一共有三道题,分别是:No.198 House RobberNo.213 House Robber IINo.337 House Robber III这个系列是动态规划的代表作,难度层层递进,从简单的动态规划到与二叉树结合,非常具有特色,接下来我就对这三题的思路和解法进行讲解No.198 House Ro...原创 2020-02-10 23:40:55 · 223 阅读 · 0 评论 -
Dynamic Programming——No.198 House Robber
Problem: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 adja...原创 2019-04-15 20:42:03 · 114 阅读 · 0 评论 -
Tree——No.108 Convert Sorted Array to Binary Search Tree
Problem:Given 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 in which the de...原创 2019-04-28 16:31:31 · 99 阅读 · 0 评论 -
Tree——No.538. Convert BST to Greater Tree
Problem:Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BS...原创 2019-04-28 16:32:43 · 92 阅读 · 0 评论 -
Tree——No.938. Range Sum of BST
Problem:Given the root node of a binary search tree, return the sum of values of all nodes with value between L and R (inclusive).The binary search tree is guaranteed to have unique values.Expla...原创 2019-04-28 16:33:35 · 91 阅读 · 0 评论 -
LinkedList——No.160 Intersection of Two Linked Lists
Problem:Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:Explanation:找两个链表的交点(地址相同的点)My Thinking:My ...原创 2019-04-28 16:34:47 · 110 阅读 · 0 评论 -
Tree——No.897 Increasing Order Search Tree
Problem:Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only 1 right child.Explanation:将一个搜索二...原创 2019-04-19 19:57:53 · 82 阅读 · 0 评论 -
Dynamic Programming——No.62 Unique Paths
Problem:A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to re...原创 2019-04-19 19:59:04 · 104 阅读 · 0 评论 -
Tree——No.653 Two Sum IV - Input is a BST
Problem:Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target.Explanation:在搜索二叉树中寻找两数之和。My Thinkin...原创 2019-04-25 17:55:57 · 102 阅读 · 0 评论 -
Tree——No.606 Construct String from Binary Tree
Problem:You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way.The null node needs to be represented by empty parenthesis pair "()"...原创 2019-04-25 17:56:42 · 87 阅读 · 0 评论 -
Tree——No.993 Cousins in Binary Tree
Problem:In a binary tree, the root node is at depth 0, and children of each depth k node are at depth k+1.Two nodes of a binary tree are cousins if they have the same depth, but have different par...原创 2019-04-25 17:58:15 · 151 阅读 · 0 评论 -
Dynamic Programming——No.140 Word Break II
Problem:Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such...原创 2019-04-18 21:13:24 · 95 阅读 · 0 评论 -
Tree——No.671 Second Minimum Node In a Binary Tree
Problem:Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then this...原创 2019-04-18 21:12:39 · 103 阅读 · 0 评论 -
Dynamic Programming——No.121 Best Time to Buy and Sell Stock
Problem:Say 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 sell one share of t...原创 2019-04-15 20:43:38 · 92 阅读 · 0 评论 -
Tree——No.637 Average of Levels in Binary Tree
Problem:Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array.Explanation:计算二叉树每一层的平均值。My Thinking:层序遍历二叉树,计算平均值即可。My Solution:class...原创 2019-04-21 14:30:57 · 99 阅读 · 0 评论 -
Dynamic Programming——No.300 Longest Increasing Subsequence
Problem:Given an unsorted array of integers, find the length of longest increasing subsequence.Explanation:找到最长的连续增长的数列长度,数组中的数字可以跳跃使用。My Thinking:My Solution:Optimum Thinking:dp[i]表示0-i...原创 2019-04-21 14:34:22 · 127 阅读 · 0 评论 -
Tree——No.700 Search in a Binary Search Tree
Problem:Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node's value equals the given value. Return the subtree rooted with that node. If...原创 2019-04-16 21:17:39 · 92 阅读 · 0 评论 -
Dynamic Programming——No.746 Min Cost Climbing Stairs
Problem:On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed).Once you pay the cost, you can either climb one or two steps. You need to find minimum cost to reach t...原创 2019-04-16 21:18:48 · 83 阅读 · 0 评论 -
Dynamic Programming——No.309 Best Time to Buy and Sell Stock with Cooldown(未搞懂)
Problem:Say 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 as you like (ie...原创 2019-04-16 21:20:07 · 120 阅读 · 0 评论 -
Tree——No.226 Invert Binary Tree
Problem:Invert a binary tree.Explanation:将二叉树轴对称翻转。My Thinking:前序遍历,将左右孩子交换。My Solution:class Solution { public TreeNode invertTree(TreeNode root) { if(root==null) ...原创 2019-04-21 21:11:17 · 88 阅读 · 0 评论 -
Tree——No.559 Maximum Depth of N-ary Tree
Problem:Given a n-ary 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.For example, given a 3-ary tr...原创 2019-04-17 22:23:23 · 74 阅读 · 0 评论 -
Dynamic Programming——No.139 Word Break
Problem:Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.N...原创 2019-04-17 22:24:19 · 123 阅读 · 0 评论 -
Dynamic Programming——No.63 Unique Paths II
Problem:A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to re...原创 2019-05-02 20:50:50 · 108 阅读 · 0 评论 -
Dynamic Programming——No.813 Largest Sum of Averages
Problem:We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?Note that our p...原创 2019-04-25 18:02:23 · 87 阅读 · 0 评论 -
Tree——No.103 Binary Tree Zigzag Level Order Traversal
Problem:Given 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).Explanation:二叉树z字...原创 2019-05-08 23:20:57 · 83 阅读 · 0 评论 -
Dynamic Programming——No.213 House Robber II
Problem:You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first...原创 2019-05-16 18:52:54 · 145 阅读 · 0 评论 -
Tree——No.98 Validate Binary Search Tree
Problem:Given a binary tree, determine if it is a valid binary search tree (BST).Explanation:判断一棵树是否为BSTMy Thinking:左结点范围:(上个结点的下限,上个结点的值)右结点范围:(上个结点的值,上个结点的上限)My Solution:class Solut...原创 2019-05-21 21:53:16 · 110 阅读 · 0 评论 -
String——No.344 Reverse String
Problem:Write a function that reverses a string. The input string is given as an array of characters char[].Do not allocate extra space for another array, you must do this by modifying the input a...原创 2019-05-21 21:53:59 · 158 阅读 · 0 评论 -
Tree——No.173 Binary Search Tree Iterator
Problem:Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the next smallest number in the BST.Explan...原创 2019-05-27 18:26:50 · 114 阅读 · 0 评论 -
String——No.20 Valid Parentheses
Problem:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.An input string is valid if:Open brackets must be closed by the same...原创 2019-05-27 18:27:46 · 130 阅读 · 0 评论 -
Tree——No.222 Count Complete Tree Nodes
Problem:Given a complete binary tree, count the number of nodes.Explanation:计算完全二叉树的结点数量。若设二叉树的深度为h,除第 h 层外,其它各层 (1~h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树。My Thinking:直接递归计算树的结点数(虽然...原创 2019-05-28 16:14:46 · 124 阅读 · 0 评论 -
Tree——No.199 Binary Tree Right Side View
Problem:Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.Explanation:得到树的右视图。My Thinking:层序遍历,将每层...原创 2019-05-28 16:16:03 · 114 阅读 · 0 评论 -
Tree——No.222 Count Complete Tree Nodes
Problem:Given a complete binary tree, count the number of nodes.Explanation:计算完全二叉树的结点数量。若设二叉树的深度为h,除第 h 层外,其它各层 (1~h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树。My Thinking:直接递归计算树的结点数(虽然...原创 2019-05-29 19:41:57 · 95 阅读 · 0 评论 -
No.513 Find Bottom Left Tree Value
Problem:Given a binary tree, find the leftmost value in the last row of the tree.Explanation:找二叉树的左下结点My Thinking:层序遍历,从右向左遍历,最后剩余的就是最左结点My Solution:class Solution { public int find...原创 2019-05-29 19:42:51 · 121 阅读 · 0 评论 -
Dynamic Programming——No.64 Minimum Path Sum
Problem:Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or ...原创 2019-05-16 18:23:36 · 119 阅读 · 0 评论 -
Tree——No.116 Populating Next Right Pointers in Each Node
Problem:You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:struct Node {int val;Nod...原创 2019-05-16 18:19:34 · 111 阅读 · 0 评论 -
LinkedList——No.19 Remove Nth Node From End of List
Problem:Given a linked list, remove the n-th node from the end of list and return its head.Explanation:将链表中倒数第n个结点移除,返回头结点。My Thinking:使用递归,返回到倒数n+1个结点时,将其指针指向后一个结点的后一个结点即可。My Solution:...原创 2019-05-12 11:20:50 · 112 阅读 · 0 评论 -
Tree——No.450 Delete Node in a BST
Problem:Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST.Note: Time complexity should be ...原创 2019-05-14 18:53:07 · 113 阅读 · 0 评论 -
Tree——No.337 House Robber III
Problem:The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. Af...原创 2019-05-14 18:53:52 · 196 阅读 · 0 评论 -
Tree——No.144 Binary Tree Preorder Traversal
Problem:Given a binary tree, return the preorder traversal of its nodes' values.Explanation:前序遍历二叉树My Thinking:(1)递归(2)使用栈迭代,先入右孩子,再入左孩子。My Solution:(1)class Solution { List<...原创 2019-05-07 18:11:22 · 79 阅读 · 0 评论