题解 | #相等的草堆#

相等的草堆

https://www.nowcoder.com/practice/0e2f3b27bbdc45fcbc70cc4fd41e15fe

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param nums int整型一维数组 
 * @return int整型
 */
function pivotIndex(nums) {
    // write code here
    let res = -1
    let index = 1

    while (index < nums.length - 1) {
        let left = nums.slice(0, index).reduce((a, b) => a + b)
        let right = nums.slice(index + 1).reduce((a, b) => a + b)

        if (left === right) {
            return index
        }

        index++
    }


    return res
}
module.exports = {
    pivotIndex: pivotIndex
};

全部评论

相关推荐

09-21 23:16
门头沟学院 Java
传奇逃兵王:招不起就别招,叽里咕噜说啥呢
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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