To solve the problem of rounding a large number represented as a string to the nearest multiple of 10, we need to handle the operation efficiently, especially considering the constraint where the string length can be up to (10^5) digits.
Approach:
-
Identify the Last Digit:
- The last digit of the number determines whether we should round up or down.
- If the last digit is greater than 5, we round up.
- If the last digit is less than or equal to 5, we round down. In the case where it's exactly 5, we choose to round down to the smaller multiple as specified.
-
Rounding Down:
- Simply set the last digit to '0'.
-
Rounding Up:
- Add 1 to the number excluding the last digit.
- Handle any carry that may occur during this addition.
- After handling the carry, set the last digit to '0'.
- If the carry propagates all the way to the most significant digit, prepend '1' to the number.
-
Handling Edge Cases:
- When the number consists of all '9's, rounding up will result in an additional digit (e.g., "999" becomes "1000").
- If the string length is 1, ensure that rounding is handled correctly (e.g., "5" becomes "0").
Implementation:
Here's the Python implementation of the above approach within the Solution class:
class Solution:
def roundToNearest(self, str_num):
# Convert the string to a list for easy manipulation
s = list(str_num)
n = len(s)
last_digit = int(s[-1])
if last_digit > 5:
# Need to round up
carry = 1
i = n - 2 # Start from the second last digit
while i >= 0 and carry:
if s[i] == '9':
s[i] = '0'
carry = 1
else:
s[i] = str(int(s[i]) + 1)
carry = 0
i -= 1
if carry:
# If carry is still 1 after the loop, prepend '1'
s = ['1'] + s
n += 1 # Update the length after prepending
s[-1] = '0' # Set the last digit to '0' after rounding up
else:
# Round down (this includes the case when last_digit == 5)
s[-1] = '0'
return ''.join(s)
Explanation:
-
Conversion to List:
- Strings in Python are immutable, so we convert the string to a list of characters to allow in-place modifications.
-
Determining Rounding Direction:
- We first check the value of the last digit to decide whether to round up or down.
-
Rounding Up:
- We iterate from the second last digit towards the front, adding the carry as needed.
- If a digit is '9', it becomes '0' and the carry remains.
- Otherwise, we increment the digit and remove the carry.
- If a carry remains after processing all digits, we prepend '1' to handle cases like "999" becoming "1000".
- Finally, we set the last digit to '0'.
-
Rounding Down:
- Simply set the last digit to '0' without modifying the preceding digits.
-
Returning the Result:
- Join the list back into a string and return it as the final rounded number.
Examples:
-
Input: "29"
Output: "30"
Explanation: The last digit is 9 (>5), so we round up to "30".
-
Input: "15"
Output: "10"
Explanation: The last digit is 5, so we round down to the smaller multiple "10".
-
Input: "99999"
Output: "100000"
Explanation: Rounding up causes a carry that propagates through all digits.
Time and Space Complexity:
-
Time Complexity: (O(n)), where (n) is the length of the string. We might need to traverse the entire string in the worst case (e.g., when all digits are '9').
-
Space Complexity: (O(n)) due to the conversion of the string to a list for manipulation.
This solution efficiently handles large numbers by performing only necessary operations and ensures that the result is correctly rounded according to the problem's specifications.