03 2020 档案
摘要:C++。思路如下: 对于连续整数序列[left, right]中的一点i,若要生成以i为根节点的BST,则有如下规律: i左边的序列可以作为左子树结点,且左儿子可能有多个,所以有vector<TreeNode *> left_nodes = generate(left, i - 1);; i右边的序
阅读全文
摘要:1 class Solution 2 { 3 vector<string> res; 4 public: 5 vector<string> restoreIpAddresses(string s) 6 { 7 string ip;//满足条件的IP地址 8 helper(s, 0, ip); 9 r
阅读全文
摘要:1 //套模板 2 class Solution 3 { 4 vector<vector<int>> res; 5 void helper(vector<int>& nums, int start, vector<int>& out) 6 { 7 res.push_back(out); 8 for
阅读全文
摘要:1 //0010——>00010——>0011 2 //00——>0异或 3 //00——>0 4 //01——>1 5 //10——>1 6 class Solution 7 { 8 public: 9 vector<int> grayCode(int n) 10 { 11 if(n == 0)
阅读全文
摘要:1 //从nums1后往前依次放置 2 class Solution 3 { 4 public: 5 void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) 6 { 7 int len = m + n - 1; 8 int i
阅读全文
摘要:1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * };
阅读全文
摘要:1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * };
阅读全文
摘要:1 class Solution 2 { 3 public: 4 bool search(vector<int>& nums, int target) 5 { 6 int l = 0, r = nums.size()-1; 7 while(l <= r) 8 { 9 //处理重复数字 10 whil
阅读全文
摘要:1 class Solution 2 { 3 public: 4 int removeDuplicates(vector<int>& nums) 5 { 6 int j = 0; 7 for(int i = 0;i < nums.size();i ++) 8 { 9 if(j < 2 || nums
阅读全文
摘要:1 //删除重复数模板 2 class Solution 3 { 4 public: 5 int removeDuplicates(vector<int>& nums) 6 { 7 int i = 0; 8 for(int j = 0;j < nums.size();j ++) 9 if(i < k
阅读全文
摘要:1 class Solution 2 { 3 public: 4 int dir[4][4]={{-1,0},{1,0},{0,-1},{0,1}}; 5 6 bool exist(vector<vector<char>>& board, string word) 7 { 8 int m=board
阅读全文
摘要:1 //直接套模板 2 class Solution 3 { 4 vector<vector<int>> res; 5 void helper(vector<int>& nums, int start, vector<int>& out) 6 { 7 res.push_back(out); 8 9
阅读全文
摘要:1 //直接套模板 2 class Solution 3 { 4 vector<vector<int>> res; 5 void helper(vector<int>& nums, int start, vector<int>& out,int k) 6 { 7 if(k == out.size()
阅读全文
摘要:1 class Solution 2 { 3 public: 4 void sortColors(vector<int>& nums) 5 { 6 int n = nums.size(); 7 int begin = -1; 8 int end = n; 9 int i = 0; 10 while(
阅读全文
摘要:1 void partition(int arr[], int l, int r, int num)//三路快排 2 { 3 int less = l - 1; 4 int more = r + 1; 5 int cur = 0; 6 while (cur < more) 7 { 8 if (arr
阅读全文
摘要:1 class Solution 2 { 3 int dx[4] = {1,0,-1,0}; 4 int dy[4] = {0,1,0,-1}; 5 public: 6 void setZeroes(vector<vector<int>>& matrix) 7 { 8 int m = matrix.
阅读全文
摘要:1 // "/a//b////c/d//././/.." 2 class Solution 3 { 4 vector<string> res; 5 void spilt(string s,char c,vector<string> &res) 6 { 7 istringstream iss(s);
阅读全文
摘要:1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x),
阅读全文
摘要:1 class Solution 2 { 3 public: 4 int firstMissingPositive(vector<int>& nums) 5 { 6 int minV = INT_MAX; 7 int maxV = INT_MIN; 8 int n = nums.size(); 9
阅读全文
摘要:1 //用到了贪心思想 2 class Solution 3 { 4 public: 5 string small[20] = {"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten", 6 "Eleve
阅读全文
摘要:1 //所有实现最好写在一个类中 2 class Trie 3 { 4 public: 5 bool is_end; //是否以该单词结尾 6 Trie* son[26]; //该节点儿子的个数 7 Trie() 8 { 9 is_end = false; 10 for(int i = 0;i <
阅读全文
摘要:1 //1、除了小写字母,这些电子邮件还可能包含 '.' 或 '+' 2 //2、每封 emails[i] 都包含有且仅有一个 '@' 字符 3 //3、只考虑本地名称,即'@'字符以前的 4 class Solution 5 { 6 public: 7 int numUniqueEmails(ve
阅读全文
摘要:1 class Solution 2 { 3 vector<int> res; 4 void spilt(string s,char c,vector<int> &res) 5 { 6 istringstream iss(s); 7 string temp; 8 while(getline(iss,
阅读全文
摘要:1 class Solution 2 { 3 //spilt函数的实现 4 //假设以空格切分:char c = ' '; 5 vector<string> res; 6 void spilt(string s,char c,vector<string> &res) 7 { 8 istringstr
阅读全文
摘要:1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x),
阅读全文
摘要:1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x),
阅读全文
摘要:1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x),
阅读全文
摘要:1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x),
阅读全文
摘要:1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x),
阅读全文
摘要:1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x),
阅读全文
摘要:1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x),
阅读全文
摘要:1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x),
阅读全文
摘要:1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x),
阅读全文
摘要:1 //前序 2 void preOrderUnRecur(ListNode* head) 3 { 4 cout << "pre-order: "; 5 if (head != NULL) 6 { 7 stack<ListNode*> Stack; 8 Stack.push(head); 9 10
阅读全文
摘要:1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x),
阅读全文
摘要:1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * };
阅读全文
摘要:1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * };
阅读全文
摘要:1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * };
阅读全文
摘要:1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * };
阅读全文
摘要:1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * };
阅读全文
摘要:1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * };
阅读全文
摘要:1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * };
阅读全文
摘要:1 class Solution 2 { 3 public: 4 int hIndex(vector<int>& nums) 5 { 6 int l = 0,r = nums.size(); 7 while(l < r) 8 { 9 int mid = l + r + 1 >> 1; 10 11 /
阅读全文
摘要:1 class Solution 2 { 3 public: 4 int findPeakElement(vector<int>& nums) 5 { 6 int l = 0,r = nums.size() - 1; 7 while(l < r) 8 { 9 int mid = l + r >> 1
阅读全文
摘要:1 // Forward declaration of isBadVersion API. 2 bool isBadVersion(int version); 3 4 class Solution 5 { 6 public: 7 int firstBadVersion(int n) 8 { 9 in
阅读全文
摘要:1 //利用模板 2 3 //以最后一个值来划分 4 //如果比它小,在右边,更新r 5 //反之,更新l 6 class Solution 7 { 8 public: 9 int findMin(vector<int>& nums) 10 { 11 int l = 0,r = nums.size(
阅读全文
摘要:1 class Solution 2 { 3 public: 4 bool searchMatrix(vector<vector<int>>& matrix, int target) 5 { 6 if(matrix.empty() || matrix[0].empty()) return false
阅读全文
摘要:1 使用数组中的值作为索引下标进行遍历,遍历的结果肯定是一个环(有一个重复元素) 2 检测重复元素问题转换成检测环的入口 3 为了找到环的入口,可以进行如下步骤: 4 5 设置两个快慢指针, fast每次走两步,slow每次走一步,最终走了slow走了n步与fast相遇,fast走了2*n,fast
阅读全文
摘要:1 class Solution 2 { 3 public: 4 int climbStairs(int n) 5 { 6 vector<int> nums(n + 10); 7 nums[1] = 1; 8 nums[2] = 2; 9 for(int i = 3;i <= n;i ++) num
阅读全文
摘要:1 class Solution 2 { 3 public: 4 int mySqrt(int x) 5 { 6 double l = 0; 7 double r = x; 8 9 while(r - l > 1e-6) 10 { 11 double mid = (l + r)/2; 12 if(m
阅读全文
摘要:1 class Solution 2 { 3 public: 4 string addBinary(string a, string b) 5 { 6 if(a.size() < b.size()) swap(a,b); 7 int C = 0;//进位 8 int m = a.size(),n =
阅读全文
摘要:1 class Solution 2 { 3 public: 4 vector<int> plusOne(vector<int>& digits) 5 { 6 int n = digits.size(); 7 int C = 1;//进位 8 for(int i = n - 1;i >= 0;i -
阅读全文
摘要:1 class Solution 2 { 3 public: 4 int minPathSum(vector<vector<int>>& grid) 5 { 6 int m = grid.size(); 7 int n = grid[0].size(); 8 vector<vector<int>>
阅读全文
摘要:STL中next_permutation:下一个排列 prev_permutation:上一个排列 1 class Solution 2 { 3 public: 4 string getPermutation(int n, int k) 5 { 6 string str; 7 for(int i =
阅读全文
摘要:1 class Solution 2 { 3 vector<vector<int>> ans; 4 int a; 5 public: 6 vector<vector<int>> generateMatrix(int n) 7 { 8 a = 1; 9 ans = vector<vector<int>
阅读全文
摘要:1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * };
阅读全文
摘要:1 class Solution 2 { 3 public: 4 int uniquePathsWithObstacles(vector<vector<int>>& nums) 5 { 6 int m = nums.size(); 7 int n = nums[0].size(); 8 9 //起点
阅读全文
摘要:1 class Solution 2 { 3 public: 4 int uniquePaths(int m, int n) 5 { 6 vector<vector<int>> dp(m,vector<int>(n,1)); 7 for(int i = 1;i < m;i ++) 8 { 9 for
阅读全文
摘要:1 class Solution 2 { 3 void spilt(string s,char c,vector<string> &res) 4 { 5 istringstream iss(s); 6 string temp; 7 while(getline(iss,temp,c)) 8 { 9 /
阅读全文
摘要:1 //spilt函数的实现 2 //假设以空格切分:char c = ' '; 3 class Solution 4 { 5 vector<string> res; 6 void spilt(string s,char c,vector<string> &res) 7 { 8 istringstr
阅读全文
摘要:1 class Solution 2 { 3 public: 4 vector<vector<int>> merge(vector<vector<int>>& nums) 5 { 6 vector<vector<int>> res; 7 if(nums.empty()) return res; 8
阅读全文
摘要:1 class Solution 2 { 3 public: 4 bool canJump(vector<int>& nums) 5 { 6 int n = nums.size(); 7 int max_size = nums[0]; 8 for(int i = 0;i < n;i ++) 9 {
阅读全文
摘要:1 class Solution 2 { 3 vector<int> ans; 4 public: 5 vector<int> spiralOrder(vector<vector<int>>& matrix) 6 { 7 if(matrix.size() == 0) return {}; 8 int
阅读全文
摘要:1 class Solution 2 { 3 public: 4 int longestCommonSubsequence(string text1, string text2) 5 { 6 int m = text1.size(); 7 int n = text2.size(); 8 vector
阅读全文
摘要:1 class Solution 2 { 3 public: 4 int findNumberOfLIS(vector<int>& nums) 5 { 6 if(nums.size() == 0) return 0; 7 int n = nums.size(); 8 vector<int> dp(n
阅读全文
摘要:1 class Solution 2 { 3 public: 4 int lengthOfLIS(vector<int>& nums) 5 { 6 if(nums.size() == 0) return 0; 7 int n = nums.size(); 8 int res = INT_MIN; 9
阅读全文
摘要:1 //看成一个下三角形 2 class Solution 3 { 4 public: 5 int minimumTotal(vector<vector<int>>& triangle) 6 { 7 int n = triangle.size(); 8 int res = INT_MAX; 9 ve
阅读全文
摘要:01背包只不过是 每一组的个数为1,分组背包每一组的个数为s[i] 1 #include<bits/stdc++.h> 2 using namespace std; 3 4 const int N=110; 5 int f[N]; 6 int v[N][N],w[N][N],s[N]; 7 int
阅读全文
摘要:1 class Solution 2 { 3 public: 4 int maxSubArray(vector<int>& nums) 5 { 6 int n = nums.size(); 7 vector<int> dp(n + 5,0); 8 dp[0] = nums[0]; 9 int res
阅读全文
摘要:思路跟51题一模一样 1 class Solution 2 { 3 vector<vector<string>> res; 4 vector<string> temp;//临时路径 5 vector<bool> col,m,s;//列,主对角线,副对角线 6 int n; 7 public: 8 i
阅读全文
摘要:(按行枚举) 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
阅读全文
摘要:有 N 种物品和一个容量是 VV 的背包。 第 i 种物品最多有 si 件,每件体积是 vi,价值是 wi。 求解将哪些物品装入背包,可使物品体积总和不超过背包容量,且价值总和最大。输出最大价值。 输入格式 第一行两个整数N,V,用空格隔开,分别表示物品种数和背包容积。 接下来有 N行,每行三个整数
阅读全文
摘要: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
阅读全文
摘要:有 N 种物品和一个容量是 V 的背包,每种物品都有无限件可用。 第 i 种物品的体积是 vi,价值是 wi。 求解将哪些物品装入背包,可使这些物品的总体积不超过背包容量,且总价值最大。输出最大价值。 输入格式 第一行两个整数N,V,用空格隔开,分别表示物品种数和背包容积。 接下来有 N 行,每行两
阅读全文
摘要:有 N 件物品和一个容量是 V 的背包。每件物品只能使用一次。 第 i 件物品的体积是 vi,价值是 wi。 求解将哪些物品装入背包,可使这些物品的总体积不超过背包容量,且总价值最大。输出最大价值。 输入格式 第一行两个整数N,V,用空格隔开,分别表示物品数量和背包容积。 接下来有 N 行,每行两个
阅读全文
摘要: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、全排列 1 class Solution 2 { 3 public: 4 int n; 5 vector<bool> st; 6 vector<vector<int>> ans; 7 vector<int> path; 8 9 vector<vector<int>> permute(vector
阅读全文
摘要: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
阅读全文
摘要:1 //跟上一题一样的思路 2 class Solution 3 { 4 public: 5 int removeElement(vector<int>& nums, int val) 6 { 7 vector<int> vec; 8 int j = 0; 9 for(int i = 0;i < n
阅读全文
摘要:1 //双指针算法,也就是实现STL中的unique函数 2 class Solution 3 { 4 public: 5 int removeDuplicates(vector<int>& nums) 6 { 7 int n = nums.size(); 8 int j = 0; 9 for(in
阅读全文
摘要:1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * };
阅读全文
摘要:1 //这道题没有思路,借鉴别人的代码 2 3 4 //1、使用递归。 5 //2、每次可以放置左括号的条件是当前左括号的数目不超过 nn。 6 //3、每次可以放置右括号的条件是当前右括号的数目不超过左括号的数目。 7 class Solution 8 { 9 vector<string> res
阅读全文
摘要:1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * };
阅读全文
摘要:1 //思路很清晰,直接用stack 2 class Solution 3 { 4 unordered_map<char,char> hash = {{'(',')'},{'[',']'},{'{','}'}}; 5 public: 6 bool isValid(string s) 7 { 8 st
阅读全文
摘要:1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * };
阅读全文
摘要:1 //跟三数之和思想一样的,只不过多一层循环 2 class Solution 3 { 4 public: 5 vector<vector<int>> fourSum(vector<int>& nums, int target) 6 { 7 vector<vector<int>> res; 8 i
阅读全文
摘要:1 //DFS问题一直很难 2 class Solution 3 { 4 void dfs(string digits,vector<vector<char>>& d,vector<string> &res,int cur,string& temp) 5 { 6 if(cur == digits.s
阅读全文
摘要:1 //搞清楚各个变量的含义 2 //***忘记对数组进行排序,以至于一直卡在这里*** 3 class Solution 4 { 5 public: 6 int threeSumClosest(vector<int>& nums, int target) 7 { 8 int n = nums.si
阅读全文
摘要:1 // 312/313 始终会出现一种情况——>全0 2 3 // 后来引进了双指针算法 4 class Solution 5 { 6 public: 7 vector<vector<int>> threeSum(vector<int>& nums) 8 { 9 vector<vector<int
阅读全文
摘要:1 class Solution 2 { 3 public: 4 string longestCommonPrefix(vector<string>& strs) 5 { 6 string res; 7 if(strs.empty()) return res; 8 int min_length =
阅读全文
摘要:1 class Solution 2 { 3 private: 4 unordered_map<string,int> hash = {{"I",1},{"IV",4},{"V",5},{"IX",9},{"X",10}, 5 {"XL",40},{"L",50},{"XC",90},{"C",10
阅读全文
摘要:1 //用到了贪心思想 2 class Solution 3 { 4 public: 5 int a[13] = {1,4,5,9,10,40,50,90,100,400,500,900,1000}; 6 string b[13] = {"I","IV","V","IX","X","XL","L",
阅读全文
摘要:1 //双指针算法 2 class Solution 3 { 4 public: 5 int maxArea(vector<int>& height) 6 { 7 int n = height.size(); 8 int Area = 0; 9 int l = 0,r = n - 1; 10 whi
阅读全文
摘要:1 class Solution 2 { 3 public: 4 bool isPalindrome(int x) 5 { 6 if(x < 0) return false; 7 vector<int> nums; 8 while(x) 9 { 10 nums.push_back(x % 10);
阅读全文
摘要:1 class Solution 2 { 3 public: 4 int myAtoi(string str) 5 { 6 long long res = 0; 7 bool flag = true; 8 bool jisuan = false; 9 for(int i = 0;i < str.si
阅读全文
摘要:1 //注意x = abs(x) x->[-2147483648,2147483647] 2 class Solution 3 { 4 public: 5 int reverse(int x) 6 { 7 bool f = false; 8 long long res = 0; 9 if(x < 0
阅读全文
摘要:1 //找规律题 等差数列 2m-2 2 class Solution 3 { 4 public: 5 string convert(string s, int m) 6 { 7 if(m == 1) return s; 8 int n = s.size(); 9 string res; 10 fo
阅读全文
摘要:1 //左右同时走,如果相等,更新最大值 2 class Solution 3 { 4 public: 5 string longestPalindrome(string s) 6 { 7 int res = 0; 8 string str; 9 for(int i = 0;i < s.size()
阅读全文
摘要:1 //双指针算法模板+哈希表 2 class Solution 3 { 4 public: 5 int lengthOfLongestSubstring(string s) 6 { 7 unordered_map<char,int> nums; 8 int res = 0; 9 for(int i
阅读全文
摘要:1 class Solution 2 { 3 public: 4 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) 5 { 6 ListNode* dummy = new ListNode(-1); 7 ListNode* pre = dummy
阅读全文
摘要:https://leetcode-cn.com/problems/two-sum/ 1 //哈希表 2 class Solution 3 { 4 public: 5 vector<int> twoSum(vector<int>& nums, int target) 6 { 7 unordered_m
阅读全文
摘要:求解next数组 下标 0 1 2 3 4 a b a b c next -1 0 0 1 2 求解next数组时,下标为0,1已知,从下标为2开始 1 //KMP算法 2 class Solution 3 { 4 public: 5 //求next数组 6 vector<int> pre(stri
阅读全文

浙公网安备 33010602011771号