
Leetcode
文章平均质量分 90
Brielleqqqqqqjie
简单的事做到极致就是成功
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
LeetCode题解目录(1-100)(C++)
在努力更新中ヾ(◍°∇°◍)ノ゙题号 题名 考点 题解 难度 1 两数之和 hash C++/Python 简单 2 两数相加 链表 C++/Python 中等 3 无重复字符的最长子串 双指针+hash C++/Python 中等 4 寻找两个有序数组...原创 2019-05-26 11:17:25 · 664 阅读 · 0 评论 -
关于类中重载sort函数的报错问题(解决error: invalid use of non-static member function 'bool Solution::cmp(int, int)')
刷LeetCode的时候,由于最终结果是要写一个solution的类,但是比较头疼的是sort似乎不可以写一个cmp函数来重载。例如class Solution {public: int Hash[1005]; bool cmp(int a, int b){ return Hash[a] != Hash[b] ? Hash[a] < Hash[b]...原创 2019-07-14 20:10:01 · 3678 阅读 · 7 评论 -
LeetCode 二进制在枚举法上的应用
文章目录总结题解78. 子集90. 子集 II总结二进制位运算可以用来枚举,dp状态压缩的时候也可以派上用场。还有像是树状数组就是基于位运算。题解78. 子集对有n个数的的数组,他的子集个数应该是2n2^n2n,在[0,2n)[0,2^n)[0,2n)中遍历iii,不难发现,将数组中每个数字对应到 iii 的二进制表示。假设iii 为5,即101,那么第iii个集合中放入数组中第0和第2个数字。可以发现,由于iii 不重复,得到的也正是不重复的2n2^n2n的集合。class Solution原创 2021-04-02 10:06:41 · 3563 阅读 · 0 评论 -
LeetCode 二分法在旋转数组应用系列
文章目录总结题解33. 搜索旋转排序数组81. 搜索旋转排序数组 II153. 寻找旋转排序数组中的最小值154. 寻找旋转排序数组中的最小值 II寻找旋转排序数组中的最大值寻找旋转排序数组中的最大值II总结这个系列整理一下旋转数组,就是原始单调不减数组,在预先未知的某个下标 k(0 <= k < nums.length)上进行了 旋转,使数组变为 [nums[k], nums[k+1], …, nums[n-1], nums[0], nums[1], …, nums[k-1]]针对这种数原创 2021-03-18 10:53:31 · 4511 阅读 · 0 评论 -
LeetCode二分法的高级应用系列
最近刷到挺多巧用二分的题目,经常在hard题中出现。这类型的题下面会总结什么情况可以考虑二分法。另外二分法看起来简单,细节是魔鬼,这里也简单总结一下二分法的常用的几个模板。文章目录总结二分法模板题解1539. 第 k 个缺失的正整数总结二分法模板题解1539. 第 k 个缺失的正整数...原创 2021-03-12 10:40:36 · 3621 阅读 · 0 评论 -
LeetCode买卖股票系列
这几天基金一地基毛,钱没了,不如来总结一下LeetCode买卖股票的题,争取下次都在最佳时机买入卖出。总结题解121. 买卖股票的最佳时机贪心法,显然,如果我们真的在买卖股票,我们肯定会想:如果我是在历史最低点买的股票就好了!我们只要用一个变量记录一个历史最低价格 minprice,我们就可以假设自己的股票是在那天买的。那么我们在第 i 天卖出股票能得到的利润就是 prices[i]−minpriceprices[i] - minpriceprices[i]−minprice。需要更新的就是min原创 2021-03-09 13:55:27 · 2035 阅读 · 0 评论 -
LeetCode的石子游戏系列
整理一下亚历克斯和李用两个人闲来无聊玩的7场石子游戏(╯‵□′)╯︵┻━┻文章目录总结具体题解877. 石子游戏1140. 石子游戏 II1406. 石子游戏 III1510. 石子游戏 IV1563. 石子游戏 V1686. 石子游戏 VI1690. 石子游戏 VII总结双方博弈的题一般都是动态规划解决。也不排除像第一场游戏有讨巧的解法,或者第6场用的贪心方法。一般来讲,两端都可以取,考虑二维dp;只有一端,考虑一维dp;任意取,大概率贪心。两端的情况,先看游戏7。一端的情况,先看游戏3再看游原创 2021-03-07 19:09:14 · 1212 阅读 · 0 评论 -
团灭LeetCode关于回文串的算法题
总结LeetCode上关于回文字符串的题目原创 2021-02-23 02:58:21 · 986 阅读 · 2 评论 -
Leetcode1. Two Sum (C++/Python)
Given an array of integers, returnindicesof the two numbers such that they add up to a specific target.You may assume that each input would haveexactlyone solution, and you may not use thesame...原创 2018-12-30 08:28:36 · 288 阅读 · 0 评论 -
LeetCode2. Add Two Numbers(C++/Python)
You are given twonon-emptylinked lists representing two non-negative integers. The digits are stored inreverse orderand each of their nodes contain a single digit. Add the two numbers and return i...原创 2018-12-30 08:38:44 · 170 阅读 · 0 评论 -
LeetCode3. Longest Substring Without Repeating Characters(C++/Python)
Given a string, find the length of thelongest substringwithout repeating characters.Example 1:Input: "abcabcbb"Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2:...原创 2018-12-30 08:48:31 · 151 阅读 · 0 评论 -
LeetCode6. ZigZag Conversion (C++/Python)
The string"PAYPALISHIRING"is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA P L S I I...原创 2018-12-30 09:12:53 · 297 阅读 · 0 评论 -
Leetcode5. Longest Palindromic Substring(C++/Python)
Given a strings, find the longest palindromic substring ins. You may assume that the maximum length ofsis 1000.Example 1:Input: "babad"Output: "bab"Note: "aba" is also a valid answer.Exa...原创 2018-12-30 21:26:29 · 275 阅读 · 0 评论 -
LeetCode7.Rverse Integer(C++/Python)
Given a 32-bit signed integer, reverse digits of an integer.Example 1:Input: 123Output: 321Example 2:Input: -123Output: -321Example 3:Input: 120Output: 21Note:Assume we are dea...原创 2018-12-31 08:32:34 · 182 阅读 · 0 评论 -
LeetCode8. String to Integer (atoi)(C++/Python)
Implementatoiwhichconverts a string to an integer.The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from thi...原创 2018-12-31 08:46:29 · 228 阅读 · 0 评论 -
LeetCode9. Palindrome Number(C++/Python)
Determine whether an integer is a palindrome. An integerisapalindrome when itreads the same backward as forward.Example 1:Input: 121Output: trueExample 2:Input: -121Output: falseExpl...原创 2018-12-31 08:48:48 · 414 阅读 · 0 评论 -
LeetCode11. Container With Most Water(C++/Python)
Givennnon-negative integersa1,a2, ...,an, where each represents a point at coordinate (i,ai).nvertical lines are drawn such that the two endpoints of lineiis at (i,ai) and (i, 0). Find two...原创 2018-12-31 08:54:28 · 176 阅读 · 0 评论 -
LeetCode12. Integer to Roman(C++/Python)
Roman numerals are represented by seven different symbols:I,V,X,L,C,DandM.Symbol ValueI 1V 5X 10L 50C 100D ...原创 2018-12-31 08:59:14 · 186 阅读 · 0 评论 -
LeetCode13. Roman to Integer(C++/Python)
Roman numerals are represented by seven different symbols:I,V,X,L,C,DandM.Symbol ValueI 1V 5X 10L 50C 100D ...原创 2018-12-31 09:06:15 · 364 阅读 · 0 评论 -
LeetCode14. Longest Common Prefix(C++/Python)
Write 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: ["flower","flow","flight"]Output:...原创 2018-12-31 09:16:39 · 180 阅读 · 0 评论 -
LeetCode53. 最大子序和(C++)
给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。示例:输入: [-2,1,-3,4,-1,2,1,-5,4],输出: 6解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。进阶:如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的分治法求解。解题思路:贪心法,若此时相加的和小于等于0,则直接丢弃,下一...原创 2019-01-17 14:24:54 · 301 阅读 · 0 评论 -
LeetCode59. Spiral Matrix II(C++)
给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。示例:输入: 3输出:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]]解题思路:一圈一圈地进行填充,共(n+1)/2圈,每一圈分为四顺时针顺序填充四条边,填充每条边的时候第一个数填充,最后一个数放到填充第二条边的时候进行填充,第k圈的第一条边的...原创 2019-01-17 14:34:50 · 242 阅读 · 0 评论 -
LeetCode58. Length of Last Word (C++)
给定一个仅包含大小写字母和空格' '的字符串,返回其最后一个单词的长度。如果不存在最后一个单词,请返回 0。说明:一个单词是指由字母组成,但不包含任何空格的字符串。示例:输入: "Hello World"输出: 5class Solution {public: int lengthOfLastWord(string s) { int res...原创 2019-01-17 14:37:44 · 395 阅读 · 0 评论 -
LeetCode24. Swap Nodes in Pairs(C++/Python)
Given alinked list, swap every two adjacent nodes and return its head.Example:Given 1->2->3->4, you should return the list as 2->1->4->3.Note:Your algorithm should use only...原创 2019-01-12 13:31:18 · 277 阅读 · 0 评论 -
LeetCode26. Remove Duplicates from Sorted Array(C++/Python)
Given a sorted arraynums, remove the duplicatesin-placesuch that each element appear onlyonceand return the new length.Do not allocate extra space for another array, you must do this bymodifyi...原创 2019-01-12 16:34:22 · 243 阅读 · 0 评论 -
LeetCode27. Remove Element(C++/Python)
Given an arraynumsand a valueval, remove all instances of that valuein-placeand return the new length.Do not allocate extra space for another array, you must do this bymodifying the input arra...原创 2019-01-13 17:37:41 · 145 阅读 · 0 评论 -
LeetCode29. Divide Two Integers(C++/Python)
Given two integersdividendanddivisor, divide two integers without using multiplication, division and mod operator.Return the quotient after dividingdividendbydivisor.The integer division sho...原创 2019-07-15 16:52:43 · 440 阅读 · 0 评论 -
LeetCode28. Implement strStr()(C++/Python)
ImplementstrStr().Return the index of the first occurrence of needle in haystack, or-1if needle is not part of haystack.Example 1:Input: haystack = "hello", needle = "ll"Output: 2Example...原创 2019-01-13 17:51:47 · 260 阅读 · 1 评论 -
LeetCode74. Search a 2D Matrix(C++)
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right. The first integer of each row...原创 2019-01-23 23:07:15 · 243 阅读 · 0 评论 -
LeetCode75. Sort Colors(C++)
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the in...原创 2019-01-23 23:09:35 · 170 阅读 · 0 评论 -
LeetCode77. Combinations(C++)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.Example:Input: n = 4, k = 2Output:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]解题思路:回溯+剪枝...原创 2019-01-23 23:16:44 · 301 阅读 · 0 评论 -
LeetCode78. Subsets(C++)
Given a set ofdistinctintegers,nums, return all possible subsets (the power set).Note:The solution set must not contain duplicate subsets.Example:Input: nums = [1,2,3]Output:[ [3], [1...原创 2019-01-23 23:22:58 · 237 阅读 · 0 评论 -
LeetCode31. Next Permutation(C++/Python)
Implementnext permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible o...原创 2019-01-14 09:52:41 · 219 阅读 · 0 评论 -
LeetCode34. Find First and Last Position of Element in Sorted Array(C++/Python)
Given an array of integersnumssorted in ascending order, find the starting and ending position of a giventargetvalue.Your algorithm's runtime complexity must be in the order ofO(logn).If the...原创 2019-01-14 10:00:15 · 232 阅读 · 0 评论 -
LeetCode35. Search Insert Position(C++/Python)
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 no duplicates in the array....原创 2019-01-14 10:07:22 · 234 阅读 · 0 评论 -
LeetCode36. Valid Sudoku(C++/Python)
Determine if a9x9 Sudoku boardis valid.Only the filled cells need to be validatedaccording to the following rules:Each rowmust contain thedigits1-9without repetition. Each column must conta...原创 2019-01-14 10:21:26 · 270 阅读 · 1 评论 -
LeetCode38. Count and Say(C++/Python)
The count-and-say sequence is the sequence of integers with the first five terms as following:1. 12. 113. 214. 12115. 1112211is read off as"one 1"or11.11is read off ...原创 2019-01-14 10:30:18 · 194 阅读 · 0 评论 -
LeetCode15. 3Sum(C++/Python)
Given an arraynumsofnintegers, are there elementsa,b,cinnumssuch thata+b+c= 0? Find all unique triplets in the array which gives the sum of zero.Note:The solution set must not cont...原创 2019-01-08 20:09:53 · 263 阅读 · 0 评论 -
LeetCode55. Jump Game(C++)
Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if yo...原创 2019-01-19 21:49:23 · 239 阅读 · 0 评论 -
LeetCode16. 3Sum Closest(C++/Python)
Given an arraynumsofnintegers and an integertarget, find three integers innumssuch that the sum is closest totarget. Return the sum of the three integers. You may assume that each input would ...原创 2019-01-09 12:15:39 · 148 阅读 · 0 评论