Home Plus One
Post
Cancel

Plus One

Leetcode Problem

Plus One

주어진 배열을 숫자로 만들어 1을 더하고 다시 배열로 반환하는 문제입니다.

1
2
3
4
5
6
7
class Solution:
    def plusOne(self, digits: List[int]) -> List[int]:
        s = ""
        for x in digits:
            s += str(x)
        s = str(int(s) + 1)
        return [int(x) for x in s]

주어진 배열을 문자열로 바꿔서 더합니다. 더해진 문자열을 int형으로 바꿔서 1을 더한 후에 다시 int형 배열로 바꿉니다.



1
2
3
class Solution:
    def plusOne(self, digits: List[int]) -> List[int]:
        return map(int,str(int(''.join(map(str, digits))) + 1))

간단하게 map과 join을 이용하여 한 줄로도 끝낼 수 있었습니다.





참고

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