题解 | #牛牛猜节点#

牛牛猜节点

https://www.nowcoder.com/practice/6a3dfb5be4544381908529dc678ca6dd

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param first int整型 
     * @param second int整型 
     * @param n int整型 
     * @return int整型
     */
    int findNthValue(int first, int second, int n) {
        // write code here
        if (n == 1) return first;
        if (n == 2) return second;

        long long prev1 = second;
        long long prev2 = first;
        long long current = 0;

        for (int i = 3; i <= n; ++i) {
            current = prev1 + prev2;
            prev2 = prev1;
            prev1 = current;
        }

        return current;
    }
};

全部评论

相关推荐

哈哈哈,你是老六:我去,这面试还要靠抢啊
点赞 评论 收藏
分享
11-06 23:30
已编辑
华中师范大学 后端工程师
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务