-
[LeetCode] Plus One알고리즘 2019. 11. 3. 20:18
문제
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.
Approach
마지막자리에 1을 더하고 carry가 생기면 carry가 안생길때까지 앞자리도 바꿔준다
마지막까지 carry가 생기면 맨 앞에 1을 넣어준다.
Code
https://github.com/chi3236/algorithm/blob/master/LeetCode_PlusOne.cpp
'알고리즘' 카테고리의 다른 글
[LeetCode] Climbing Stairs (0) 2019.11.03 [LeetCode] Sqrt(x) (0) 2019.11.03 [LeetCode] Unique Paths (0) 2019.11.03 [LeetCode] Merge Intervals (0) 2019.11.02 [LeetCode] Jump Game (0) 2019.11.01