题解 | #杨辉三角的变形#

杨辉三角的变形

https://www.nowcoder.com/practice/8ef655edf42d4e08b44be4d777edbf43

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

// 方法一:模拟取最后一行遍历,运行超时22/30 组用例通过。用例输入10000
void async function () {
    let n = parseInt(await readline());
    if(n <=2) return console.log(-1);
    let pre = [1],cur = [];
    while(--n){
        pre.unshift(0);
        pre.unshift(0);
        for(let i = 0; i < pre.length; i++){
            cur.push(pre.slice(i,i+3).reduce((a,b)=>a+b,0));
        }
        [pre,cur]=[[...cur],[]];
    }
    for(let i = 0; i < pre.length; i++){
        if(pre[i]%2===0){
            console.log(i+1);
            break;
        }
    }
}()

// 方法二:找规律
void async function () {
    const n = parseInt(await readline());
    if(n <=2) return console.log(-1);
    const rule = [2,3,2,4];
    console.log(rule[(n-2+3)%4])
}()

全部评论

相关推荐

野猪不是猪🐗:我assume that你must技术aspect是solid的,temperament也挺good的,however面试不太serious,generally会feel style上不够sharp
点赞 评论 收藏
分享
04-25 18:13
五邑大学 Java
后来123321:大二两段实习太厉害了,我现在大二连面试都没有
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务