剑指offer_2020_9_20

剑指 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;
    }
}; 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值