1.String类的toCharArray()方法,将字符串转为字符(char)数组
2.String类的valueOf()方法,将字符(char)数组转换为字符串
要注意的是toCharArray()还是比较拖时间的方法,能不用就不用,在leetcode实战种第二个例子很好的说明这一点。
3. leetcode实战
给你一个字符串 s 和一个 长度相同 的整数数组 indices 。
请你重新排列字符串 s ,其中第 i 个字符需要移动到 indices[i] 指示的位置。
返回重新排列后的字符串。
class Solution {
public String restoreString(String s, int[] indices) {
char[] ans = new char[indices.length];
for(int i = 0 ; i < indices.length; i++){
ans[indices[i]] = s.charAt(i);
}
return String.valueOf(ans);
}
}