随笔分类 - LeetCode
摘要:1 #include "000库函数.h" 2 3 4 //使用排序,感觉在作弊,但是题目没说不可以 5 //36ms 6 class Solution { 7 public: 8 vector> permuteUnique(vector& nums) { 9 vector>res; 10 if (nums.emp...
阅读全文
摘要:1 #include "000库函数.h" 2 3 4 //使用排序,感觉在作弊,但是题目没说不可以 5 //24ms 6 class solution { 7 public: 8 vector> permute(vector& nums) { 9 vector>res; 10 if (nums.empty())r...
阅读全文
摘要:1 #include "000库函数.h" 2 3 4 //考虑当前最远能到什么地方,例如2, 3, 1, 1, 4, 5 //首先只考虑a[0] = 2,即最远可以到a[2],然后从1到2中找下一个可到的最远点, 6 //即a[1]可以到达a[4],此时找到结果,步数记录为2。若接着考虑, 7 //下一次应该从3 - 4里面找一个最远即a[4]可达a[8](4 + 4), ...
阅读全文
摘要://此为博客讲解//p串中星号的位置很重要,用jStar来表示,还有星号匹配到s串中的位置,//使用iStart来表示,这里 iStar 和 jStar 均初始化为 - 1,表示默认情况下是没有星号的。//然后再用两个变量i和j分别指向当前s串和p串中遍历到的位置。////开始进行匹配,若i小于s串
阅读全文
摘要:1 #include"000库函数.h" 2 //一点头绪都没有 3 //然后就自己按自己的意思来一遍 4 //好像没有用算法 5 //16ms,让我激动一把 6 7 class Solution { 8 public: 9 int trap(vector& height) { 10 if (height.size() = s) {...
阅读全文
摘要:1 #include "000库函数.h" 2 3 4 //先排序,然后找值 5 //自己的解法 12ms 6 //class Solution { 7 //public: 8 // int firstMissingPositive(vector& nums) { 9 // sort(nums.rbegin(),nums.rend());//对数组进行逆...
阅读全文
摘要:1 #include "000库函数.h" 2 3 //第一感觉使用回溯比较快 4 //96ms 5 6 7 class Solution { 8 public: 9 vector> combinationSum2(vector& candidates, int target) { 10 vector>R; 11 sort(cand...
阅读全文
摘要:1 #include "000库函数.h" 2 3 //第一感觉使用回溯比较快 4 //好激动,第一次使用回溯成功 96ms,37.1M 5 6 7 class Solution { 8 public: 9 vector> combinationSum(vector& candidates, int target) { 10 v...
阅读全文
摘要:1 #include "000库函数.h" 2 3 4 //自解,就遍历数数 8ms 5 class Solution { 6 public: 7 string countAndSay(int n) { 8 if (n == 0)return ""; 9 string str = "1"; 10 string s; ...
阅读全文
摘要://跟此题类似的有 Permutations 全排列,Combinations 组合项, N - Queens N皇后问题等等,//其中尤其是跟 N - Queens N皇后问题的解题思路及其相似,对于每个需要填数字的格子带入1到9,//每代入一个数字都判定其是否合法,如果合法就继续下一次递归,结束
阅读全文
摘要:1 #include "000库函数.h" 2 3 //一看,没想出什么好法子,就遍历了 4 //最重要的是如何比较小九宫格的数据 5 //44ms 6 class Solution { 7 public: 8 bool isValidSudoku(vector>& board) { 9 for (int i = 0; i j; --k) 13 ...
阅读全文
摘要:1 #include "000库函数.h" 2 3 //第一眼,感觉没什么考虑算法的,就普通做就好了 4 //又因为是有序 的,故使用二分法最好了【别再太真爱用遍历,傻子才会一上来就遍历】 12ms 5 class Solution { 6 public: 7 int searchInsert(vector& nums, int target) { 8 ...
阅读全文
摘要:1 #include "000库函数.h" 2 3 //使用二分法查找到目标值的位置,然后分两边再查找出起始位置和终止位置 4 //16ms 不是严格意义上的logn的复杂度 5 class Solution { 6 public: 7 vector searchRange(vector& nums, int target) { 8 vectorv =...
阅读全文
摘要:1 #include "000库函数.h" 2 3 //自解 68ms 弱爆了 4 //先找到最大值,然后确定目标值在最大值的左边还是右边 5 //最后使用折中寻找法,找到目标值的位置,此方法应该满足复杂度不超过logn的要求 6 class Solution { 7 public: 8 int search(vector& nums, int ta...
阅读全文
摘要:题目描述: 给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度。 示例 1: 示例 2: 这里我们还是借助栈来求解,需要定义个start变量来记录合法括号串的起始位置,我们遍历字符串,如果遇到左括号,则将当前下标压入栈,如果遇到右括号,如果当前栈为空,则将下一个坐标位置
阅读全文
摘要:自解法 20ms使用一遍法,从右向左依次比较相邻两个数字,看其是否为降序排列[降序排列无更大的排列了]当出现升序排列时,只需要将此处的数字与其右端比其大一点点的数进行交换,再将【i-1】右端的数进行升序排列就得到比目前大一点的排列了。如图所示 //使用C++的排列函数next_permutation
阅读全文
摘要:1 #include "000库函数.h" 2 3 //自解; 4 //这道题 的突破口就是找到words的组合情况 5 //然后将所有组合一一查找是否存在子串,还要对答案去重、查找相同子串不同位置出现!!! 6 //超出时间限制^_^,悲催,做了一个小时 7 class Solution { 8 public: 9 vector findSubst...
阅读全文
摘要:1 #pragma once 2 #include "000库函数.h" 3 4 /*********************自解**************/ 5 //使用算法中的find 12ms 6 class Solution { 7 public: 8 int strStr(string haystack, string needle) { 9 ...
阅读全文
摘要:1 #pragma once 2 #include "000库函数.h" 3 /******************自解***********************/ 4 //虽然很快通过,但不满足题中不允许使用除法的要求 36ms 5 class Solution { 6 public: 7 int divide(int dividend, int d...
阅读全文