Interview icon indicating copy to clipboard operation
Interview copied to clipboard

Day384:用二分法移除一个字符串中的所有字母字符?(腾讯)

Open qappleh opened this issue 4 years ago • 2 comments

qappleh avatar Aug 24 '21 07:08 qappleh

移出字符串的字母为啥还得要二分法。。。

guanxc9876 avatar Aug 24 '21 09:08 guanxc9876

getValue() {
  const valList = []
  function handleLetter(str, idx) {
    if ((str[idx] >= 'a' && str[idx] <= 'z') || (str[idx] >= 'A' && str[idx] <= 'Z')) {
      valList.push(idx)
    }
  }
  function handle(str, direction = null, val = 0) {
    let low
    let high
    if (direction === 'left') {
      low = 0
      high = val - 1
    } else {
      low = val
      high = str.length - 1
    }

    if (low > high) return
    const mid = Math.floor((low + high) / 2)
    if (mid === low === high) return
    handleLetter(str, mid)
    if (mid === val) return
    if (mid + 1 === str.length - 1) {
      handleLetter(str, mid + 1)
      return
    }
    console.log('=====')
    if (Number(valList.length) === Number(str.length)) {
      [...new Set(valList)]
      console.log('valList', JSON.parse(JSON.stringify(valList)))
      return
    }
    (function(i) {
      setTimeout(function() {
        handle(str, 'right', i)
        handle(str, 'left', i)
      }, 0)
    })(mid)
  }
  handle('1a2b3d4e5f6g7h8i9j')
  console.log('===aaaaaaaaaaaaaaaaaaa====', valList)
},

废了,获取字母下标陷入死循环,还能不能抢救下

guanxc9876 avatar Aug 25 '21 08:08 guanxc9876