![](https://img-blog.csdnimg.cn/20190804122352597.jpeg?x-oss-process=image/resize,m_fixed,h_224,w_224)
LeetCode刷题题解记录
此专栏为本人的LeetCode刷题记录,欢迎各位指正。
Ubuntu1996
这个作者很懒,什么都没留下…
展开
-
LeetCode题解及源码汇总
My C++ Solutions for LeetCodeC++菜鸟,欢迎探讨指正!#TitleC++JavaPythonDifficultyCSDN1Two SumC++PythonEasyCSDN2Add Two NumbersC++MediumCSDN3Longest Substring Without Repeatin...原创 2019-08-04 12:11:38 · 3110 阅读 · 1 评论 -
LeetCode 1170:比较字符串最小字母出现频次(Compare Strings by Frequency of the Smallest Character)解法汇总
文章目录Solution更多LeetCode题解Let’s define a function f(s) over a non-empty string s, which calculates the frequency of the smallest character in s. For example, if s = "dcce" then f(s) = 2 because the sm...原创 2019-08-25 13:34:31 · 252 阅读 · 0 评论 -
LeetCode 1169:查询无效交易(Invalid Transactions)解法汇总
文章目录Solution更多LeetCode题解A transaction is possibly invalid if:the amount exceeds $1000, or;if it occurs within (and including) 60 minutes of another transaction with the same name in a different c...原创 2019-08-25 13:28:51 · 1265 阅读 · 0 评论 -
LeetCode 1154:一年中的第几天(Day of the Year)解法汇总
文章目录Solution更多LeetCode题解Given a string date representing a Gregorian calendar date formatted as YYYY-MM-DD, return the day number of the year.Example 1:Input: date = “2019-01-09”Output: 9Explan...原创 2019-08-20 10:47:11 · 540 阅读 · 0 评论 -
LeetCode 1144:递减元素使数组呈锯齿状(Decrease Elements To Make Array Zigzag)解法汇总
文章目录Solution更多LeetCode题解Given an array nums of integers, a move consists of choosing any element and decreasing it by 1.An array A is a zigzag array if either:Every even-indexed element is greate...原创 2019-08-04 12:08:58 · 294 阅读 · 0 评论 -
LeetCode 48:旋转图像(Rotate Image)解法汇总
文章目录Solution更多LeetCode题解You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Note:You have to rotate the image in-place, which means you have to modif...原创 2019-08-03 14:04:56 · 292 阅读 · 0 评论 -
LeetCode 47:全排列 II(Permutations II)解法汇总
文章目录Solution更多LeetCode题解Given a collection of numbers that might contain duplicates, return all possible unique permutations.Example:Input: [1,1,2]Output:[[1,1,2],[1,2,1],[2,1,1]]Solution...原创 2019-08-02 21:45:55 · 212 阅读 · 0 评论 -
LeetCode 46:全排列(Permutations)解法汇总
文章目录Solution更多LeetCode题解Given a collection of distinct integers, return all possible permutations.Example:Input: [1,2,3]Output:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]Solution...原创 2019-08-01 16:13:28 · 257 阅读 · 0 评论 -
LeetCode 43:字符串相乘(Multiply Strings)解法汇总
文章目录Solution更多LeetCode题解Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.Example 1:Input: num1 = “2”, num2 =...原创 2019-07-31 15:50:14 · 248 阅读 · 0 评论 -
LeetCode 40:组合总和 II(Combination Sum II)解法汇总
文章目录Solution更多LeetCode题解Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.Each...原创 2019-07-30 19:46:14 · 183 阅读 · 0 评论 -
LeetCode 39:组合总和(Combination Sum)解法汇总
文章目录Solution更多LeetCode题解Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums t...原创 2019-07-29 21:41:50 · 206 阅读 · 0 评论 -
LeetCode 1139:最大的以 1 为边界的正方形(Largest 1-Bordered Square)解法汇总
文章目录Solution更多LeetCode题解Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn’t exist in the grid.Exa...原创 2019-07-28 12:14:32 · 549 阅读 · 0 评论 -
LeetCode 1138:字母板上的路径(Alphabet Board Path)解法汇总
文章目录Solution更多LeetCode题解On an alphabet board, we start at position (0, 0), corresponding to character board[0][0].Here, board = ["abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"].We may make the f...原创 2019-07-28 12:11:20 · 416 阅读 · 0 评论 -
LeetCode 1137:第 N 个泰波那契数(N-th Tribonacci Number)解法汇总
文章目录Solution更多LeetCode题解The Tribonacci sequence Tn is defined as follows:T0=0T_0 = 0T0=0, T1=1T_1 = 1T1=1, T2=1T_2 = 1T2=1, and Tn+3=Tn+Tn+1+Tn+2T_{n+3} = T_n + T_{n+1} + T_{n+2}Tn+3=Tn+Tn+1+...原创 2019-07-28 12:09:08 · 287 阅读 · 0 评论 -
LeetCode 36:有效的数独(Valid Sudoku)解法汇总
文章目录Solution更多LeetCode题解Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:Each row must contain the digits 1-9 without repetitio...原创 2019-07-27 22:00:28 · 149 阅读 · 0 评论 -
LeetCode 38:报数(Count and Say)解法汇总
文章目录Solution更多LeetCode题解The count-and-say sequence is the sequence of integers with the first five terms as following:1112112111112211 is read off as “one 1” or 11.11 is read...原创 2019-07-26 23:03:29 · 202 阅读 · 0 评论 -
LeetCode 34:在排序数组中查找元素的第一个和最后一个位置(Find First and Last Position of Element in Sorted Array)解法汇总
文章目录Solution更多LeetCode题解Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.Your algorithm’s runtime complexity must be in the o...原创 2019-07-25 23:26:19 · 280 阅读 · 0 评论 -
LeetCode 33:搜索旋转排序数组(Search in Rotated Sorted Array)解法汇总
文章目录Solution更多LeetCode题解Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).You are given a target va...原创 2019-07-24 16:44:30 · 191 阅读 · 0 评论 -
LeetCode 1128:等价多米诺骨牌对的数量(Number of Equivalent Domino Pairs)解法汇总
文章目录My Solution更多LeetCode题解Given a list of dominoes, dominoes[i] = [a, b] is equivalent to dominoes[j] = [c, d] if and only if either (a==c and b==d), or (a==d and b==c) - that is, one domino can be...原创 2019-07-21 11:54:55 · 199 阅读 · 0 评论 -
LeetCode 31:下一个排列(Next Permutation)解法汇总
文章目录Solution更多LeetCode题解Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it...原创 2019-07-20 17:40:26 · 200 阅读 · 0 评论 -
LeetCode 29:两数相除(Divide Two Integers)解法汇总
文章目录Solution更多LeetCode题解Given two integers dividend and divisor, divide two integers without using multiplication, division and mod operator.Return the quotient after dividing dividend by divisor....原创 2019-07-16 16:38:04 · 207 阅读 · 0 评论 -
LeetCode 1124:表现良好的最长时间段(Longest Well-Performing Interval)解法汇总
文章目录My Solution更多LeetCode题解We are given hours, a list of the number of hours worked per day for a given employee.A day is considered to be a tiring day if and only if the number of hours worked is ...原创 2019-07-14 14:05:11 · 2031 阅读 · 0 评论 -
LeetCode 1122:数组的相对排序(Relative Sort Array)解法汇总
文章目录My Solution更多LeetCode题解Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all elements in arr2 are also in arr1.Sort the elements of arr1 such that the relative ordering of ...原创 2019-07-14 13:56:48 · 488 阅读 · 0 评论 -
LeetCode 23:合并K个排序链表(Merge k Sorted Lists)解法汇总
文章目录My Solution更多LeetCode题解Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.Example:Input:[1->4->5,1->3->4,2->6]Output: 1->...原创 2019-06-29 13:02:09 · 230 阅读 · 0 评论 -
LeetCode 35:搜索插入位置(Search Insert Position)解法汇总
文章目录My Solution更多LeetCode题解Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume n...原创 2019-06-27 21:29:42 · 288 阅读 · 0 评论 -
LeetCode 28:实现strStr()(Implement strStr())解法汇总
文章目录My Solution更多LeetCode题解Implement strStr().Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.Example 1:Input: haystack = “hello”, needle ...原创 2019-06-19 18:40:31 · 136 阅读 · 0 评论 -
LeetCode 27:移除元素(Remove Element)解法汇总
文章目录My Solution更多LeetCode题解Given an array nums and a value val, remove all instances of that value in-place and return the new length.Do not allocate extra space for another array, you must do this...原创 2019-06-09 16:42:39 · 195 阅读 · 0 评论 -
LeetCode 26:删除排序数组中的重复项(Remove Duplicates from Sorted Array)解法汇总
文章目录My Solution更多LeetCode题解Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.Do not allocate extra space for another array,...原创 2019-06-08 22:09:46 · 187 阅读 · 0 评论 -
LeetCode 24:两两交换链表中的节点(Swap Nodes in Pairs)解法汇总
文章目录My Solution更多LeetCode题解My Solution由于尝试交换未成功,所以使用了一种讨巧的方法:新建一个List,向这个List内添加结点。struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {}};class Solution {public...原创 2019-06-07 20:23:11 · 199 阅读 · 0 评论 -
LeetCode 22:括号生成(Generate Parentheses)解法汇总
更多LeetCode题解回溯法class Solution {public: char paren[2] = { '(',')' }; vector<string> generateParenthesis(int n) { vector<string> res; string str; for (int i = 0; i < 2 * n; i...原创 2019-05-08 20:07:10 · 193 阅读 · 0 评论 -
LeetCode 21:合并两个有序链表(Merge Two Sorted Lists)解法汇总
更多LeetCode题解有序链表的归并排序,很简单class Solution {public: bool isValid(string s) { stack<char> sta; for (int i = 0; i < s.size(); i++) { if (s[i] == '(' || s[i] == '[' || s[i] == '{') { ...原创 2019-04-30 14:29:04 · 198 阅读 · 0 评论 -
LeetCode 20:有效的括号(Valid Parentheses)解法汇总
更多LeetCode题解class Solution {public: bool isValid(string s) { stack<char> sta; for (int i = 0; i < s.size(); i++) { if (s[i] == '(' || s[i] == '[' || s[i] == '{') { sta.push(s[i]...原创 2019-04-30 13:57:18 · 271 阅读 · 0 评论 -
LeetCode 19:删除链表的倒数第N个节点(Remove Nth Node From End of List)解法汇总
文章目录My Solutionsimplified更多LeetCode题解Given a linked list, remove the n-th node from the end of list and return its head.Note:Given n will always be valid.My Solution将倒数转为正数:先计数,再删除第count-n+1个cla...原创 2019-04-29 11:00:44 · 227 阅读 · 0 评论 -
LeetCode 1033:移动石子直到连续(Moving Stones Until Consecutive)解法汇总
文章目录更多LeetCode题解这题没有太多好说的,直接上代码class Solution {public: vector<int> numMovesStones(int a, int b, int c) { vector<int> nums = { a,b,c }; sort(nums.begin(), nums.end()); vector<...原创 2019-04-28 13:04:04 · 350 阅读 · 0 评论 -
LeetCode 18:四数之和(4Sum)解法汇总
文章目录My Solution更多LeetCode题解My Solution在3Sum的基础上加一层循环class Solution {public: vector<vector<int>> fourSum(vector<int>& nums, int target) { if (nums.size() < 4) { return ...原创 2019-04-27 18:05:20 · 177 阅读 · 0 评论 -
LeetCode 17:电话号码的字母组合(Letter Combinations of a Phone Number)解法汇总
文章目录My Solution更多LeetCode题解My Solution本质上是一个字符串拼接的问题,输入n个数字,对应n个字符串,将这n个字符串以各种方式拼接。采用减而治之的策略,变成两个两个字符串数组的拼接。class Solution {public: vector<vector<string>> v = { {"a","b","c"}, {"d",...原创 2019-04-27 16:55:18 · 399 阅读 · 0 评论 -
LeetCode 16:最接近的三数之和(3Sum Closest)解法汇总
文章目录Brute Force与15题一样采用双指针更多LeetCode题解Brute Forceclass Solution {public: int threeSumClosest(vector<int>& nums, int target) { if (nums.size() < 3) { return 0; } int distance = ab...原创 2019-04-26 16:00:22 · 174 阅读 · 0 评论 -
LeetCode 15:三数之和(3Sum)解法汇总
文章目录My Solutionsimplified在寻找的过程中去重更多LeetCode题解My SolutionTLE//O(n^3), TLEclass Solution {public: vector<vector<int>> threeSum(vector<int>& nums) { vector<vector<in...原创 2019-04-26 15:52:44 · 284 阅读 · 0 评论 -
LeetCode 12:整数转罗马数字(Integer to Roman)解法汇总
文章目录My Solution勉强算第二种解法吧My Solutionclass Solution {public: vector<string> unit = { "","I","II","III","IV","V","VI","VII","VIII","IX" }; vector<string> ten = { "","X" , "XX", "XXX", "X...原创 2019-04-25 19:42:44 · 209 阅读 · 0 评论 -
LeetCode 10:正则表达式匹配(Regular Expression Matching)解法汇总
官方解答英文版:https://leetcode.com/problems/regular-expression-matching/solution/原创 2019-04-20 10:49:26 · 271 阅读 · 0 评论