#tri

leetcode 542. 01 Matrix 、663. Walls and Gates(lintcode) 、773. Sliding Puzzle 、803. Shortest Distance from All Buildings

542.01Matrixhttps://www.cnblogs.com/grandyang/p/6602288.html将所有的1置为INT_MAX,然后用所有的0去更新原本位置为1的值。最短距离肯定使用bfs。每次更新了值的地方还要再加入队列中。classSolution{public:vector<vecto...

leetcode 329. Longest Increasing Path in a Matrix

329.LongestIncreasingPathinaMatrixhttps://www.cnblogs.com/grandyang/p/5148030.html这个题是在二维数组中找递增序列的最长长度。因为使用dfs都是从当前位置进行搜索,所以每次dp计算的值是以当前为起点的最长长度。这里使用了一个二维数组记录每个...

leetcode 334. Increasing Triplet Subsequence

334.IncreasingTripletSubsequencehttps://www.cnblogs.com/grandyang/p/5194599.html要求时间复杂度为O(N),空间复杂度为O(1)。解题思路:用a来记录最小的数,b来记录次小的数。如果当前数比之前的数还小,则需要更新。当a、b不需要更新时,则证...

字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word

字典树(查找树)26个分支作用:检测字符串是否在这个字典里面插入、查找字典树与哈希表的对比:时间复杂度:以字符来看:O(N)、O(N)以字符串来看:O(1)、O(1)空间复杂度:字典树远远小于哈希表前缀相关的题目字典树优于哈希表字典树可以查询abc是否有ab的前缀字典树常考点:1.字典树实现2.利用字典树前缀特性解题3...

leetcode 73. Set Matrix Zeroes

73.SetMatrixZeroes题目要求是二维数组中出现0的地方所在行和所在列全变成0。题目的关键是O(1)的时间复杂度,也就是在原地做。思路:将第一行、第一列,除了(0,0)这个点的其他所有点用来记录这一行或者这一列是否出现0。具体做法:先判断第一行第一列本身是否有0,用两个变量保存。然后遍历从(1,1)到(n-...
代码星球 代码星球·2020-10-13

leetcode 395. Longest Substring with At Least K Repeating Characters

395.LongestSubstringwithAtLeastKRepeatingCharactershttps://www.cnblogs.com/grandyang/p/5852352.html题目的要求是找一段字符串,这段字符串中每个单词出现的次数都必须至少超过k次,求满足这种条件的字符串的最长的长度。暴力的方法...

leetcode 611. Valid Triangle Number 、259. 3Sum Smaller(lintcode 918. 3Sum Smaller)

这两个题几乎一样,只是说611.ValidTriangleNumber满足大于条件,259.3SumSmaller满足小于条件,两者都是先排序,然后用双指针的方式。 611.ValidTriangleNumber判断这个数组能组成三角形的个数,利用两边之和大于第三边https://www.cnblogs.co...

leetcode 54. Spiral Matrix 、59. Spiral Matrix II

54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置  54.SpiralMatrixstart表示的是每次一圈的开始,每次开始其实就是从(0,0)、(1,1)这种开始的。用endx、endy来表示每次转圈的x、y方向的终止位置,方便后面进行边界条件设置。注意:...
代码星球 代码星球·2020-10-13

leetcode 378. Kth Smallest Element in a Sorted Matrix

这道题求有序矩阵中第K小的元素,数组如下:二分的方法解决,时间复杂度: O(nlgX)。从左下角进行遍历classSolution{public:intkthSmallest(vector<vector<int>>&matrix,intk){intleft=matrix[0][...

leetcode 557. Reverse Words in a String III 、151. Reverse Words in a String

557.ReverseWordsinaStringIII最简单的把空白之间的词反转classSolution{public:stringreverseWords(strings){vector<int>blank;for(inti=0;i<s.size();i++){if(s[i]=='')blank...

leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String

344.ReverseString最基础的旋转字符串classSolution{public:voidreverseString(vector<char>&s){if(s.empty())return;intstart=0;intend=s.size()-1;while(start<end){...

leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II

74.Searcha2DMatrix整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了。这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,%col获得y坐标classSolution{public:boolsearchMatrix(vector<vector<...

5. Longest Palindromic Substring

https://www.cnblogs.com/grandyang/p/4464476.html用动态规划做classSolution{public:stringlongestPalindrome(strings){if(s.empty())return"";intdp[s.size()][s.size()]={0};...
代码星球 代码星球·2020-10-13

https://leetcode.com/problems/palindromic-substrings/description/

https://www.cnblogs.com/grandyang/p/7404777.html博客中写的<=2,实际上<=1也是可以的相当于判断一个大指针内所有子字符串是否可能为回文classSolution{public:intcountSubstrings(strings){intlength=s.s...

394. Decode String

  https://www.cnblogs.com/grandyang/p/5849037.htmlclassSolution{public:stringdecodeString(strings){stringres="";intlength=s.size();stack<int>num...
代码星球 代码星球·2020-10-13
首页上一页...292293294295296...下一页尾页