随笔分类 - 【工作】leetcode
摘要:class Solution { public: int maxProfit(vector<int>& prices) { int counter=0;int ans=0; for(int i=1;i<prices.size();i++) { counter+=prices[i]-prices[i-
阅读全文
摘要:class Solution { public: int singleNumber(vector<int>& nums) { int n=0; for(auto a:nums) { n^=a; } return n; } };
阅读全文
摘要:f(n)=f(n-1)+f(n-2) class Solution { public: int climbStairs(int n) { int s1(1);int s2(2); int s3; if(n==1)return s1; if(n==2)return s2; for(int i=3;i<
阅读全文
摘要:#方法一暴力搜索 class Solution { public: int maxSubArray(vector<int>& nums) { int n=nums.size();int ans=nums[0];int sum=0;//注意这里,差值/临时存储可以用0。因为没有直接比较并和ans相关联
阅读全文
摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), rig
阅读全文
摘要:public: bool isOneBitCharacter(vector<int>& bits) { int n=bits.size();int i; for(i=0;i<n-1;) { if(bits[i]==1) i+=2; else if(bits[i]==0) i+=1;//注意不要忘了e
阅读全文
摘要:#递归 先解决空 在规定子递归项目 最后是现在怎么操作现在 【二者可以交换】 最后返回传递给父递归 #广度有限遍历 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; *
阅读全文
摘要:public: bool isValid(string s) { unordered_map<char,char> pairs={ { ')','('}, {']','['}, {'}','{'} }; stack<char> stk; for(char ch: s) { if(pairs.coun
阅读全文
摘要:#滑动窗口 public: int lengthOfLongestSubstring(string s) { int left=0,right=0,max=1; int len=s.size(); //int len=std::strlen(s);报错,只接受char*。可以使用s.c_str //
阅读全文
摘要:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) :
阅读全文
摘要:#暴力解法 class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { int n=nums.size(); for(int i=0; i<n;i++) { for(int j=i+1; j<n; j++)
阅读全文