leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit eebb47978e7c8ceed09a4d76572a7608b287066e
parent 739f042f2cd9dc7ac136d82c5698eaaefe63d5ba
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Tue, 23 Apr 2024 23:06:45 +0200

1 Random Problem

Diffstat:
AProblems/2165.cpp | 33+++++++++++++++++++++++++++++++++
MREADME.md | 1+
2 files changed, 34 insertions(+), 0 deletions(-)

diff --git a/Problems/2165.cpp b/Problems/2165.cpp @@ -0,0 +1,33 @@ +class Solution { + public: + long long smallestNumber(long long num) { + static uint8_t digits[16]; + bool neg = false; + uint8_t cnt = 0; + + if (num < 0) { + num = -num; + neg = true; + } + + do { + digits[cnt++] = num % 10; + } while ((num /= 10) > 0); + + if (neg) + sort(begin(digits), begin(digits) + cnt, greater()); + else { + sort(begin(digits), begin(digits) + cnt); + for (int i = 0; i < cnt; i++) { + if (digits[i] == 0) continue; + swap(digits[0], digits[i]); + break; + } + } + + num = 0; + for (int i = 0; i < cnt; i++) + num = (num * 10) + digits[i]; + return !neg ? num : -num; + } +}; diff --git a/README.md b/README.md @@ -1027,6 +1027,7 @@ for solving problems. | 2150 | Medium | [Find All Lonely Numbers in the Array](Problems/2150.cpp) | | 2155 | Medium | [All Divisions With the Highest Score of a Binary Array](Problems/2155.cpp) | | 2161 | Medium | [Partition Array According to Given Pivot](Problems/2161.cpp) | +| 2165 | Medium | [Smallest Value of the Rearranged Number](Problems/2165.cpp) | | 2177 | Medium | [Find Three Consecutive Integers That Sum to a Given Number](Problems/2177.cpp) | | 2178 | Medium | [Maximum Split of Positive Even Integers](Problems/2178.cpp) | | 2181 | Medium | [Merge Nodes in Between Zeros](Problems/2181.cpp) |