Count number of 1s

17.09.20201 Min Read — In Algorithms

Count occurence of number 1s from 1 to an upperbound number

Question

sum1s(400)
// -> return 180

Solution

const sum1s = num => {
  let numstr
  if (!num) return 0
  if (typeof num === 'string') numstr = num
  else numstr = String(num)

  if (Number(numstr) === 0) return 0

  const curr =
    numstr[0] > 1
      ? 10 ** (numstr.length - 1) +
        numstr[0] * (numstr.length - 1) * 10 ** (numstr.length - 2)
      : sum1s(10 ** (numstr.length - 1) - 1) + 1

  return curr + sum1s(numstr.substr(1))
}
你必须登录才能发表评论