加载中...

daily leetcode - best-time-to-buy-and-sell-stock-ii - !

题目地址 https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 题目描述 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 (i.e., buy one and sell one share of the stock multiple times). Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again). Example 1: Input: [7,1,5,3,6,4] Output: 7 Explanation....

daily leetcode - best-time-to-buy-and-sell-stock - !

题目地址 https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ 题目描述 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 the stock), design an algorithm to find the maximum profit. Note that you cannot sell a stock before you buy one. Example 1: Input: [7,1,5,3,6,4] Output: 5 Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 =....

daily leetcode - triangle - !

题目地址 https://leetcode.com/problems/triangle/ 题目描述 Given 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 [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11). Note: Bonus point if you are able to do this using only O ( n ) extra space, where n is the total number of rows in the triangle. 思路 这道题给了我们一个二维数组组成的三角形,让我们寻找一条自上而下的路径,使得路径和....

daily leetcode - pascals-triangle-ii - !

题目地址 https://leetcode.com/problems/pascals-triangle-ii/ 题目描述 Given a non-negative index k where k ≤ 33, return the k th index row of the Pascal's triangle. Note that the row index starts from 0. In Pascal's triangle, each number is the sum of the two numbers directly above it. Example: Input: 3 Output: [1,3,3,1] Follow up: Could you optimize your algorithm to use only O ( k ) extra space? 思路 杨辉三角想必大家并不陌生,应该最早出现在初高中的数学中,其实就是二项式系数的一种写法。         1        1 1       1 2 1      1 3 3 1     1 4 6 4 1....

daily leetcode - pascals-triangle - !

题目地址 https://leetcode.com/problems/pascals-triangle/ 题目描述 Given 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. Example: Input: 5 Output: [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 思路 杨辉三角是二项式系数的一种写法,如果熟悉杨辉三角的五个性质,那么很好生成,可参见我的上一篇博文Pascal's Triangle II。具体生成算是:每一行的首个和结尾一个数字都是 1,从第三行开始,中间的每个数字都是上一行的左右两个数字之和。 关键点解析 代码 class Solution { public: vector<vector<int>> gen....

daily leetcode - populating-next-right-pointers-in-each-node-ii - !

题目地址 https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ 题目描述 Given a binary tree struct Node { int val; Node *left; Node *right; Node *next; } Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. Initially, all next pointers are set to NULL. Example: Input: root = [1,2,3,4,5,null,7] Output: [1,#,2,3,#,4,5,7,#] Explanation: Given the above binary tree (Figure A), your function should popula....

daily leetcode - populating-next-right-pointers-in-each-node - !

题目地址 https://leetcode.com/problems/populating-next-right-pointers-in-each-node/ 题目描述 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; Node *left; Node *right; Node *next; } Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. Initially, all next pointers are set to NULL. Follow up: You m....

daily leetcode - distinct-subsequences - !

题目地址 https://leetcode.com/problems/distinct-subsequences/ 题目描述 Given a string S and a string T, count the number of distinct subsequences of S which equals T. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ACE" is a subsequence of "ABCDE" while "AEC" is not). It's guaranteed the answer fits on a 32-bit signed integer. example1: Input: S....

daily leetcode - 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 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6 click to show hints. Hints: If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order trave 思路 这道题要求把二叉树展开成链表,根据展开后形成的链表的顺序分析出是使用先序遍历,那么只要是数的遍历就有递归和非递归的两种方法来求解,这里我们也用两种方法来求解。首先来看递归版本的,思路是先利用 DFS 的思路找到最左子节点,然后回....

daily leetcode - path-sum-ii - !

题目地址 https://leetcode.com/problems/path-sum-ii/ 题目描述 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 思路 这道二叉树路径之和在之前那道题 Path Sum 的基础上又需要找出路径,但是基本思想都一样,还是需要用深度优先搜索 DFS,只不过数据结构相对复杂一点,需要用到二维的 vector,而且每当 DFS 搜索到新结点时,都要保存该结点。而且每当找出一条路径之后,都将这个保存为一维 vector 的路径保存到最终结果二维 vector 中。并且,每当 DFS 搜索到子结点,发现不是路径和时,返回上一个结点时,需要把该结点....

avatar
Lonus Lan
It's better to burn out than to fade away!
公告
暂无更新通知!
最新文章
网站资讯
文章数目 :
156
已运行时间 :
0 天
本站在线访客数 :
0
本站总访问量 :
0