LC.P2697[字典序最小回文串]

方法一:贪心+双指针

1
2
3
4
5
6
7
8
9
class Solution {
public String makeSmallestPalindrome(String s) {
char[] cs = s.toCharArray();
for (int i = 0, j = cs.length - 1; i < j; ++i, --j) {
cs[i] = cs[j] = (char) Math.min(cs[i], cs[j]);
}
return new String(cs);
}
}
  • 时间复杂度:$O(n)$
  • 空间复杂度:$O(n)$