1.【计算数组中心位置】
function getValue(nums) {
let arr = [1, ...nums]
let leftValue = 1;
let rightValue = 1;
let counts = nums.reduce(function(prev, cur, index, arr) {
return prev*cur
}, 1)
let result = 1;
for (let index = 1; index < arr.length; index++) {
leftValue = leftValue * (arr[index-1])
if ((leftValue * leftValue) === counts/arr[index]) {
result = index
return result - 1
}
}
console.log('12',result)
return result
}
getValue([2,5,3,6,5,6])
console.log(getValue([2,5,3,6,5,6]))
2.【统计友好度最大值】
这道题目的友好度定义为:空位中左右连续老员工之和,转化为0位置左右的连续为1的总和的最大值。
function getValue(nums) {
let result = 0
if (nums.indexOf(0) === -1) {
return 0
}
let left_stash = 0
let right_stash = 0
let restart = false
for (let left_index = 0; left_index < nums.length; left_index++) {
if (nums[left_index] === 1) {
left_stash = left_stash + 1
continue
}
if (nums[left_index] === 0) {
let right_index = left_index + 1
while (nums[right_index] === 1) {
right_stash = right_stash + 1;
right_index = right_index + 1
}
left_index = right_index
debugger
result = Math.max(result, left_stash + right_stash)
}
if (nums[left_index] === 0) {
left_stash = right_stash
right_stash = 0
}
if (nums[left_index] === 2) {
left_stash = 0
right_stash = 0
left_index = left_index + 1
}
}
return result
}
getValue([1,1,0,1,2,1,0,1,1,1,2,1,1])