![](https://img-blog.csdnimg.cn/20210810164653185.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
LeetCode
文章平均质量分 58
LeetCode刷题
ashtime99
因为热爱,所以探寻
展开
-
LeetCode#430. 扁平化多级双向链表
430. 扁平化多级双向链表难度:中等来源:LeetCode多级双向链表中,除了指向下一个节点和前一个节点指针之外,它还有一个子链表指针,可能指向单独的双向链表。这些子列表也可能会有一个或多个自己的子项,依此类推,生成多级数据结构,如下面的示例所示。给你位于列表第一级的头节点,请你扁平化列表,使所有结点出现在单级双链表中。示例 1:输入:head = [1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]输出:[1,2,3,7,8,11,1原创 2021-09-24 11:51:30 · 64 阅读 · 0 评论 -
LeetCode#326. 3的幂
326. 3的幂难度:简单来源:LeetCode给定一个整数,写一个函数来判断它是否是 3 的幂次方。如果是,返回 true ;否则,返回 false 。整数 n 是 3 的幂次方需满足:存在整数 x 使得 n == 3x示例 1:输入:n = 27输出:true示例 2:输入:n = 0输出:false示例 3:输入:n = 9输出:true示例 4:输入:n = 45输出:false提示:-231 <= n <= 231 - 1进阶:你原创 2021-09-23 15:47:29 · 82 阅读 · 0 评论 -
LeetCode#725. 分隔链表
725. 分隔链表难度:中等来源:LeetCode给你一个头结点为 head 的单链表和一个整数 k ,请你设计一个算法将链表分隔为 k 个连续的部分。每部分的长度应该尽可能的相等:任意两部分的长度差距不能超过 1 。这可能会导致有些部分为 null 。这 k 个部分应该按照在链表中出现的顺序排列,并且排在前面的部分的长度应该大于或等于排在后面的长度。返回一个由上述 k 部分组成的数组。示例 1:输入:head = [1,2,3], k = 5输出:[[1],[2],[3],[],[]原创 2021-09-23 15:46:16 · 63 阅读 · 0 评论 -
LeetCode#383. 赎金信
383. 赎金信难度:简单来源:LeetCode给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串 ransom 能不能由第二个字符串 magazines 里面的字符构成。如果可以构成,返回 true ;否则返回 false。(题目说明:为了不暴露赎金信字迹,要从杂志上搜索各个需要的字母,组成单词来表达意思。杂志字符串中的每个字符只能在赎金信字符串中使用一次。)示例 1:输入:ransomNote = "a", magazine = "b"输出:f原创 2021-09-21 23:24:09 · 50 阅读 · 0 评论 -
LeetCode#58. 最后一个单词的长度
58. 最后一个单词的长度难度:简单来源:LeetCode给你一个字符串 s,由若干单词组成,单词前后用一些空格字符隔开。返回字符串中最后一个单词的长度。单词 是指仅由字母组成、不包含任何空格字符的最大子字符串。示例 1:输入:s = "Hello World"输出:5示例 2:输入:s = " fly me to the moon "输出:4示例 3:输入:s = "luffy is still joyboy"输出:6提示:1 <= s.lengt原创 2021-09-21 23:23:59 · 86 阅读 · 0 评论 -
LeetCode#387. 字符串中的第一个唯一字符
387. 字符串中的第一个唯一字符难度:简单来源:LeetCode给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。示例:s = "leetcode"返回 0s = "loveleetcode"返回 2**提示:**你可以假定该字符串只包含小写字母。哈希表class Solution { public int firstUniqChar(String s) { Map<Character, Integer> h原创 2021-09-21 13:32:28 · 59 阅读 · 0 评论 -
LeetCode#292. Nim 游戏
292. Nim 游戏难度:简单来源:LeetCode你和你的朋友,两个人一起玩 Nim 游戏:桌子上有一堆石头。你们轮流进行自己的回合,你作为先手。每一回合,轮到的人拿掉 1 - 3 块石头。拿掉最后一块石头的人就是获胜者。假设你们每一步都是最优解。请编写一个函数,来判断你是否可以在给定石头数量为 n 的情况下赢得游戏。如果可以赢,返回 true;否则,返回 false 。示例 1:输入:n = 4输出:false 解释:如果堆中有 4 块石头,那么你永远不会赢得比赛;原创 2021-09-18 14:30:44 · 58 阅读 · 0 评论 -
LeetCode#118. 杨辉三角
118. 杨辉三角难度:简单来源:LeetCode给定一个非负整数 numRows,生成「杨辉三角」的前 numRows 行。在「杨辉三角」中,每个数是它左上方和右上方的数的和。示例 1:输入: numRows = 5输出: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]示例 2:输入: numRows = 1输出: [[1]]提示:1 <= numRows <= 30class Solution { public原创 2021-09-16 14:44:32 · 58 阅读 · 0 评论 -
LeetCode#566.重塑矩阵
566. 重塑矩阵难度:简单来源:LeetCode在 MATLAB 中,有一个非常有用的函数 reshape ,它可以将一个 m x n 矩阵重塑为另一个大小不同(r x c)的新矩阵,但保留其原始数据。给你一个由二维数组 mat 表示的 m x n 矩阵,以及两个正整数 r 和 c ,分别表示想要的重构的矩阵的行数和列数。重构后的矩阵需要将原始矩阵的所有元素以相同的 行遍历顺序 填充。如果具有给定参数的 reshape 操作是可行且合理的,则输出新的重塑矩阵;否则,输出原始矩阵。示例 1:原创 2021-09-16 12:49:08 · 62 阅读 · 0 评论 -
LeetCode#162.寻找峰值
162. 寻找峰值难度:中等来源:LeetCode峰值元素是指其值严格大于左右相邻值的元素。给你一个整数数组 nums,找到峰值元素并返回其索引。数组可能包含多个峰值,在这种情况下,返回 任何一个峰值 所在位置即可。你可以假设 nums[-1] = nums[n] = -∞ 。你必须实现时间复杂度为 O(log n) 的算法来解决此问题。示例 1:输入:nums = [1,2,3,1]输出:2解释:3 是峰值元素,你的函数应该返回其索引 2。示例 2:输入:nums = [1,2原创 2021-09-15 11:26:46 · 58 阅读 · 0 评论 -
LeetCode#1894. Find the Student that Will Replace the Chalk
1894. 找到需要补充粉笔的学生编号难度:中等来源:LeetCode一个班级里有 n 个学生,编号为 0 到 n - 1 。每个学生会依次回答问题,编号为 0 的学生先回答,然后是编号为 1 的学生,以此类推,直到编号为 n - 1 的学生,然后老师会重复这个过程,重新从编号为 0 的学生开始回答问题。给你一个长度为 n 且下标从 0 开始的整数数组 chalk 和一个整数 k 。一开始粉笔盒里总共有 k 支粉笔。当编号为 i 的学生回答问题时,他会消耗 chalk[i] 支粉笔。如果剩余粉笔数量原创 2021-09-10 17:49:39 · 70 阅读 · 0 评论 -
LeetCode#1221. Split a String in Balanced Strings
1221. Split a String in Balanced Strings难度:简单来源:LeetCodeBalanced strings are those that have an equal quantity of 'L' and 'R' characters.Given a balanced string s, split it in the maximum amount of balanced strings.Return the maximum amount of split b原创 2021-09-07 23:45:07 · 70 阅读 · 0 评论 -
LeetCode#704. Binary Search
704. Binary Search难度:简单来源:LeetCodeGiven an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.You must write an algo原创 2021-09-07 23:35:08 · 106 阅读 · 0 评论 -
剑指Offer#10-I. 斐波那契数列
剑指 Offer 10- I. 斐波那契数列难度:简单来源:LeetCode写一个函数,输入 n ,求斐波那契(Fibonacci)数列的第 n 项(即 F(N))。斐波那契数列的定义如下:F(0) = 0, F(1) = 1F(N) = F(N - 1) + F(N - 2), 其中 N > 1.斐波那契数列由 0 和 1 开始,之后的斐波那契数就是由之前的两数相加而得出。答案需要取模 1e9+7(1000000007),如计算初始结果为:1000000008,请返回 1。示例原创 2021-09-05 23:21:33 · 67 阅读 · 0 评论 -
LeetCode#470. Implement Rand10() Using Rand7()
470. Implement Rand10() Using Rand7()难度:中等来源:LeetCodeGiven the API rand7() that generates a uniform random integer in the range [1, 7], write a function rand10() that generates a uniform random integer in the range [1, 10]. You can only call the API ran原创 2021-09-05 23:12:03 · 77 阅读 · 0 评论 -
程序员面试宝典6-面试题17.14.最小K个数
面试题 17.14. 最小K个数难度:中等来源:LeetCode设计一个算法,找出数组中最小的k个数。以任意顺序返回这k个数均可。示例:输入: arr = [1,3,5,7,2,4,6,8], k = 4输出: [1,2,3,4]提示:0 <= len(arr) <= 1000000 <= k <= min(100000, len(arr))直接遍历class Solution { public int[] smallestK ( int[]原创 2021-09-03 15:08:26 · 71 阅读 · 0 评论 -
剑指Offer#22-链表中倒数第k个节点
剑指 Offer 22. 链表中倒数第k个节点难度:简单来源:LeetCode输入一个链表,输出该链表中倒数第k个节点。为了符合大多数人的习惯,本题从1开始计数,即链表的尾节点是倒数第1个节点。例如,一个链表有 6 个节点,从头节点开始,它们的值依次是 1、2、3、4、5、6。这个链表的倒数第 3 个节点是值为 4 的节点。示例:给定一个链表: 1->2->3->4->5, 和 k = 2.返回链表 4->5.双指针一前一后,中间隔k个节点,当后节点到达尾原创 2021-09-02 11:17:43 · 40 阅读 · 0 评论 -
LeetCode#165. Compare Version Numbers
165. Compare Version Numbers难度:中等来源:LeetCodeGiven two version numbers, version1 and version2, compare them.Version numbers consist of one or more revisions joined by a dot '.'. Each revision consists of digits and may contain leading zeros. Every revis原创 2021-09-01 14:25:08 · 77 阅读 · 0 评论 -
LeetCode#1109. Corporate Flight Bookings
1109. Corporate Flight Bookings难度:中等来源:LeetCodeThere are n flights that are labeled from 1 to n.You are given an array of flight bookings bookings, where bookings[i] = [firsti, lasti, seatsi] represents a booking for flights firsti through lasti (inclu原创 2021-08-31 15:38:41 · 90 阅读 · 0 评论 -
LeetCode#1588. Sum of All Odd Length Subarrays
1588. Sum of All Odd Length SubarraysGiven an array of positive integers arr, calculate the sum of all possible odd-length subarrays.A subarray is a contiguous subsequence of the array.Return the sum of all odd-length subarrays of ar原创 2021-08-29 23:13:32 · 102 阅读 · 0 评论 -
LeetCode#881. Boats to Save People
881. Boats to Save PeopleYou are given an array people where people[i] is the weight of the ith person, and an infinite number of boats where each boat can carry a maximum weight of limit. Each boat carries at most two people at the same time, provided th原创 2021-08-26 18:21:05 · 80 阅读 · 0 评论 -
LeetCode#121. Best Time to Buy and Sell Stock
121. Best Time to Buy and Sell Stock难度:简单You are given an array prices where prices[i] is the price of a given stock on the ith day.You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to原创 2021-08-19 15:41:04 · 66 阅读 · 0 评论 -
LeetCode#350. Intersection of Two Arrays II
350. Intersection of Two Arrays II难度:简单Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must appear as many times as it shows in both arrays and you may return the result in any order.Example 1:原创 2021-08-19 14:56:20 · 94 阅读 · 0 评论 -
LeetCode#1137. N-th Tribonacci Number
1137. N-th Tribonacci NumberThe Tribonacci sequence Tn is defined as follows:T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0.Given n, return the value of Tn.Example 1:Input: n = 4Output: 4Explanation:T_3 = 0 + 1 + 1 = 2T_4 = 1 +原创 2021-08-18 12:24:14 · 84 阅读 · 0 评论 -
LeetCode#509. Fibonacci Number
509. Fibonacci NumberThe Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,F(0) = 0, F(1) = 1F(n) = F(n - 1) + F(n - 2), fo原创 2021-08-18 11:43:12 · 65 阅读 · 0 评论 -
LeetCode#88. Merge Sorted Array
88. Merge Sorted ArrayYou are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively.Merge nums1 and nums2 into a single array sorted in non-原创 2021-08-18 11:23:53 · 138 阅读 · 0 评论 -
LeetCode#53. Maximum Subarray
53. Maximum SubarrayGiven an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.A subarray is a contiguous part of an array.Example 1:Input: nums = [-2,1,-3,4,-1,2,1,-5,4]Outp原创 2021-08-17 18:12:01 · 87 阅读 · 0 评论 -
LeetCode#217. Contains Duplicate
217. Contains DuplicateGiven an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.Example 1:Input: nums = [1,2,3,1]Output: trueExample 2:Input: nums = [1,2,3,4]Output: fa原创 2021-08-17 15:20:57 · 61 阅读 · 0 评论 -
LeetCode#551. Student Attendance Record I
551. Student Attendance Record IYou are given a string s representing an attendance record for a student where each character signifies whether the student was absent, late, or present on that day. The record only contains the following three characters:原创 2021-08-17 15:04:53 · 77 阅读 · 0 评论 -
LeetCode#233. Number of Digit One
233. Number of Digit OneGiven an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.Example 1:Input: n = 13Output: 6Example 2:Input: n = 0Output: 0 Constraints:0 <= n <= 109找规律参原创 2021-08-13 14:25:39 · 70 阅读 · 0 评论 -
LeetCode#5. Longest Palindromic Substring
5. Longest Palindromic SubstringGiven a string s, return the longest palindromic substring in s.Example 1:Input: s = "babad"Output: "bab"Note: "aba" is also a valid answer.Example 2:Input: s = "cbbd"Output: "bb"Example 3:Input: s = "a"Out原创 2021-08-12 17:15:21 · 91 阅读 · 0 评论 -
LeetCode#516. Longest Palindromic Subsequence
516. Longest Palindromic SubsequenceGiven a string s, find the longest palindromic subsequence’s length in s.A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining原创 2021-08-12 12:38:46 · 71 阅读 · 0 评论 -
LeetCode#15. 3Sum
15. 3SumGiven an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.Notice that the solution set must not contain duplicate triplets.Example 1:Input: nums =原创 2021-08-11 12:18:54 · 72 阅读 · 0 评论 -
LeetCode#14. Longest Common Prefix
14. Longest Common PrefixWrite a function to find the longest common prefix string amongst an array of strings.If there is no common prefix, return an empty string "".Example 1:Input: strs = ["flower","flow","flight"]Output: "fl"Example 2:Input: st原创 2021-08-10 16:20:30 · 79 阅读 · 0 评论 -
LeetCode#13. Roman to Integer
13. Roman to IntegerRoman numerals are represented by seven different symbols: I, V, X, L, C, D and M.Symbol ValueI 1V 5X 10L 50C 100D 500M 1000For ex原创 2021-08-10 15:10:01 · 83 阅读 · 0 评论 -
LeetCode#12. Integer to Roman
12. Integer to RomanRoman numerals are represented by seven different symbols: I,V, X, L, C, D and M.Symbol ValueI 1V 5X 10L 50C 100D 500M 1000For example,原创 2021-08-10 11:20:15 · 79 阅读 · 0 评论 -
LeetCode#11. Container With Most Water
11. Container With Most WaterGiven n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of the line i is at (i, ai) and (i, 0). Find two lines, which, together w原创 2021-08-09 14:50:16 · 80 阅读 · 0 评论 -
LeetCode#9. Palindrome Number
9. Palindrome NumberGiven an integer x, return true if x is palindrome integer.An integer is a palindrome when it reads the same backward as forward. For example, 121 is palindrome while 123 is not.Example 1:Input: x = 121Output: trueExample 2:Inp原创 2021-08-09 12:30:32 · 91 阅读 · 0 评论 -
LeetCode#8. String to Integer (atoi)
8. String to Integer (atoi)Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++'s atoi function).The algorithm for myAtoi(string s) is as follows:Read in and ignore any leading whitespace.Check原创 2021-08-09 11:38:54 · 88 阅读 · 0 评论 -
LeetCode#3. Longest Substring Without Repeating Characters
3. Longest Substring Without Repeating CharactersGiven a string s, find the length of the longest substring without repeating characters.Example 1:Input: s = "abcabcbb"Output: 3Explanation: The answer is "abc", with the length of 3.Example 2:Input:原创 2021-07-29 14:59:19 · 79 阅读 · 0 评论