初级算法

时间:2020-09-12 21:11:02   收藏:0   阅读:39
// log
/**
 * 找到第一个字母,若没有满足要求的返回null
 * @param {string} str
 */
function getFirstCharThatAppearsOnce(str) {
  let result
  let len = str.length
  let blackList = []
  for (let i = 0; i < len; i++) {
    result = str[i]
    if (blackList.indexOf(result) >= 0) {
      if (i === len - 1) {
        return null
      }
      continue
    }
    // test it
    for (let j = i + 1; j < len; j++) {
      if (result === str[j]) {
        blackList.push(result)
        break
      }
      if (j === len - 1) {
        return result
      }
    }
  }
}

let x1 = ‘facsdfas22p1dfawsdfasfapsdfdsasdfcnvcviuafawejklnauiyas1‘
let x2 = ‘aabbsdxzcfadzxcsdfa‘
let y1 = getFirstCharThatAppearsOnce(x1)
console.log(y1)
let y2 = getFirstCharThatAppearsOnce(x2)
console.log(y2)

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!