[LeetCode] 3602. Hexadecimal and Hexatrigesimal Conversion

Chobby·2026년 1월 16일

LeetCode

목록 보기
933/989

😎풀이

  1. 전달된 10진수를 다른 진수로 변환해주는 헬퍼 함수 toBase 정의
  2. n의 제곱을 16진법으로 변환
  3. n의 세제곱을 36진법으로 변환
  4. 두 결괏값을 문자열 형태로 결합하여 반환
function concatHex36(n: number): string {
    const hexadecimal = toBase(n ** 2, 16)
    const hexatrigesimal = toBase(n ** 3, 36)
    return hexadecimal + hexatrigesimal
};

function toBase(num: number, base: number) {
    const hex = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
    let result = ''
    while(num) {
        result = hex[num % base] + result
        num = Math.floor(num / base)
    }
    return result
}
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글