관리 메뉴

솜씨좋은장씨

[leetCode] 66. Plus One (Python) 본문

Programming/코딩 1일 1문제

[leetCode] 66. Plus One (Python)

솜씨좋은장씨 2020. 5. 20. 17:28
728x90
반응형

Given a non-empty array of digits representing a non-negative integer, plus one to the integer.

The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.

You may assume the integer does not contain any leading zero, except the number 0 itself.

 

Example 1:

Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.

Example 2:

Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.

Solution

class Solution:
    def plusOne(self, digits: List[int]) -> List[int]:
        num_string_list = list(map(str, digits))
        num_string = ''.join(num_string_list)
        
        my_number = int(num_string)
        
        answer_number = my_number + 1
        
        answer_number_string = str(answer_number)
        
        answer_number_list = list(answer_number_string)
        
        answer = list(map(int, answer_number_list))
        
        return answer 

코드를 조금 줄인 Solution

class Solution:
    def plusOne(self, digits: List[int]) -> List[int]:
        num_string_list = list(map(str, digits))
        num_string = ''.join(num_string_list)
        
        my_number = int(num_string) + 1
        
        answer_number_string = list(str(my_number))
        
        answer = list(map(int, answer_number_string))
        
        return answer 

 

 

SOMJANG/CODINGTEST_PRACTICE

1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.

github.com

 

Comments