【字符串】【完整程序+倒序输出+每个区间倒序输出】KamaCoder55
- 解法1 ==完整程序书写kama== 倒序输出+每个区间倒序输出
---------------🎈🎈题目链接🎈🎈-------------------
解法1 完整程序书写kama 倒序输出+每个区间倒序输出
时间复杂度O(N)
空间复杂度O(1)
import java.util.Arrays;
import java.util.Scanner;
class Main{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int k = scanner.nextInt();
String str = scanner.next();
char[] ch = str.toCharArray();
// 倒序输出
int left = 0;
int right = ch.length-1;
while(left < right){
ch[left] ^= ch[right];
ch[right] ^= ch[left];
ch[left] ^= ch[right];
left++;
right--;
}
// 每个区间再倒叙输出
left = 0;
right = k-1;
while(left < right){
ch[left] ^= ch[right];
ch[right] ^= ch[left];
ch[left] ^= ch[right];
left++;
right--;
}
left = k;
right = ch.length-1;
while(left < right){
ch[left] ^= ch[right];
ch[right] ^= ch[left];
ch[left] ^= ch[right];
left++;
right--;
}
System.out.println(ch);
}
}