手写笔试题

97 阅读1分钟

1.【计算数组中心位置】 image.png

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.【统计友好度最大值】 image.png 这道题目的友好度定义为:空位中左右连续老员工之和,转化为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])

3.【深度优先搜索/机器人】

image.png

4.【开放日活动/二分法】

image.png

image.png