본문 바로가기
IT/LeetCode

[LeetCode] 66. Plus One

by 강천구 2022. 8. 19.

You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's.

Increment the large integer by one and return the resulting array of digits.

 

Example 1:

Input: digits = [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
Incrementing by one gives 123 + 1 = 124.
Thus, the result should be [1,2,4].

Example 2:

Input: digits = [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.
Incrementing by one gives 4321 + 1 = 4322.
Thus, the result should be [4,3,2,2].

Example 3:

Input: digits = [9]
Output: [1,0]
Explanation: The array represents the integer 9.
Incrementing by one gives 9 + 1 = 10.
Thus, the result should be [1,0].

 

Constraints:

  • 1 <= digits.length <= 100
  • 0 <= digits[i] <= 9
  • digits does not contain any leading 0's.
반응형

주어진 숫자 배열에서 1을 더한 배열을 리턴하는 문제이다.

기존의 숫자 배열을 하나의 숫자로 정리한 후 1을 더하고 새로운 배열에 append를 이용해 하나하나 넣는 방식으로 작성했다.

 

class Solution:
   def plusOne(self, digits: 'List[int]') -> 'List[int]':
       sum = 0
       newDigits = []
       for i in range(0, len(digits)):
           sum = sum*10 + digits[i]
       sum += 1
       str = sum.__str__()
       for i in range(0, len(str)):
           newDigits.append(int(str[i]))
       return newDigits

 

문제 출처 : https://leetcode.com/problems/plus-one/

 

Plus One - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

Github : https://github.com/kkkkang1009/leetcode/blob/master/leetcode_66.py

 

GitHub - kkkkang1009/leetcode: leetcode

leetcode. Contribute to kkkkang1009/leetcode development by creating an account on GitHub.

github.com

 

반응형

'IT > LeetCode' 카테고리의 다른 글

[LeetCode] 69. Sqrt(x)  (0) 2022.08.19
[LeetCode] 67. Add Binary  (0) 2022.08.19
[LeetCode] 64. Minimum Path Sum  (0) 2022.08.17
[LeetCode] 63. Unique Paths II  (0) 2022.08.17
[LeetCode] 62. Unique Paths  (0) 2022.08.17

댓글