refactor: remove unnecessary using declarations and qualify std symbols
Replace global using declarations with explicit std:: qualifications. Update ranges algorithms usage to std::ranges and fix permutation iterator offset to correctly handle 1-based k indexing.
This commit is contained in:
+8
-14
@@ -4,25 +4,19 @@
|
|||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
using std::set;
|
|
||||||
using std::string;
|
|
||||||
using std::ranges::next_permutation;
|
|
||||||
using std::ranges::reverse;
|
|
||||||
using std::ranges::sort;
|
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
string smallestPalindrome(string s, int k) {
|
std::string smallestPalindrome(std::string s, int k) {
|
||||||
|
|
||||||
string half = s.substr(0, s.size() / 2);
|
std::string half = s.substr(0, s.size() / 2);
|
||||||
|
|
||||||
sort(half);
|
std::ranges::sort(half);
|
||||||
|
|
||||||
set<string> permutations;
|
std::set<std::string> permutations;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
permutations.insert(half);
|
permutations.insert(half);
|
||||||
} while (next_permutation(half).found);
|
} while (std::ranges::next_permutation(half).found);
|
||||||
|
|
||||||
if (permutations.size() > std::numeric_limits<int>::max()) {
|
if (permutations.size() > std::numeric_limits<int>::max()) {
|
||||||
throw std::overflow_error("Number of permutations exceeds int limit");
|
throw std::overflow_error("Number of permutations exceeds int limit");
|
||||||
@@ -30,14 +24,14 @@ public:
|
|||||||
int size = static_cast<int>(permutations.size());
|
int size = static_cast<int>(permutations.size());
|
||||||
|
|
||||||
if (k > size) {
|
if (k > size) {
|
||||||
return string();
|
return std::string();
|
||||||
}
|
}
|
||||||
|
|
||||||
auto it = permutations.begin();
|
auto it = permutations.begin();
|
||||||
advance(it, k - 1);
|
advance(it, k - 1);
|
||||||
|
|
||||||
string mirroredHalf = *it;
|
std::string mirroredHalf = *it;
|
||||||
reverse(mirroredHalf);
|
std::ranges::reverse(mirroredHalf);
|
||||||
|
|
||||||
return *it + mirroredHalf;
|
return *it + mirroredHalf;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user