leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0670.cpp (505B)
0 class Solution { 1 public: 2 int maximumSwap(int numi) const { 3 string num = to_string(numi); 4 const int n = size(num); 5 int last[10] = {0}; 6 7 for (int i = 0; i < n; i++) 8 last[num[i] - '0'] = i; 9 for (int i = 0; i < n; i++) { 10 for (int d = 9; d > num[i] - '0'; d--) { 11 if (last[d] <= i) continue; 12 swap(num[i], num[last[d]]); 13 return stoi(num); 14 } 15 } 16 17 return numi; 18 } 19 };