07 2021 档案
摘要:这题作者的想法太简洁了,涉及好多细节,一般人想不到。 class Solution { public: string decodeString(string s) { stack<pair<int, string>> st; int num = 0; string res = ""; for(int
阅读全文
摘要:class Solution { public: static int cmp(vector<int> a, vector<int> b){ if(a[0] != b[0]) // 第一个元素不相等,按照从大到小排序, return a[0]>b[0]; else // 第一个元素相等,第二个元素按
阅读全文
摘要:class Solution { public: bool canPartition(vector<int>& nums) { int n = nums.size(); if(n<=1) // 数组长度小于等于1返回false return false; int sum = 0; for(int i
阅读全文
摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), rig
阅读全文
摘要:class Solution { public: bool check(int s[], int p[]) { for (int i = 0; i < 26; i++) { if (s[i] != p[i]) { return false; } } return true; } vector<int
阅读全文
摘要:指针是c和c++一个十分重要的概念,一个数据对象的内存地址称为该数据对象的指针。指针可以表示各种数据对象,允许直接获取和操作数据地址,实现动态存储分配。 面试问题一: 指针和引用的区别 (1)非空区别。在任何情况下都不能使用指向控制的引用。一个引用必须总是指向某些对象。 (2)合法性区别。在使用引用
阅读全文
摘要:函数的调用,想必大家都用过,一个函数在被另一个函数调用的时候,才有生命,才会为其准备对应的内存空间,再调用完毕之后再清理释放结束。 可以看到,每一次的函数调用都会带来一些时间和空间上的花销。 而自定义函数的一个作用,也是为了提高代码的重用性,可以在需要的时候随时调用,提高开发效率。那么,一个代码本身
阅读全文
摘要:class Solution { public: int res = 0; int findTargetSumWays(vector<int>& nums, int target) { if(nums.size()==0) return 0; dfs(nums,0,0,target); return
阅读全文
摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), rig
阅读全文
摘要:解析参考: https://leetcode-cn.com/problems/task-scheduler/solution/tong-zi-by-popopop/ class Solution { public: /* 结合桶的思想,我们只需要计算两个数,第一:一个是找出最大任务数量 N, 看一下
阅读全文
摘要:class Solution { public: int hammingDistance(int x, int y) { int res = x^y; // 异或不同为结果为1 unsigned int flag = 1; int count = 0; while(flag){ if(flag&re
阅读全文
摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), rig
阅读全文
摘要:前缀和与哈希表 // class Solution { // public: // int subarraySum(vector<int>& nums, int k) { // // 前缀和 // // 超时 // vector<int> pre(nums.size()+1,0); // pre[0
阅读全文
摘要:class Solution { public: int findUnsortedSubarray(vector<int>& nums) { // 笨方法,先排序,再从两端进行比较 int i = 0; int j = nums.size()-1; vector<int> res(nums.size
阅读全文
摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), rig
阅读全文
摘要:class Solution { public: int countSubstrings(string s) { // 中心扩展法 // 注意回文中心可能是一个,也可能是两个 // 如果aba 有中心a向两边扩展, abba右bb向两边分别扩展得到。 int res = 0; for(int i =
阅读全文
摘要:// class Solution { // public: // vector<int> dailyTemperatures(vector<int>& temperatures) { // vector<int> dp(temperatures.size(), 0); // // 这个是求最长子序
阅读全文
摘要:class Solution { public: vector<int> countBits(int n) { vector<int> res; int temp = 0; for(int i = 0; i <=n;i++){ temp = countone(i); res.push_back(te
阅读全文
摘要:class Solution { public: int lengthOfLIS(vector<int>& nums) { vector<int> dp(nums.size(), 1); // dp[0] = 1; // dp[i] 0~i范围内的最长递增子序列。 for(int i = 1; i
阅读全文
摘要:class Solution { public: int findDuplicate(vector<int>& nums) { unordered_map<int, int> in_map; for(int i =0; i < nums.size(); i++){ in_map[nums[i]]++
阅读全文
摘要:class Solution { public: /* 这里我们可以用第一个0当做这个中间点, 把不等于0(注意题目没说不能有负数)的放到中间点的左边,等于0的放到其右边。 */ void moveZeroes(vector<int>& nums) { int j = 0; // j 指向第一个0
阅读全文
摘要:class Solution { public: int numSquares(int n) { vector<int> dp(n+1,0); for(int i = 1; i <= n;i++){ dp[i] = i; //最差情况,有i个1组成, for(int j = 1; i-j*j>=0;
阅读全文
摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), r
阅读全文
摘要:class Solution { public: bool searchMatrix(vector<vector<int>>& matrix, int target) { int i = matrix.size()-1; int j = 0; while(i>=0&&j<matrix[0].size
阅读全文
摘要:class Solution { public: vector<int> maxSlidingWindow(vector<int>& nums, int k) { priority_queue<pair<int ,int>> q; vector<int> res; //规则:pair的比较,先比较第
阅读全文
摘要:class Solution { public: vector<int> productExceptSelf(vector<int>& nums) { if(nums.size()==0) return {}; vector<int> a(nums.size(),1); vector<int> b(
阅读全文
摘要:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) :
阅读全文
摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), rig
阅读全文
摘要:class Solution { public: int findKthLargest(vector<int>& nums, int k) { priority_queue<int, vector<int>, less<int>> maxheap; for(int i = 0; i < nums.s
阅读全文
摘要:class Solution { public: int maximalSquare(vector<vector<char>>& matrix) { int m = matrix.size(); int n = matrix[0].size(); if(m==0) return 0; vector<
阅读全文
摘要:class Solution { public: bool canFinish(int numCourses, vector<vector<int>>& prerequisites) { vector<vector<int>> edge; vector<int> index; edge.resize
阅读全文
摘要:class Solution { public: int numIslands(vector<vector<char>>& grid) { int row = grid.size(); int col = grid[0].size(); if(row==0) return 0; int count
阅读全文
摘要:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) :
阅读全文
摘要:class Solution { public: int rob(vector<int>& nums) { if(nums.size()==0) return 0; if(nums.size()==1) return nums[0]; // dp[i]表示前i间房屋能偷窃到的最高总金额 vector
阅读全文
摘要:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solu
阅读全文
摘要:class MinStack { public: /** initialize your data structure here. */ MinStack() { } stack<int> s; stack<int> mins; void push(int val) { if(mins.empty(
阅读全文
摘要:标签:动态规划 遍历数组时计算当前最大值,不断更新 令imax为当前最大值,则当前最大值为 imax = max(imax * nums[i], nums[i]) 由于存在负数,那么会导致最大的变最小的,最小的变最大的。因此还需要维护当前最小值imin,imin = min(imin * nums[
阅读全文
摘要:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) :
阅读全文
摘要:// /** // * Definition for singly-linked list. // * struct ListNode { // * int val; // * ListNode *next; // * ListNode(int x) : val(x), next(NULL) {}
阅读全文
摘要:class Solution { public: int singleNumber(vector<int>& nums) { // 俩个相同的数异或结果为0, // 任何数与0异或结果为本身 int res = 0; for(int i = 0; i < nums.size(); i++){ res
阅读全文
摘要:class Solution { public: int longestConsecutive(vector<int>& nums) { if(nums.size()==0) return 0; if(nums.size()==1) return 1; sort(nums.begin(), nums
阅读全文
摘要:解析参考: https://leetcode-cn.com/problems/binary-tree-maximum-path-sum/solution/er-cha-shu-zhong-de-zui-da-lu-jing-he-by-leetcode-/ /** * Definition for
阅读全文
摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), rig
阅读全文
摘要:方法一: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr)
阅读全文
摘要:memcpy(a,b,c) 将从b开始的c个长度的数据拷贝到a开始的内存里面
阅读全文
摘要:
阅读全文
摘要:开源许可证就是对用户的权利进行声明与限制。 参考:周明辉 北大计算机系教授《开源软件的历史、现状和发展》
阅读全文
摘要:直接使用以下命令老师抱超时报错: pip install torchvision 于是在官网搜索安装命令: 官网地址: https://pytorch.org/get-started/previous-versions/ conda install pytorch==1.7.0 torchvisio
阅读全文
摘要:https://www.nowcoder.com/question/next?pid=27976983&qid=235785&tid=45624435 #include <iostream> using namespace std; int main(){ int sum=0; int temp;
阅读全文
摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), rig
阅读全文
摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), rig
阅读全文
摘要:要找出所有二叉搜索树,可用动态规划的思想 以1 ... n 为节点组成的二叉搜索树,不同的树在于根结点的不同和左右子树的不同 根结点不同,可能有n种情况,以1为根结点,以2为根结点...类推到以n为根结点,共有n种情况,区分了根节点不同后,剩下的 就是左右子树不同了,而左右子树的不同二叉树则是一个相
阅读全文
摘要:下面的dfs在剑指offer上没毛病,在leetcode上超时 class Solution { public: bool exist(vector<vector<char>>& board, string word) { int m = board.size(); int n = board[0]
阅读全文
摘要:此题dfs用的出神入化 class Solution { public: vector<vector<int>> res; vector<int> temp; vector<vector<int>> subsets(vector<int>& nums) { dfs(nums,0); return r
阅读全文
摘要:求解方法 知道了编辑距离的定义,那么如何求最小编辑距离呢?这里用到了动态规划的思想。 用例子来说明,假如我们要求解 jary和jerry的最小编辑距离,那么首先要创建如下矩阵: Φ j a r y Φ 0 1 2 3 4 j 1 e 2 r 3 r 4 y 5 这个矩阵什么意思呢?第一行是字符串ja
阅读全文
摘要:和62题不同路径一样,使用dfs方法同样超时。 class Solution { public: vector<int> res; int minPathSum(vector<vector<int>>& grid) { int m = grid.size(); int n = grid[0].siz
阅读全文
摘要:第一种方法 DFS 果然超时 class Solution { public: int count = 0; int uniquePaths(int m, int n) { vector<vector<bool>> visited(m, vector<bool>(n,false)); path(vi
阅读全文
摘要:算法解析: class Solution { public: vector<vector<int>> merge(vector<vector<int>>& intervals) { // 此题最关键的就是排序,如果不排序很难处理 sort(intervals.begin(), intervals.e
阅读全文
摘要:第一种方式超时 class Solution { public: bool canJump(vector<int>& nums) { if(jump(nums,0)) return true; return false; } bool jump(vector<int> nums,int index)
阅读全文
摘要:class Solution { public: int climbStairs(int n) { vector<int> dp(n+1,0); if(n<2) return n; dp[0] = 0; dp[1] = 1; dp[2] = 2; // dp[i] 定义:到达当前i阶有dp[i]种不
阅读全文
摘要:直接想到动态规划 class Solution { public: int maxSubArray(vector<int>& nums) { vector<int> dp(nums.size(),0); dp[0] = nums[0]; for(int i = 1; i < nums.size();
阅读全文
摘要:class Solution { public: static bool cmp(string a, string b){ int sum_a = 0; int sum_b = 0; sort(a.begin(), a.end()); sort(b.begin(), b.end()); return
阅读全文
摘要:class Solution { public: void rotate(vector<vector<int>>& matrix) { int len = matrix.size(); // 先转化为对成矩阵 for(int i = 0; i < len;i++) for(int j = 0; j<
阅读全文
摘要:39题组合总数也可以用这种模式 class Solution { public: vector<vector<int>> res; vector<vector<int>> permute(vector<int>& nums) { if(nums.size()==0) return res; vect
阅读全文
摘要:解析参见: https://leetcode-cn.com/problems/trapping-rain-water/solution/jie-yu-shui-by-leetcode/327718/ class Solution { public: int trap(vector<int>& hei
阅读全文
摘要:看到此题回想到用回溯法枚举可能的情况。 class Solution { public: vector<vector<int>> res; vector<vector<int>> combinationSum(vector<int>& candidates, int target) { if(can
阅读全文
摘要:class Solution { public: vector<int> searchRange(vector<int>& nums, int target) { vector<int> res; int left = lcoateleft(nums,0,nums.size()-1,target);
阅读全文
摘要:class Solution { public: int search(vector<int>& nums, int target) { int len = nums.size(); // if(len<2) // return 0; int left = 0; int right = len-1;
阅读全文
摘要:ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 直接使用呢上面命令可能会报403错误 所以推荐使用国内镜像源 /bin/zsh -c "$(curl -fsSL htt
阅读全文
摘要:class Solution { public: void nextPermutation(vector<int>& nums) { int len = nums.size(); for(int i = len-1; i > 0 ; i--){ if(nums[i]> nums[i-1]){ sor
阅读全文
摘要:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) :
阅读全文
摘要:class Solution { public: vector<string> res; vector<string> generateParenthesis(int n) { if(n==0) return res; back("", n, n); return res; } void back(
阅读全文
摘要:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) :
阅读全文
摘要:class Solution { public: bool isValid(string s) { int len = s.length(); if(len==0) return true; vector<char> res; if(s[0]==')'||s[0]==']'||s[0]=='}')
阅读全文
摘要:class Solution { public: vector<string> res; unordered_map<char,string> map; string str = ""; vector<string> letterCombinations(string digits) { int l
阅读全文
摘要:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) :
阅读全文
摘要:class Solution { public: vector<vector<int>> threeSum(vector<int>& nums) { vector<vector<int>> res; if(nums.size()<3) return res; sort(nums.begin(), n
阅读全文
摘要:class Solution { public: int maxArea(vector<int>& height) { int i = 0; int j = height.size()-1; int res = -1; while(i<j){ res = max(res,min(height[j],
阅读全文
摘要:注意这题求的是子串不是子序列 class Solution { public: string longestPalindrome(string s) { if(s.size()<2) return s; int len = s.length(); int maxlen = 1; int start
阅读全文
摘要:\ 此题与之前在一个数组中找中位数类似,可以在基础上修改。 不过在入堆的时候需要对两个数组大小元素进行判断,小的入堆。 class Solution { public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nu
阅读全文
摘要:class Solution { public: int lengthOfLongestSubstring(string s) { if(s.length()==0) return 0; vector<int> dp(s.size(),0); dp[0] = 1; // 初始状态。 // dp[i]
阅读全文
摘要:首先在官网下载指定版本opencv源码: https://opencv.org/releases/ cd ~/opencv #此处为opencv的解压文件目录 mkdir build cd build cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTAL
阅读全文
摘要:1 常量的定义方式 一:define定义的宏常量 二:const 修饰的变量 #define 常量名 常量值 const 类型 常量名 常量值 变量前加const关键字变成常量,不可更改。
阅读全文
摘要:class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { vector<int> res; unordered_map<int, int> map; for(int i = 0; i < nums.siz
阅读全文
摘要:https://blog.csdn.net/brazy/article/details/92801958 添加作者信息: https://www.php.cn/tool/vscode/441222.html 添加todo: https://blog.csdn.net/Redase/article/d
阅读全文
摘要:mac无法开机时可尝试以下方式开机 按住shift开机,出现白苹果和进度条后松开,进入安全启动模式; shift + ctrl + option + 开机 commond + option + 开机 commond + option + r + p + 开机 commond + r + 开机后,选择
阅读全文
摘要:(这里有一个更好的官方教程:https://tutorials.ubuntu.com/tutorial/tutorial-create-a-usb-stick-on-macos) 确保你的U盘至少有4G空间 获取Ubuntu ISO映像文件 应用->其它->磁盘工具 选择U盘,然后擦除。千万别选错了
阅读全文

浙公网安备 33010602011771号