leet-code/src/main/java/leetcode/editor/cn/ReverseStringIi.java

63 lines
1.5 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//给定一个字符串 s 和一个整数 k从字符串开头算起每 2k 个字符反转前 k 个字符。
//
//
// 如果剩余字符少于 k 个,则将剩余字符全部反转。
// 如果剩余字符小于 2k 但大于或等于 k 个,则反转前 k 个字符,其余字符保持原样。
//
//
//
//
// 示例 1
//
//
//输入s = "abcdefg", k = 2
//输出:"bacdfeg"
//
//
// 示例 2
//
//
//输入s = "abcd", k = 2
//输出:"bacd"
//
//
//
//
// 提示:
//
//
// 1 <= s.length <= 104
// s 仅由小写英文组成
// 1 <= k <= 104
//
// Related Topics 双指针 字符串
// 👍 139 👎 0
package leetcode.editor.cn;
//541:反转字符串 II
class ReverseStringIi {
public static void main(String[] args) {
//测试代码
Solution solution = new ReverseStringIi().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public String reverseStr(String s, int k) {
char[] a = s.toCharArray();
for (int start = 0; start < a.length; start += 2 * k) {
int i = start, j = Math.min(start + k - 1, a.length - 1);
while (i < j) {
char tmp = a[i];
a[i++] = a[j];
a[j--] = tmp;
}
}
return new String(a);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}