∞拾年

js 算法题
题目:电话号码的字母组合给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返...
扫描右侧二维码阅读全文
10
2021/06

js 算法题

题目:电话号码的字母组合

给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number

//我的解答【有的方法写的还不健全 精力有限】
class LetterCombinations {
  constructor() {
    //字母数组
    this.aryLetters = []
    //数字数组
    this.aryNumber = []
    //组合后的数组
    this.newData = []

    //初始化
    this.init()
  }
  //初始化
  init() {
    this.getLetters()
    this.getNumber()
    this.getNewData()
  }
  //获取字母数组
  getLetters() {
    for (var i = 0; i <= 25; i++) {
      this.aryLetters.push(String.fromCharCode((65 + i)).toLowerCase());
    }

  }
  //获取数字数组
  getNumber() {
    for (let i = 0; i <= 9; i++) {
      this.aryNumber.push(i)
    }
  }
  //组合数据【格式 {aryNumber[item]:letterAry】
  //需要注意 0,1没有对应的字母;7 9 对应4个字母
  //config配置参数【最后一个telNumber数字返回剩下的所有letterAry字母】

  getNewData() {
    let configParams = {
      //截取字母长度
      dafaultStrLength: 3,
      //返回本身Number号
      selfNum: [0, 1],
      //特殊处理的Number[注意配置的长度不能大于默认长度dafaultStrLength]
      otherNumber: [{
        key: 7,
        length: 4
      }, {
        key: 9,
        length: 4
      }]
    }
    let start = null
    let end = null
    //循环aryNumber每一项作为新数据对象中的key
    this.aryNumber.forEach(i => {
      //临时对象
      let _obj = {}
      //先过滤返回本身的aryNumber[i]
      if (configParams.selfNum.includes(i)) {
        _obj[i] = i
        this.newData.push(_obj)
        return
      }
      //默认长度配置
      start = !end ? (i - 2) * configParams.dafaultStrLength : end
      end = !end ? (i - 1) * configParams.dafaultStrLength : start + configParams.dafaultStrLength
      //其他长度处理
      configParams.otherNumber.forEach(j => {
        if (i === j.key) {

          end = !end ? (i - 1) * configParams.dafaultStrLength + Math.abs(configParams.dafaultStrLength - j.length) : start + configParams.dafaultStrLength + Math.abs(configParams.dafaultStrLength - j.length)
        }
      }
      )

      //截取字母作为aryNumber[i]的value
      _obj[i] = this.aryLetters.slice(start, end)
      //push 
      this.newData.push(_obj)
    }
    )

  }
  //获取需要组合的数组集
  getAssociationData(...arg) {
    if (arg.length === 0) {
      console.log('请输入正确的数字格式')
      return []
    }
    if (arg.includes(1) || arg.includes(0)) {
      return []
    }
    //需要组合的数组集
    let associationData = []
    arg.forEach(k => {
      if (k === 0 || k === 1) {
        associationData.push([k])
        return
      }
      associationData.push(this.newData.filter(m => m[k])[0][k])
    }
    )
    return this.getFinishData(associationData)
  }
  //组合字母方法一
  getFinishData(ary) {
    let result = []
    ary.forEach(i => {
      let _ary = []
      i.forEach(n => {
        if (result.length > 0) {
          _ary.push(...result.map(m => m + n))
        } else {
          _ary.push(n)
        }
      })
      result = _ary
    })
    return result
  }

}

let instandes = new LetterCombinations()
instandes.getAssociationData(2, 3, 4)


//二维数组字母组合方法二-递归
getFinishData(ary) {
  if (!Array.isArray(ary[ary.length - 1])) {
    return ary
  }
  let otherAry = ary.slice(2, initAry.length)
  let thisAry = ary[0].map(item => ary[1].map(iten => item + iten)).flat()
  let newAry = otherAry.length === 0 ? thisAry : [thisAry].concat(otherAry)
  return dos(newAry)
}
//二维数组字母组合方法三-递归
let formatUseDigits = [['a','b','c'],['d','e','f']]
let count = formatUseDigits.length
let result = []
function doOperating(str='', index=0) {
    // 只push 符合长度的
    if (str.length === count) {
        result.push(str);
    }
    // 递归条件
    if (index < count) {
        const curr = formatUseDigits[index];
        if (curr) {
            for (const iterator of curr) {
                
                // 拼接字符串
                doOperating(str + iterator, index + 1);
            }
        }
    }
}
doOperating()
console.log(result)

截屏2021-06-10 下午11.46.06.png

后端同事被卡住的一道面试题

WechatIMG10677.jpeg

急着给写答案,方法可能不是太精简

let n = 6, r = 10, x = [1, 7, 15, 20, 30, 50];
let res = []

function getSome(ary) {

  if (ary.length == 0) {
    return
  } else if (ary.length == 1) {
    res.push(ary[0])
    return
  }
  let newAry = []
  let a = ary[0]
  //获取最后一个小于r的位置作为中间位置
  let bAry = ary.filter(i => i <= ary[0] + r)
  let b = ary[bAry.length - 1]
  let c = ary[2]
  if (b > a + r) {
    newAry = ary.splice(1)
    res.push(a)
    getSome(newAry)
  } else if (b <= a + r && !c) {
    res.push(b)

  } else if (b <= a + r && b + r <= c) {
    newAry = ary.splice(bAry.length)
    res.push(b)
    getSome(newAry)

  } else if (b <= a + r && b + r >= c) {
    newAry = ary.splice(bAry.length + 1)
    res.push(b)
    getSome(newAry)
  }
}
getSome(x)
console.log(`位置集合:${res}`,`摄像头数量:${res.length}`)
Last modification:June 23rd, 2021 at 09:32 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment