加载中...

daily leetcode - palindrome-partitioning-ii - !

题目地址 https://leetcode.com/problems/palindrome-partitioning-ii/ 题目描述 Given a string s , partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. Example: Input: "aab" Output: 1 Explanation: The palindrome partitioning ["aa","b"] could be produced using 1 cut. 思路 这道题是让找到把原字符串拆分成回文串的最小切割数,如果我们首先考虑用 brute force 来做的话就会十分的复杂,因为我们不但要判断子串是否是回文串,而且还要找出最小切割数,情况会非常的多,不好做。所以对于这种玩字符串且是求极值的题,就要祭出旷古神器动态规划 Dynamic Programming 了,秒....

daily leetcode - palindrome-partitioning - !

题目地址 https://leetcode.com/problems/palindrome-partitioning/ 题目描述 Given a string s , partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. Example: Input: "aab" Output: [ ["aa","b"], ["a","a","b"] ] 思路 这又是一道需要用 DFS 来解的题目,既然题目要求找到所有可能拆分成回文数的情况,那么肯定是所有的情况都要遍历到,对于每一个子字符串都要分别判断一次是不是回文数,那么肯定有一个判断回文数的子函数,还需要一个 DFS 函数用来递归,再加上原本的这个函数,总共需要三个函数来求解。我们将已经检测好的回文子串放到字符串数组 out 中,当 s 遍历完了之后,将 out 加入结果 res 中。那么在递归函数中我们必须要知道当前遍历到的位置,用变量 start 来表示....

daily leetcode - surrounded-regions - !

题目地址 https://leetcode.com/problems/surrounded-regions/ 题目描述 Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. Example: X X X X X O O X X X O X X O X X After running your function, the board should be: X X X X X X X X X X X X X O X X Explanation: Surrounded regions shouldn’t be on the border, which means that any 'O' on the border of the board are not flipped to 'X'. Any 'O....

daily leetcode - sum-root-to-leaf-numbers - !

题目地址 https://leetcode.com/problems/sum-root-to-leaf-numbers/ 题目描述 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. Note: A leaf is a node with no children. Example: Input: [1,2,3] 1 / \ 2 3 Output: 25 Explanation: The root-to-leaf path 1->2 represents the number 12. The root-to-leaf path 1->3 represents the n....

daily leetcode - longest-consecutive-sequence - !

题目地址 https://leetcode.com/problems/longest-consecutive-sequence/ 题目描述 Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Your algorithm should run in O( n ) complexity. Example: Input: [100, 4, 200, 1, 3, 2] Output: 4 Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4. 思路 这道题要求求最长连续序列,并给定了 O(n)复杂度限制,我们的思路是,使用一个集合 HashSet 存入所有的数字,然后遍历数组中的每个数字,如果其在集合中存在,那么将其移除,然后分别用两个变量 pre 和 next 算出其前一个数跟后一个数,然后在集合中循环查....

daily leetcode - word-ladder - !

题目地址 https://leetcode.com/problems/word-ladder/ 题目描述 Given two words ( beginWord and endWord ), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord , such that: Only one letter can be changed at a time. Each transformed word must exist in the word list. Note that beginWord is not a transformed word. Note: Return 0 if there is no such transformation sequence. All words have the same length. All words contain only lowercase alphabetic char....

daily leetcode - word-ladder-ii - !

题目地址 https://leetcode.com/problems/word-ladder-ii/ 题目描述 Given two words ( beginWord and endWord ), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord , such that: Only one letter can be changed at a time Each transformed word must exist in the word list. Note that beginWord is not a transformed word. Note: Return an empty list if there is no such transformation sequence. All words have the same length. All words contain only lowercase alphabet....

daily leetcode - valid-palindrome - !

题目地址 https://leetcode.com/problems/valid-palindrome/ 题目描述 Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a palindrome. Note: Have you consider that the string might be empty? This is a good question to ask during an interview. For the purpose of this problem, we define empty string as valid palindrome. 思路 验证回文字符串是比较常见的问题,所谓回文,就是一个正读和反读都一样的字符串,比如“level”....

daily leetcode - binary-tree-maximum-path-sum - !

题目地址 https://leetcode.com/problems/binary-tree-maximum-path-sum/ 题目描述 Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root. Example 1: Input: [1,2,3] 1 / \ 2 3 Output: 6 Example 2: Input: [-10,9,20,null,null,15,7] -10 / \ 9 20 / \ 15 7 Output: 42 思路 这道求二叉树的最大路径和是一道蛮有....

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

题目地址 https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 题目描述 Say you have an array for which the i th element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions. 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: [3,3,5,0,0,3,1,4] Output: 6 Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit ....

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