0
Leetcode Problem 2165. Smallest Value of the Rearranged Number
2165. Smallest Value of the Rearranged Number
AI Mock Interview
Leetcode Solutions
Sort and Swap
Solution Idea
Algorithm Steps
Code Implementation
Complexity Analysis
Convert the number to a string and take its absolute value.
Sort the string:
In ascending order if the number is positive.
In descending order if the number is negative.
If the number is positive and the first digit is zero:
Find the index of the first non-zero digit.
Swap the first digit with the non-zero digit found.
Convert the string back to a number.
If the original number was negative, multiply the result by -1 to restore the sign.
Return the result.
Count Digits and Reconstruct Number
Ask Question
Programming Language
Purpose:
General Question
Debug My Code
image/screenshot of info
(optional)
[+]
Full Screen
Loading...
Get Answer
Suggested Answer
Answer
Full Screen
Copy Answer Code
Loading...