Home Convert a Number to Hexadecimal
Post
Cancel

Convert a Number to Hexadecimal

Leetcode Problem

Convert a Number to Hexadecimal

주어진 수를 16진법으로 바꿔서 출력하는 문제입니다. 음수일 경우 보수를 출력합니다.

1
2
3
class Solution:
    def toHex(self, num: int) -> str:
        return "%x" % num if num >= 0 else "%x" % (num + 2**32)

음수일 경우 보수 처리를 해야하므로 2*32를 더해줍니다.





참고

This post is licensed under CC BY 4.0 by the author.