剑指 Offer第一天

剑指 Offer 09. 用两个栈实现队列

class CQueue {
public:
    stack<int> stk1, stk2;
    CQueue() {
        while (stk1.size()) stk1.pop();
        while (stk2.size()) stk2.pop();
    }
    
    void appendTail(int value) {
        stk1.push(value);
    }
    
    int deleteHead() {
        if (stk2.size() == 0) {
            while (stk1.size()) {
                stk2.push(stk1.top());
                stk1.pop();
            }
        }
        if (stk2.size() == 0) return -1;
        int t = stk2.top();
        stk2.pop();
        return t;
    }
};

/**
 * Your CQueue object will be instantiated and called as such:
 * CQueue* obj = new CQueue();
 * obj->appendTail(value);
 * int param_2 = obj->deleteHead();
 */

剑指 Offer 10- I. 斐波那契数列

class Solution {
public:
    int fib(int n) {
        int a = 0, b = 1;
        int mod = 1e9 +7;
        if (!n) return 0;
        for (int i = 2; i <= n; i ++ ) {
            int c = (a + b) % mod;
            a = b;
            b = c;
        }
        return b;
    }
};

剑指 Offer 03. 数组中重复的数字

class Solution {
public:
    int findRepeatNumber(vector<int>& nums) {
        unordered_set<int> S;
        for (auto& t : nums) {
            if (S.count(t)) return t;
            S.insert(t);
        }
        return 0;
    }
};

剑指 Offer 04. 二维数组中的查找

class Solution {
public:
    bool findNumberIn2DArray(vector<vector<int>>& matrix, int target) {
        if(matrix.size() == 0 )return false;
        int i = 0, j = matrix[0].size() - 1;
        while (i < matrix.size() && j >= 0)
        {
            if(matrix[i][j] == target) return true;
            else if(matrix[i][j] > target) j --;
            else i ++;
        }
        return false;
    }
};

剑指 Offer 10- II. 青蛙跳台阶问题

class Solution {
public:
    int numWays(int n) {
        const int N=1e9+7;
        int a[105];
        a[0]=1,a[1]=1;
        for(int i=2;i<=n;i++)
        {
            a[i]=(a[i-2]%N+a[i-1]%N)%N;
        }
        return a[n];
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Shirandexiaowo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值