<p>You are given an integer <code>num</code>. You can swap two digits at most once to get the maximum valued number.</p>

<p>Return <em>the maximum valued number you can get</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> num = 2736
<strong>Output:</strong> 7236
<strong>Explanation:</strong> Swap the number 2 and the number 7.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> num = 9973
<strong>Output:</strong> 9973
<strong>Explanation:</strong> No swap.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>0 &lt;= num &lt;= 10<sup>8</sup></code></li>
</ul>

class Solution {
public:
    int maximumSwap(int num) {
        int n = num, nn = 0;
        int testSwap = 0, testIndex = -1;
        int r1 = -1, rI1 = 0;
        int r2 = 0, rI2 = 0;
        int tens = 0;
        int i = 0;
        bool isStart = true;
        while (n > 0) {
            int d = n % 10;
            n /= 10;
            if (d == 0 && isStart) {
                tens++;
            }
            if (d > 0)
                isStart = false;

            if (i > 0 && d < testSwap) {
                r1 = d;
                rI1 = i;
                r2 = testSwap;
                rI2 = testIndex;
                
            }

            if (d > testSwap) {
                testSwap = d;
                testIndex = i;
            }
            nn = (nn * 10) + d;
            i++;
        }

        if (r1 == -1) {
            return num;
        }
        i--;

        while (nn > 0) {
            int d = nn % 10;
            nn /= 10;
            n *= 10;

            if (i == rI1) {
                n += r2;
            }
            else if (i == rI2) {
                n += r1;
            }
            else {
                n += d;
            }
            i--;
        }
        if (tens > 0) {
            n = n * (10 * tens);
        }
        return n;
    }
};
7200