【每天40分钟,我们一起用50天刷完 (剑指Offer)】第四天 4/50

专注 效率 记忆
预习 笔记 复习 做题

欢迎观看我的博客,如有问题交流,欢迎评论区留言,一定尽快回复!(大家可以去看我的专栏,是所有文章的目录)
 
文章字体风格:
红色文字表示:重难点★✔
蓝色文字表示:思路以及想法★✔
 
如果大家觉得有帮助的话,感谢大家帮忙
点赞!收藏!转发!

本博客带大家一起学习,我们不图快,只求稳扎稳打。
由于我高三是在家自学的,经验教训告诉我,学习一定要长期积累,并且复习,所以我推出此系列。
只求每天坚持40分钟,一周学5天,复习2天
也就是一周学10道题
50天后我们就可以学完76道题,相信50天后,我们一定可以有扎实的代码基础!我们每天就40分钟,和我一起坚持下去吧!
qq群:866984458

本题出自 acwing网站
这个系列是免费的
打卡即刻退回费用。

第四天【剑指Offer例题代码 系列】

    • 8. 用两个栈实现队列
        • 补充:copy(a,b) 把a赋值给b
    • 9. 斐波那契数列
    • 10. 旋转数组的最小数字

8. 用两个栈实现队列

原题链接

补充:copy(a,b) 把a赋值给b

【每天40分钟,我们一起用50天刷完 (剑指Offer)】第四天 4/50_第1张图片
【每天40分钟,我们一起用50天刷完 (剑指Offer)】第四天 4/50_第2张图片

class MyQueue {
public:
    /** Initialize your data structure here. */
    stack<int> stk, cache;
    MyQueue() {

    }

    /** Push element x to the back of queue. */
    void push(int x) {
        stk.push(x);
    }

    void copy(stack<int> &a, stack<int> &b) {
        while (a.size()) {
            b.push(a.top());
            a.pop();
        }
    }

    /** Removes the element from in front of queue and returns that element. */
    int pop() {
        copy(stk, cache);
        int res = cache.top();
        cache.pop();
        copy(cache, stk);
        return res;
    }

    /** Get the front element. */
    int peek() {
        copy(stk, cache);
        int res = cache.top();
        copy(cache, stk);
        return res;
    }

    /** Returns whether the queue is empty. */
    bool empty() {
        return stk.empty();
    }
};

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * bool param_4 = obj.empty();
 */

9. 斐波那契数列

原题链接

class Solution {
    public int Fibonacci(int n) {
        if(n==1||n==2)
            return 1;
         int a = 0,b = 0,c = 0;
         a = 1;
         b = 1;
         for(int i = 3; i <= n; i++)
         {
             c = a + b;
             a = b;
             b = c;
         }
         return c;
    }
}

10. 旋转数组的最小数字

【每天40分钟,我们一起用50天刷完 (剑指Offer)】第四天 4/50_第3张图片

原题链接

class Solution {
public:
    int findMin(vector<int>& nums) {
        if(nums.size() == 0)
            return -1;
        if(nums.size() == 1)
            return nums[0];
        
        for(int i = 0; i < nums.size()-1; i++)
        {
            if(nums[i] > nums[i+1])
            {
                return nums[i+1];
            }
        }
        return nums[0];
    }
};

你可能感兴趣的:(java,开发语言)