[LeetCode] 3697. Compute Decimal Representation

Chobby·2026년 1월 22일

LeetCode

목록 보기
948/992

😎풀이

  1. n을 문자열 형변환 하여 각 자릿수에 해당하는 값을 곱함
  2. 각 요소를 모두 더하면, n이 나오는 배열을 반환
function decimalRepresentation(n: number): number[] {
    const strN = String(n)
    const nLen = strN.length
    const decimal = []
    for(let i = 0; i < nLen; i++) {
        const digit = Number(strN[i])
        if(!digit) continue
        let power = '1'
        for(let j = 0; j < nLen - (i + 1); j++) {
            power += '0'
        }
        decimal.push(digit * Number(power))
    }
    return decimal
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글