class Solution {
public:intstrToInt(string str){int len = str.size();int i =0;int flag =1;//标记正负longlong num =0;//返回转化后的数字while(str[i]==' ')//去掉前导空格{i++;}if(str[i]=='+'||str[i]=='-'){if(str[i]=='-')flag =-flag;i++;}while(i < len && str[i]>='0'&& str[i]<='9'){num = num*10+(str[i]-'0')*flag;i++;if(flag ==-1&& num < INT_MIN ){return INT_MIN;}if(flag ==1&& num > INT_MAX ){return INT_MAX;}}if(i != len)//没有遍历完就是错误,直接返回当前的值{return num;}return num;}};
class Solution {
public:voidexchange(vector<int>&nums,int len){int* ret =(int*)malloc(sizeof(int)*(len +1));int j =0;for(int i =0; i < len; i++){if((nums[i]&1)==1){ret[j++]= nums[i];}}for(int i =0; i < len; i++){if((nums[i]&1)==0){ret[j++]= nums[i];}}for(int i =0; i < len; i++){nums[i]= ret[i];}}vector<int>exchange(vector<int>& nums){int len = nums.size();exchange(nums,len);return nums;}};
class Solution {
public:vector<int>twoSum(vector<int>& nums,int target){int len = nums.size();unordered_map<int,int>hash;vector<int>ret;for(int i =0; i < len; i++){int num = target - nums[i];if(hash.count(num)){ret.push_back(num);ret.push_back(nums[i]);break;}hash.insert({nums[i],i});}return ret;}};
class Solution {
public:vector<int>maxSlidingWindow(vector<int>& nums,int k){int len = nums.size();deque<int>q;vector<int>ret;for(int i =0; i < nums.size(); i++){if(!q.empty()&& i - k +1> q[0]) q.pop_front();//下标小于滑动窗口则头删//形成单调递增的队列while(!q.empty()&& nums[q[q.size()-1]]<= nums[i]) q.pop_back();q.push_back(i);if(i >= k -1){ret.push_back(nums[q[0]]);}}return ret;}};
class CQueue {
public:CQueue(){}voidappendTail(int value){p_push.push(value);}intdeleteHead(){if(p_push.empty()&& p_pop.empty()){return-1;}if(!p_pop.empty()){int top = p_pop.top();p_pop.pop();return top;}else{while(!p_push.empty()){p_pop.push(p_push.top());p_push.pop();}}int top = p_pop.top();p_pop.pop();return top;}
private:stack<int>p_push;stack<int>p_pop;};/*** Your CQueue object will be instantiated and called as such:* CQueue* obj = new CQueue();* obj->appendTail(value);* int param_2 = obj->deleteHead();*/
class MinStack {stack<int>x_push;stack<int>min_push;
public:/** initialize your data structure here. */MinStack(){min_push.push(INT_MAX);}voidpush(int x){x_push.push(x);min_push.push(std::min(min_push.top(),x));//每次插入插最小的值进去,和最小值的栈顶比较。}voidpop(){x_push.pop();min_push.pop();}inttop(){return x_push.top();}intmin(){return min_push.top();}};/*** Your MinStack object will be instantiated and called as such:* MinStack* obj = new MinStack();* obj->push(x);* obj->pop();* int param_3 = obj->top();* int param_4 = obj->min();*/
class MaxQueue {
public:deque<int>val;deque<int>vmax;MaxQueue(){}intmax_value(){if(vmax.empty()){return-1;}return vmax.front();}voidpush_back(int value){while((!vmax.empty())&&(vmax.back()< value))//单调递减的队列{vmax.pop_back();}val.push_back(value);vmax.push_back(value);}intpop_front(){if(val.empty()){return-1;}int ans = vmax.front();if(ans == val.front())//只有和最大值相等才需要删除,跟滑动窗口差不多{vmax.pop_front();}int front = val.front();val.pop_front();return front;}};/*** Your MaxQueue object will be instantiated and called as such:* MaxQueue* obj = new MaxQueue();* int param_1 = obj->max_value();* obj->push_back(value);* int param_3 = obj->pop_front();*/
class Solution {
public:intfindRepeatNumber(vector<int>& nums){unordered_map<int,int>hash;int k =0;for(int i =0; i < nums.size(); i++){if(hash.count(nums[i]))//hash判断该数有没有出现过{k = i;return nums[k];}hash.insert({nums[i],i});}return nums[k];}};
//遍历法
class Solution {
public:intsearch(vector<int>& nums,int target){int count =0;for(int i =0; i < nums.size(); i++){if(nums[i]> target){break;}if(nums[i]== target){count++;}}return count;}};//二分法// class Solution {// public:// int search(vector<int>& nums, int target) {// if(nums.size() == 0) return 0;// int a = 0,b = 0;// int left = 0,right = nums.size() - 1;// while(left < right)// {// int mid = left + right >> 1;// if(nums[mid] >= target) right = mid;// else left = mid + 1;// }// if(nums[left] != target) return 0;// a = left;// left = 0,right = nums.size() - 1;// while(left < right)// {// int mid = left + right + 1>> 1;// if(nums[mid] <= target) left = mid;// else right = mid - 1;// }// b = left;// return b - a + 1;// }// };
class Solution {
public:intmissingNumber(vector<int>& nums){int n = nums.size();int sum =0;for(int i =1; i <= n; i++){sum += i;}for(int i =0; i < n; i++){sum -= nums[i];}return sum;}};
class Solution {
public:charfirstUniqChar(string s){if(s.size()==0)return' ';vector<int>h;int arr[26]={0};for(int i =0; i < s.size(); i++){arr[s[i]-'a']++;}for(int i =0; i < s.size(); i++){if(arr[s[i]-'a']==1)//找到所有只出现一次的字符{return s[i];}}return' ';}};//哈希法
class Solution {
public:charfirstUniqChar(string s){unordered_map<int,int> frequency;for(char ch: s){++frequency[ch];}for(int i =0; i < s.size();++i){if(frequency[s[i]]==1){return s[i];}}return' ';}};
class Solution {
public:vector<vector<int>>levelOrder(TreeNode* root){if(root == nullptr)return{};queue<TreeNode*>q;vector<vector<int>>ret;q.push(root);while(!q.empty()){int num = q.size();ret.push_back(vector<int>());for(int i =0; i < num; i++){auto node = q.front();q.pop();ret.back().push_back(node->val);//在尾部进行尾插,因为插入了二维空vectorif(node->left) q.push(node->left);if(node->right) q.push(node->right);}}int k = ret.size();for(int i =0; i < k; i++){if(i %2!=0)//奇数反转{reverse(ret[i].begin(),ret[i].end());}}return ret;}};
//我写的dfs
class Solution {
public:intget(int x){int sum =0;while(x >0){sum += x %10;x /=10;}return sum;}voidcheck(vector<vector<int>>&ans,int row,int col,int k,int&res,int sum,int o){if(sum > k)return;res++;int a = ans.size(),b = ans[0].size();ans[row][col]= true;//走过就不要走了for(int i =0; i <2; i++){int x = row + dx[i], y = col + dy[i];if(x >=0&& x < a && y >=0&& y < b &&!ans[x][y]){int q =get(x), p =get(y);int sum1 = p + q;check(ans,x,y,k,res,sum1,o+1);}}}intmovingCount(int m,int n,int k){if(k ==0)return1;vector<vector<int>>ans(m,vector<int>(n));int res =0;//表示走格子的最大值int o =1;int sum =0;//记录数位和check(ans,0,0,k,res,sum,o);return res;}
private:int dx[4]={1,0};//优化成下和右int dy[4]={0,1};};//官方题解bfs
class Solution {// 计算 x 的数位之和intget(int x){int res=0;for(; x; x /=10){res += x %10;}return res;}
public:intmovingCount(int m,int n,int k){if(!k)return1;queue<pair<int,int>> Q;// 向右和向下的方向数组int dx[2]={0,1};int dy[2]={1,0};vector<vector<int>>vis(m, vector<int>(n,0));Q.push(make_pair(0,0));vis[0][0]=1;int ans =1;while(!Q.empty()){auto[x, y]= Q.front();Q.pop();for(int i =0; i <2;++i){int tx = dx[i]+ x;int ty = dy[i]+ y;if(tx <0|| tx >= m || ty <0|| ty >= n || vis[tx][ty]||get(tx)+get(ty)> k)continue;Q.push(make_pair(tx, ty));vis[tx][ty]=1;ans++;}}return ans;}};
class Codec {
public:voidrserialize(TreeNode * root,string & ret){//前序遍历转为字符串if(root == nullptr){ret +="#,";return;}ret +=to_string(root->val);//字符转换函数ret +=",";rserialize(root->left,ret);rserialize(root->right,ret);}// Encodes a tree to a single string.string serialize(TreeNode* root){string ret;rserialize(root,ret);return ret;}// Decodes your encoded data to tree.TreeNode*rdeserialize(list<string>&dateArray){if(dateArray.front()=="#"){dateArray.erase(dateArray.begin());return nullptr;}TreeNode * root = new TreeNode(stoi(dateArray.front()));//将字符串转为数字dateArray.erase(dateArray.begin());//插入后删除root->left =rdeserialize(dateArray);root->right =rdeserialize(dateArray);return root;}TreeNode*deserialize(string data){list<string>dateArray;//用链表存字符串string str;for(auto& c : data){if(c ==','){dateArray.push_back(str);str.clear();}else{str.push_back(c);}}if(!str.empty())//如果后面有节点就需要插入在清空{dateArray.push_back(str);str.clear();}returnrdeserialize(dateArray);}};// Your Codec object will be instantiated and called as such:// Codec codec;// codec.deserialize(codec.serialize(root));
class Solution {
public:vector<int>printNumbers(int n){vector<int>ret;if(n ==0)return ret;int k =1;while(n--){k *=10;}for(int i =1; i < k; i++){ret.push_back(i);}return ret;}};
class Solution {
public:doublePow(double x,longlong n){if(n ==0)return1.0;double y =Pow(x,n /2);//快速幂算法//如果n为偶数就是返回的结果的平方,否则就是返回的结果多乘xreturn n %2==0? y * y : y * y * x;}doublemyPow(double x,int n){if(n ==0)return1.0;longlong N = n;//防止越界return N >=0?Pow(x,N):1/Pow(x,-N);}};
class Solution {
public:bool isStraight(vector<int>& nums){//大小王相当于赖子,只要最大值和最小值的差小于5肯定是顺子int maxi =0, mini =14;unordered_map<int,int>hash;for(int i =0; i <5; i++){if(nums[i]==0)continue;//遇到大小王跳过maxi =::max(nums[i],maxi);mini =::min(nums[i],mini);if(hash.count(nums[i]))//如果重复则不是顺子{return false;}hash.insert({nums[i],i});}if(maxi - mini <5)return true;return false;}};
class Solution {
public:intfib(int n){longlong* dp = new longlong[110];dp[0]=0;dp[1]=1;for(int i =2; i <= n; i++){dp[i]=(dp[i -1]+ dp[i -2])%(1000000007);}return(int)dp[n];}};
class Solution {
public:intnumWays(int n){longlong* dp = new longlong[110];dp[0]=1;dp[1]=1;for(int i =2; i <= n; i++){dp[i]=(dp[i -1]+ dp[i -2])%(1000000007);}return(int)dp[n];}};
class Solution {
public:intmaxSubArray(vector<int>& nums){vector<int>dp(nums.size(),0);dp[0]= nums[0];for(int i =1; i < nums.size(); i++){dp[i]=max(dp[i -1]+ nums[i],nums[i]);}int maxi =-0x3f3f3f3f;for(int i =0; i < nums.size(); i++){maxi =max(dp[i],maxi);}return maxi;}};
class Solution {
public:inttranslateNum(int num){string str =to_string(num);int len = str.size();vector<int>dp(len+1,0);//类似青蛙跳台阶问题,青蛙可以一直跳一个台阶,跳两个台阶要分情况dp[0]=1;dp[1]=1;for(int i =2; i <= len; i++){string sub = str.substr(i -2,2);//从第几位开始取多少位字符串if(sub <="25"&& sub >="10"){dp[i]= dp[i -1]+ dp[i -2];}else{dp[i]= dp[i -1];}}return dp[len];}};
class Solution {
public:intnthUglyNumber(int n){priority_queue<double,vector<double>,greater<double>>q;double ans =1;for(int i =1; i < n; i++){q.push(ans*2);q.push(ans*3);q.push(ans*5);ans = q.top();q.pop();while(!q.empty()&& q.top()== ans) q.pop();}return ans;}};
class Solution {
public:vector<int>GetNum(vector<int>&nums,int sz){vector<int>ret;int k =0;for(int i =0; i <31; i++){if((sz >> i)&1==1){k = i;break;}}int a =0, b =0;for(int i =0; i < nums.size(); i++){if(((nums[i]>> k)&1)==0){a ^= nums[i];}else{b ^= nums[i];}}ret.push_back(a);ret.push_back(b);return ret;}vector<int>singleNumbers(vector<int>& nums){vector<int>ret;int sz =0;for(int i =0; i < nums.size(); i++){sz ^= nums[i];}returnGetNum(nums,sz);}};
class Solution {
public:vector<int>constructArr(vector<int>& a){vector<int>ret;if(a.size()==0)return ret;int len = a.size();vector<int>left(len,0);vector<int>right(len,0);left[0]=1;//因为左侧没有元素,所以赋值为1for(int i =1; i < len; i++){left[i]= a[i -1]* left[i -1];}right[len -1]=1;//因为len- 1右侧没有元素,所以赋值为1for(int i = len -2; i >=0; i--){right[i]= a[i +1]* right[i +1];}for(int i =0; i < len; i++){ret.push_back(left[i]* right[i]);}return ret;}};