class Solution {
public:
int maximumSwap(int num) {
std::string origin_n = std::to_string(num);
std::vector<char> c;
std::vector<char> max;
std::vector<int> vec;
int ret =0;
for(int i=0 ; i< origin_n.length(); i++)
{
c.push_back(origin_n.at(i));
max.push_back(origin_n.at(i));
}
std::sort(max.begin(), max.end(), std::greater<>());
if(std::equal(c.begin(), c.end(), max.begin()))
{
string str(max.begin(), max.end());
ret = std::stoi(str);
return ret;
}
for(int i=0; i< c.size(); i++)
{
for(int j=i+1; j< c.size(); j++ )
{
char temp = c[i];
c[i] = c[j];
c[j] = temp;
string str(c.begin(), c.end());
vec.push_back(std::stoi(str));
temp = c[i];
c[i] = c[j];
c[j] = temp;
}
}
int m = *max_element(vec.begin(), vec.end());
return m;
}
};
No comments:
Post a Comment