![](https://img-blog.csdnimg.cn/20201014180756922.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Leetcode题解
小·幸·运
你的所有努力最后都会回赠予你。
展开
-
LeetCode 215. 数组中的第K个最大元素
数组中的第K个最大元素在未排序的数组中找到第 k 个最大的元素。请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。示例 1:输入: [3,2,1,5,6,4] 和 k = 2输出: 5示例 2:输入: [3,2,3,1,2,4,5,5,6] 和 k = 4输出: 4说明:你可以假设 k 总是有效的,且 1 ≤ k ≤ 数组的长度。方法一:使用堆排序建立大顶堆,每次建堆完毕,把堆顶元素和数组最后面的数调换位置,如此重复k次。参考代码:class S.原创 2021-03-27 14:42:50 · 146 阅读 · 0 评论 -
LeetCode:117. 填充每个节点的下一个右侧节点指针 II
填充每个节点的下一个右侧节点指针 II难度中等给定一个二叉树struct Node { int val; Node *left; Node *right; Node *next;}填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。初始状态下,所有 next 指针都被设置为 NULL。进阶:你只能使用常量级额外空间。使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。示.原创 2021-03-26 11:35:13 · 136 阅读 · 1 评论 -
leetcode-168. Excel表列名称
给定一个正整数,返回它在 Excel 表中相对应的列名称。例如, 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB ...示例 1:输入: 1输出: "A"示例2:输入: 28输出: "AB"示例3:输入: 701输出: "ZY"思路:这是一个26进制的转换题,把1-26变成对应的A-Z,但是要注意,在对n求余数时...原创 2021-02-24 18:26:52 · 84 阅读 · 0 评论 -
leetcode:63. 不同路径 II----动态规划解法
一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?网格中的障碍物和空位置分别用 1 和 0 来表示。示例 1:输入:obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]输出:2解释:3x3 网格的正中间有一个障碍物。从左上角到右下角一共有 2 条不同的路径:原创 2021-02-18 17:45:20 · 157 阅读 · 0 评论 -
LeetCode--697. Degree of an Array
Given a non-empty array of non-negative integers nums, the degree of this array is defined as the maximum frequency of any one of its elements.Your task is to find the smallest possible length of a (c...原创 2018-06-07 20:13:00 · 141 阅读 · 0 评论 -
LeetCode--830. Positions of Large Groups
In a string S of lowercase letters, these letters form consecutive groups of the same character.For example, a string like S = "abbxxxxzyy" has the groups "a", "bb", "xxxx", "z" and "yy".Call a gr原创 2018-06-09 19:52:23 · 210 阅读 · 0 评论 -
LeetCode--747. Largest Number At Least Twice of Others
In a given integer array nums, there is always exactly one largest element.Find whether the largest element in the array is at least twice as much as every other number in the array.If it is, return t...原创 2018-06-09 20:11:49 · 113 阅读 · 0 评论 -
LeetCode--840. Magic Squares In Grid
A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row, column, and both diagonals all have the same sum.Given an grid of integers, how many 3 x 3 "magic squa...原创 2018-06-09 21:26:14 · 421 阅读 · 0 评论 -
LeetCode--344. Reverse String
Write a function that takes a string as input and returns the string reversed.Example:Given s = "hello", return "olleh".题意:给定一个字符串,逆转后输出。参考代码:class Solution {public: string reverseString(string s...原创 2018-06-09 21:53:18 · 121 阅读 · 0 评论 -
LeetCode--541. Reverse String II
Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of them...原创 2018-06-17 21:47:45 · 94 阅读 · 0 评论 -
LeetCode--521. Longest Uncommon Subsequence I
Given a group of two strings, you need to find the longest uncommon subsequence of this group of two strings.The longest uncommon subsequence is defined as the longest subsequence of one of these stri...原创 2018-06-17 22:19:03 · 109 阅读 · 0 评论 -
LeetCode--38. Count and Say
The count-and-say sequence is the sequence of integers with the first five terms as following:1. 12. 113. 214. 12115. 1112211 is read off as "one 1" or 11.11 is read off as "t...原创 2018-06-10 20:08:55 · 83 阅读 · 0 评论 -
LeetCode--520. Detect Capital
Given a word, you need to judge whether the usage of capitals in it is right or not.We define the usage of capitals in a word to be right when one of the following cases holds:All letters in this word...原创 2018-06-15 22:22:09 · 154 阅读 · 0 评论 -
LeetCode--746. Min Cost Climbing Stairs
On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed).Once you pay the cost, you can either climb one or two steps. You need to find minimum cost to reach the top of th...原创 2018-06-08 22:15:55 · 133 阅读 · 0 评论 -
LeetCode--414. Third Maximum Number
Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).Example 1:Input: [3, 2, 1]...原创 2018-05-31 21:01:35 · 101 阅读 · 0 评论 -
LeetCode--448. Find All Numbers Disappeared in an Array
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements of [1, n] inclusive that do not appear in this array.Could yo...原创 2018-05-31 22:18:43 · 86 阅读 · 0 评论 -
LeetCode--459. Repeated Substring Pattern
Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase Engl...原创 2018-06-14 21:44:07 · 123 阅读 · 0 评论 -
LeetCode--434. Number of Segments in a String
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters.Please note that the string does not contain any non-printable characters.Examp...原创 2018-06-14 22:15:24 · 127 阅读 · 0 评论 -
LeetCode--566. Reshape the Matrix
In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data.You're given a matrix represented by a two-dimensi...原创 2018-06-01 14:22:24 · 133 阅读 · 0 评论 -
LeetCode--485. Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array.Example 1:Input: [1,1,0,1,1,1]Output: 3Explanation: The first two digits or the last three digits are consecutive 1s. ...原创 2018-06-01 16:37:35 · 109 阅读 · 0 评论 -
LeetCode--581. Shortest Unsorted Continuous Subarray
Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too. You need to find ...原创 2018-06-01 19:56:57 · 165 阅读 · 0 评论 -
LeetCode--724. Find Pivot Index
Given an array of integers nums, write a method that returns the "pivot" index of this array.We define the pivot index as the index where the sum of the numbers to the left of the index is equal to th...原创 2018-06-08 19:33:37 · 104 阅读 · 0 评论 -
LeetCode--58. Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word is defined a...原创 2018-06-10 21:06:41 · 140 阅读 · 0 评论 -
LeetCode--680. Valid Palindrome II
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome.Example 1:Input: "aba"Output: TrueExample 2:Input: "abca"Output: TrueExplanation: You ...原创 2018-07-08 21:18:30 · 93 阅读 · 0 评论 -
LeetCode--973. K Closest Points to Origin
We have a list of points on the plane. Find the K closest points to the origin (0, 0).(Here, the distance between two points on a plane is the Euclidean distance.)You may return the answer in any...原创 2019-02-12 22:31:31 · 146 阅读 · 0 评论 -
LeetCode--942. DI String Match
Given a string S that only contains "I" (increase) or "D" (decrease), let N = S.length.Return any permutation A of [0, 1, ..., N] such that for all i = 0, ..., N-1:If S[i] == "I", then A[i] < A...原创 2019-02-11 21:55:05 · 149 阅读 · 0 评论 -
LeetCode--937. Reorder Log Files
You have an array of logs. Each log is a space delimited string of words.For each log, the first word in each log is an alphanumeric identifier. Then, either:Each word after the identifier will ...原创 2019-02-11 19:53:38 · 337 阅读 · 0 评论 -
LeetCode--929. Unique Email Addresses
Every email consists of a local name and a domain name, separated by the @ sign.For example, in alice@leetcode.com, alice is the local name, and leetcode.com is the domain name.Besides lowercase l...原创 2019-02-11 16:10:18 · 299 阅读 · 0 评论 -
LeetCode--849. Maximize Distance to Closest Person
In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is empty. There is at least one empty seat, and at least one person sitting.Alex wants to sit in the s...原创 2019-01-15 18:19:28 · 139 阅读 · 0 评论 -
LeetCode--888. Fair Candy Swap
Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that Alice has, and B[j] is the size of the j-th bar of candy that Bob has.Since they are friends, they wo...原创 2019-01-13 21:34:14 · 145 阅读 · 1 评论 -
LeetCode--606. Construct String from Binary Tree
You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way.The null node needs to be represented by empty parenthesis pair "()". And you ne...原创 2018-07-07 22:51:24 · 103 阅读 · 0 评论 -
LeetCode--551. Student Attendance Record I
You are given a string representing an attendance record for a student. The record only contains the following three characters:'A' : Absent. 'L' : Late. 'P' : Present. A student could be rewarded if ...原创 2018-07-07 20:16:26 · 160 阅读 · 0 评论 -
LeetCode--557. Reverse Words in a String III
Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.Example 1:Input: "Let's take LeetCode contest"Outpu...原创 2018-07-07 19:44:14 · 134 阅读 · 0 评论 -
LeetCode--198. House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent house...原创 2018-07-12 22:15:13 · 120 阅读 · 0 评论 -
LeetCode--859. Buddy Strings
Given two strings A and B of lowercase letters, return true if and only if we can swap two letters in A so that the result equals B. Example 1:Input: A = "ab", B = "ba"Output: trueExample 2:Input: A...原创 2018-07-11 22:26:28 · 309 阅读 · 0 评论 -
LeetCode--824. Goat Latin
A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and uppercase letters only.We would like to convert the sentence to "Goat Latin" (a made-up language simil...原创 2018-07-11 21:54:06 · 290 阅读 · 0 评论 -
LeetCode--819. Most Common Word
Given a paragraph and a list of banned words, return the most frequent word that is not in the list of banned words. It is guaranteed there is at least one word that isn't banned, and that the answer...原创 2018-07-11 21:27:02 · 179 阅读 · 0 评论 -
LeetCode--788. Rotated Digits
X is a good number if after rotating each digit individually by 180 degrees, we get a valid number that is different from X. Each digit must be rotated - we cannot choose to leave it alone.A number i...原创 2018-07-10 22:02:24 · 131 阅读 · 0 评论 -
LeetCode--387. First Unique Character in a String
Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.Examples:s = "leetcode"return 0.s = "loveleetcode",return 2.Note: You may assume...原创 2018-07-10 21:32:58 · 109 阅读 · 0 评论 -
LeetCode--443. String Compression
Given an array of characters, compress it in-place.The length after compression must always be smaller than or equal to the original array.Every element of the array should be a character (not int) of...原创 2018-07-09 22:23:15 · 99 阅读 · 0 评论