class Solution {
public:
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
int n = pushed.size();
stack<int> stk;
int idx =0;
for(auto it:pushed){
stk.push(it);
while(!stk.empty() && popped[idx] == stk.top()){
stk.pop();
idx++;
}
}
return stk.empty();
}
};
自己瞎写的快速幂方法
class Solution {
public:
double myPow(double x, int n) {
double k = x;
double res = 1;
if(n < 0) k =1.0/k;
while(n){
if(n%2==1 || n%2 == -1){
res = res*k;
}
k = k*k;
n/=2;
}
return res;
}
};
效果似乎不是很好.png
K神的题解一如既往的清晰明了
class Solution {
public:
vector<string> permutation(string s) {
dfs(s,0);
return res;
}
vector<string> res;
void dfs(string& s,int idx){
if(idx == s.size()-1){
res.push_back(s);
return ;
}
set<int> vis;
for(int i =idx; i < s.size(); i++){
if(vis.count(s[i])) continue;
vis.insert(s[i]);
swap(s[i],s[idx]);
dfs(s,idx+1);
swap(s[i],s[idx]);
}
}
};
image.png
网友评论