# LEETCODE 1505. Minimum Possible Integer After at Most K Adjacent Swaps On Digits 解题思路分析

```输入：num = "4321", k = 4

```

```输入：num = "100", k = 1

```

```输入：num = "36789", k = 1000

```

```输入：num = "22", k = 22

```

```输入：num = "9438957234785635408", k = 23

```

• `1 <= num.length <= 30000`
• `num` 只包含 数字 且不含有 前导 0
• `1 <= k <= 10^9`

```public String minInteger(String num, int k) {
char[] arr=num.toCharArray(); // 为了方便计算，将字符串转为数组
return String.valueOf(help(arr,k, 0)); // 递归求解
}
// arr：数组
// k：当前剩余步数
// index：当前高位
char[] help(char[] arr,int k, int index){
// 如果下标越界或者k为0，返回
if(index==arr.length||k==0) return arr;
// 找出当前高位之后最小的数字以及所在下标
char min=arr[index];
int minIndex=index;
for(int i=index;i<arr.length&&i-index<=k;i++){
if(arr[i]<min){
min=arr[i];
minIndex=i;
}
}
// 将当前高位到minIndex-1间的的数字分别向后移动一位
for(int i=minIndex;i>index;i--){
arr[i]=arr[i-1];
}
// 将最高位设置为最小值（相当于冒泡排序）
arr[index]=min;
// 递归求解次高位
return help(arr,k-(minIndex-index),index+1);
}```

Runtime: 929 ms, faster than 33.33% of Java online submissions for Minimum Possible Integer After at Most K Adjacent Swaps On Digits.

Memory Usage: 43.8 MB, less than 100.00% of Java online submissions for Minimum Possible Integer After at Most K Adjacent Swaps On Digits.