题解 | #相等的草堆#
相等的草堆
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
};
