bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 2165. Smallest Value of the Rearranged Number

2165. Smallest Value of the Rearranged Number

Leetcode Solutions

Sort and Swap

  1. Convert the number to a string and take its absolute value.
  2. Sort the string:
    • In ascending order if the number is positive.
    • In descending order if the number is negative.
  3. 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.
  4. Convert the string back to a number.
  5. If the original number was negative, multiply the result by -1 to restore the sign.
  6. Return the result.
UML Thumbnail

Count Digits and Reconstruct Number

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...