leetcode

LeetCode All in One 题目讲解汇总(持续更新中...)

对着背影说爱祢 提交于 2020-01-23 01:29:50
Given a positive integer n , return the number of all possible attendance records with length n, which will be regarded as rewardable. The answer may be very large, return it after mod 10 9 + 7. A student attendance record is a string that only contains the following three characters: 'A' : Absent. 'L' : Late. 'P' : Present. A record is regarded as rewardable if it doesn't contain more than one 'A' (absent) or more than two continuous 'L' (late) . Example 1: Input: n = 2 Output: 8 Explanation: There are 8 records with length 2 will be regarded as rewardable: "PP" , "AP", "PA", "LP", "PL", "AL"

[LeetCode]题解(python):80-Remove Duplicates from Sorted Array II

狂风中的少年 提交于 2020-01-23 00:59:56
题目来源 https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ Follow up for "Remove Duplicates": What if duplicates are allowed at most twice ? For example, Given sorted array nums = [1,1,1,2,2,3] , Your function should return length = 5 , with the first five elements of nums being 1 , 1 , 2 , 2 and 3 . It doesn't matter what you leave beyond the new length. 题意分析 Input: type numsList[int]; Output: rtype int; Conditions:删除array中的重复元素,但是允许重复次数为2,保证返回的length长度下包含所需值,大于length长度的元素不考虑 题目思路 穷举思路,只要出现过两次,就将该元素忽略。其实因为数组是有序的,穷举效率肯定差一点,但是leetcode OJ通过了= = AC代码(Python) class Solution(object)

LeetCode All in One 题目讲解汇总(持续更新中...)

别来无恙 提交于 2020-01-22 18:29:53
Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs . The org sequence is a permutation of the integers from 1 to n, with 1 ≤ n ≤ 10 4 . Reconstruction means building a shortest common supersequence of the sequences in seqs (i.e., a shortest sequence so that all sequences in seqs are subsequences of it). Determine whether there is only one sequence that can be reconstructed from seqs and it is the org sequence. Example 1: Input: org: [1,2,3], seqs: [[1,2],[1,3]] Output: false Explanation: [1,2,3] is not the only one sequence that can be

LeetCode All in One 题目讲解汇总(持续更新中...)

