# Maximum Swap

Given a non-negative integer, you could swap two digits **at most** once to get the maximum valued number. Return the maximum valued number you could get.

## Example

**Example 1:**

```
Input: 2736

Output: 7236

Explanation: Swap the number 2 and the number 7.
```

**Example 2:**

```
Input: 9973

Output: 9973

Explanation: No swap.
```

## Note

本质是寻找右边第一个最大的和左边第一个比它小的。需要满足左右的位置关系

从右往左遍历，记录最大的值和位置，需要continue

遇到比最大小的就记录，一直往左会找到最左边比最大值小的那个数字

如果一直右往左一直递增，是特殊情况，第二个if语句不会执行，left会一直是-1，直接返回

## Code

```java
class Solution {
    public int maximumSwap(int num) {
        String s = String.valueOf(num);
        char[] c = s.toCharArray();
        int len = c.length;

        int left = -1, right = -1;
        int max = -1, index = -1;
        for (int i = len - 1; i >= 0; i--) {
            if (c[i] > max) {
                index = i;
                max = c[i];
                continue;
            }

            if (c[i] < max) {
                left = i;
                right = index;
            }
        }

        System.out.println(right + " " + left);

        if (left == -1) {
            return num;
        }

        char temp = c[left];
        c[left] = c[right];
        c[right] = temp;

        return Integer.parseInt(String.valueOf(c));
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://luj.gitbook.io/code/string/4-number/maximum-swap.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
