加载中...

标签墙 - daily-leetcode

135 篇文章

daily leetcode - candy - !

题目地址 https://leetcode.com/problems/candy/ 题目描述 There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more candies than their neighbors. What is the minimum candies you must give? Example 1: Input: [1,0,2] Output: 5 Explanation: You can allocate to the first, second and third child with 2, 1, 2 candies respectively....

daily leetcode - gas-station - !

题目地址 https://leetcode.com/problems/gas-station/ 题目描述 There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations. Return the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return -1. Note: If there exists a sol....

daily leetcode - clone-graph - !

题目地址 https://leetcode.com/problems/clone-graph/ 题目描述 Given a reference of a node in a connected undirected graph. Return a deep copy (clone) of the graph. Each node in the graph contains a val (int) and a list (List[Node]) of its neighbors. class Node { public int val; public List<Node> neighbors; } Test case format: For simplicity sake, each node's value is the same as the node's index (1-indexed). For example, the first node with val = 1, the second node with val = 2, and so on. The gra....

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....

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