丶灬走出姿态 提交于 2020-01-22 18:28:49
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? 这道题看起来很难,其实解法并没有那么复杂,当然我也是看了别人的解法才做出来的,先来看看两遍遍历的解法,首先初始化每个人一个糖果,然后这个算法需要遍历两遍,第一遍从左向右遍历,如果右边的小盆友的等级高,等加一个糖果,这样保证了一个方向上高等级的糖果多。然后再从右向左遍历一遍,如果相邻两个左边的等级高,而左边的糖果又少的话,则左边糖果数为右边糖果数加一。最后再把所有小盆友的糖果数都加起来返回即可。代码如下: 解法一: class Solution { public: int candy(vector<int>& ratings) { int res = 0, n =

Leetcode错误:control reaches end of non-void function [-Werror=return-type]

痴心易碎 提交于 2020-01-22 12:29:13
在Leetcode刷题新手常犯错误! 我们可能刚开始刷leetcode的时候,会遇到这样的错误。 虽然我们在函数中有了return语句,但是如果这个return不是在代码快的结尾,是没办法通过编译的 ,会报出这个错误。 出现这个错误加上return 0;就好了 来源: CSDN 作者: 路明晓风 链接: https://blog.csdn.net/weixin_42469969/article/details/104068158

Leetcode 1.两数之和(Two Sum)

天涯浪子 提交于 2020-01-22 03:36:48
Leetcode 1.两数之和 1 题目描述( Leetcode题目链接 )   给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。   你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。示例: 给定 nums = [ 2 , 7 , 11 , 15 ] , target = 9 因为 nums [ 0 ] + nums [ 1 ] = 2 + 7 = 9 所以返回 [ 0 , 1 ] 2 题解   可以先将数组和下标排序,然后从左右两端向中间遍历,如果遇到两数之和等于target,则返回相应的下标。如果不等于target则分两种情况,如果大于target则移动right,如果小于target则移动left。 class Solution : def twoSum ( self , nums : List [ int ] , target : int ) - > List [ int ] : sort_nums = sorted ( enumerate ( nums ) , key = lambda x : x [ 1 ] ) print ( sort_nums ) left = 0 right = len ( nums ) - 1 while left < right : Sum

Leetcode 516.最长回文子序列( Longest Palindromic Subsequence)

混江龙づ霸主 提交于 2020-01-22 03:03:26
Leetcode 516.最长回文子序列 1 题目描述( Leetcode题目链接 )   给定一个字符串s,找到其中最长的回文子序列。可以假设s的最大长度为1000。示例: 输入: "bbbab" 输出: 4 2 题解   本题是动态规划问题,分析题目可知,一个字符串s的最长回文子序列可以通过两种情况得到(注意子序列可以由不连续的字符构成): 如果首字符和尾字符相等,则字符串s的最长回文子序列为2加上去除这两个字符的字符串的最长回文子序列; 如果首字符和尾字符不相等,则字符串s的最长回文子序列为去掉两个字符之一的两个字符串的最长回文子序列之间的最大值。 因此可以定义 D P [ i ] [ j ] DP[i][j] D P [ i ] [ j ] 为第 i i i 个字符到第 j j j 个字符的最长回文子序列,可以写为: D P = { 2 + D P [ i + 1 ] [ j − 1 ] i f s [ i ] = s [ j ] m a x ( D P [ i + 1 ] [ j ] , D P [ i ] [ j − 1 ] ) o t h e r w i s e DP = \begin{cases}2+DP[i+1][j-1] &&if&s[i]=s[j]\\max(DP[i+1][j],DP[i][j-1]) &&&otherwise\end{cases} D P

Leetcode 13.罗马数字转整数(Roman to Integer)

≯℡__Kan透↙ 提交于 2020-01-22 02:50:08
Leetcode 13.罗马数字转整数 1 题目描述( Leetcode题目链接 )   罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000   例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做 XXVII, 即为 XX + V + II 。   通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况: I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。 X 可以放在 L (50) 和 C (100) 的左边,来表示40 和 90。 C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。   给定一个罗马数字,将其转换成整数。输入确保在 1 到 3999 的范围内。 2 题解   首先建立字典,然后从左到右遍历字符串,比较当前字符和前一个字符的大小,如果当前字符小于等于前面的字符则结果加上当前字符,如果当前字符大于前一个字符则结果加上当前字符并减去 2倍 的前一个字符

Leetcode 62.不同路径(Unique Paths)

青春壹個敷衍的年華 提交于 2020-01-22 02:06:39
Leetcode 62.不同路径 1 题目描述( Leetcode题目链接 )   一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。   机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。   问总共有多少条不同的路径? 2 题解   本题为动态规划问题,可以构建一个 m ∗ n m*n m ∗ n 的数组,每个数组记录的值为到达该点的不同路径数,初始化第一行和第一列的值为1,DP状态转移方程如下: D P [ i ] [ j ] = D P [ i − 1 ] [ j ] + D P [ i ] [ j − 1 ] DP[i][j]=DP[i-1][j]+DP[i][j-1] D P [ i ] [ j ] = D P [ i − 1 ] [ j ] + D P [ i ] [ j − 1 ] 从第二行第二列的第一个元素开始填充DP数组,结果则为 D P [ m − 1 ] [ n − 1 ] DP[m-1][n-1] D P [ m − 1 ] [ n − 1 ] 。 class Solution : def uniquePaths ( self , m : int , n : int ) - > int : DP = [ ( [ 1 ] * ( n ) ) for i in range (

[LeetCode] Path Sum III 二叉树的路径和之三

谁说我不能喝 提交于 2020-01-21 22:11:15
You are given a binary tree in which each node contains an integer value. Find the number of paths that sum to a given value. The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes). The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000. Example: root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8 10 / \ 5 -3 / \ \ 3 2 11 / \ \ 3 -2 1 Return 3. The paths that sum to 8 are: 1. 5 -> 3 2. 5 -> 2 -> 1 3. -3 -> 11 这道题让我们求二叉树的路径的和等于一个给定值,说明了这条路径不必要从根节点开始,可以是中间的任意一段,而且二叉树的节点值也是有正有负