剑指 Offer 58 - I. 翻转单词顺序(isblank()函数)
class Solution {
public:
string reverseWords(string s) {
stack<string>d;
int l=s.size();
for(int i=0;i<l;i++)
{
string t;
while(i<l&&!isblank(s[i]))
{
t+=s[i];
i++;
}
if(!t.empty())
d.push(t);
}
string res="";
while(!d.empty())
{
res+=d.top();
d.pop();
if(!d.empty())
res+=' ';
}
return res;
}
};
剑指 Offer 58 - II. 左旋转字符串
class Solution {
public:
string reverseLeftWords(string s, int n) {
string res="";
string tmp,tmp1;
tmp=s.substr(0,n);
tmp1=s.substr(n);
res=tmp1+tmp;
return res;
}
};
剑指 Offer 59 - I. 滑动窗口的最大值
class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
vector<int>res;
if(nums.empty())return res;
int left=0,right=left+k;
while(right<=nums.size())
{
auto maxx=max_element(nums.begin()+left,nums.begin()+right);
res.push_back(*maxx);
left++;
right++;
}
return res;
}
};
剑指 Offer 59 - II. 队列的最大值(deque双端队列)
class MaxQueue {
public:
queue<int>q;
deque<int>help;
MaxQueue() {
}
int max_value() {
if(q.empty())
return -1;
else return help.front();
}
void push_back(int value) {
q.push(value);
while(!help.empty()&&value>help.back())
help.pop_back();
help.push_back(value);
}
int pop_front() {
if(q.empty()) return -1;
int res=q.front();
q.pop();
if(res==help.front())
help.pop_front();
return res;
}
};
/**
* 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();
*/
剑指 Offer 60. n个骰子的点数
class Solution {
public:
vector<double> twoSum(int n) {
int dp[15][70];
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= 6; i ++) {
dp[1][i] = 1;
}
for (int i = 2; i <= n; i ++) {
for (int j = i; j <= 6*i; j ++) {
for (int cur = 1; cur <= 6; cur ++) {
if (j - cur <= 0) {
break;
}
dp[i][j] += dp[i-1][j-cur];
}
}
}
int all = pow(6, n);
vector<double> ret;
for (int i = n; i <= 6 * n; i ++) {
ret.push_back(dp[n][i] * 1.0 / all);
}
return ret;
}
};