随笔分类 - LeetCode题解
摘要:(按行枚举) O(n!)说明:对角线dg[u + i],反对角线udg[n - u + i]中的下标表示的是截距(u, i)即(x, y)对角线y = x + b, 截距b = y - x(因为我们要把b当做数组下标,所以b不能是负的,所以我们+n,保证是结果是正的) 反对角线y = -x + b,
阅读全文
摘要:1 class Solution 2 { 3 double solve(double x, int n) //其中n为非负数 4 { 5 if(n == 0) return 1; 6 double res = 1; 7 for(int i = 1;i <= n;i ++) res *= x; 8 r
阅读全文
摘要:1 class Solution 2 { 3 public: 4 vector<vector<string>> groupAnagrams(vector<string>& strs) 5 { 6 vector<vector<string>> res; 7 unordered_map<string,v
阅读全文
摘要:1 //1、先转置 2 //2、第一列与最后一列交换、第二列与倒数第二列交换、... 3 class Solution 4 { 5 public: 6 void rotate(vector<vector<int>>& matrix) 7 { 8 int n = matrix.size(); 9 fo
阅读全文
摘要:1 class Solution 2 { 3 vector<vector<int>> ans; 4 vector<int> temp; 5 vector<bool> st; 6 public: 7 vector<vector<int>> permuteUnique(vector<int>& nums
阅读全文
摘要:1 class Solution 2 { 3 vector<vector<int>> ans; 4 vector<int> temp; 5 vector<bool> st; 6 public: 7 vector<vector<int>> permute(vector<int>& nums) 8 {
阅读全文
摘要:1 //"12" * "34" 2 // 1 2 3 // *3 4 4 // ———————— 5 // 4 8 6 // 3 6 7 // ———————— 8 // 4 0 8 9 // ***注意第二个字符串在第一层循环*** 10 11 class Solution 12 { 13 pub
阅读全文
摘要:1 //递归+记忆化 2 class Solution 3 { 4 vector<int> memo; 5 public: 6 int combinationSum4(vector<int>& nums, int target) 7 { 8 //memo数组一般取target + 1 9 memo
阅读全文
摘要:1 class Solution 2 { 3 vector<vector<int>> res; 4 int sum = 0; 5 public: 6 void helper(vector<int>& nums, int start,int target, vector<int>& out,int k
阅读全文
摘要:1 class Solution 2 { 3 vector<vector<int>> res; 4 int sum = 0; 5 public: 6 void helper(vector<int>& nums, int start,int target, vector<int>& out) 7 {
阅读全文
摘要:1 class Solution 2 { 3 vector<vector<int>> res; 4 int sum = 0; 5 public: 6 void helper(vector<int>& nums, int start,int target, vector<int>& out) 7 {
阅读全文
摘要:比如:111221 1、找出相同子串:str[i + 1] == str[i] 2、在每相同子串的最后一位进行记录与更新 1 class Solution 2 { 3 public: 4 string countAndSay(int n) 5 { 6 string str = "1"; 7 whil
阅读全文
摘要:1 class Solution 2 { 3 int row[9][9] = {0};//某一行的某个数 4 int col[9][9] = {0};//某一列的某个数 5 int cell[3][3][9] = {0};//某一个九宫格中的某个数 6 public: 7 bool isValidS
阅读全文
摘要:1 //参考全排列模板 2 class Solution 3 { 4 int row[9][9] = {0};//某一行的某个数 5 int col[9][9] = {0};//某一列的某个数 6 int cell[3][3][9] = {0};//某一个九宫格中的某个数 7 public: 8 v
阅读全文
摘要:1 class Solution 2 { 3 public: 4 int searchInsert(vector<int>& nums, int target) 5 { 6 int n = nums.size(); 7 int l = 0; 8 int r = n - 1; 9 if(target
阅读全文
摘要:1 class Solution 2 { 3 public: 4 vector<int> searchRange(vector<int>& nums, int target) 5 { 6 auto it = find(nums.begin(),nums.end(),target); 7 if(it
阅读全文
摘要:一、如果左边有序 1、target小于中点的值且小于起点的值:l = mid + 1 2、target小于中点的值且大于等于起点的值:r = mid - 1 3、target大于中点的值:l = mid + 1 二、如果右边有序 1、target大于中点的值且小于等于终点的值:l = mid + 1
阅读全文
摘要:next_permutation的实现方法: 1、从数组的末尾往前找,找到 第一个位置 j,使得 nums[j] < nums[j + 1]。2、如果不存在这样的 j,将数组逆转。 //情况13、如果存在这样的 j,则从末尾往前找,找到第一个位置 i > j,使得 nums[i] > nums[j]
阅读全文
摘要:1 class Solution 2 { 3 public: 4 5 int divide(int dividend, int divisor) 6 { 7 if(dividend == 0) return 0; 8 9 long long a = fabs(dividend);//fabs()函数
阅读全文
摘要:1 //KMP算法 2 class Solution 3 { 4 public: 5 //求next数组 6 vector<int> pre(string& str2) 7 { 8 if(str2.size() == 1) return {-1}; 9 vector<int> next(str2.s
阅读全文

浙公网安备 33010602011771号