給你一個字符串 s 和一個 長度相同 的整數數組 indices 。
請你重新排列字符串 s ,其中第 i 個字符需要移動到 indices[i] 指示的位置。
返回重新排列后的字符串。
?
示例 1:
輸入:s = "codeleet", indices = [4,5,6,7,0,2,1,3]
輸出:"leetcode"
解釋:如圖所示,"codeleet" 重新排列后變為 "leetcode" 。
示例 2:
輸入:s = "abc", indices = [0,1,2]
輸出:"abc"
解釋:重新排列后,每個字符都還留在原來的位置上。
示例 3:
輸入:s = "aiohn", indices = [3,1,4,2,0]
輸出:"nihao"
示例 4:
輸入:s = "aaiougrt", indices = [4,0,2,6,7,3,1,5]
輸出:"arigatou"
示例 5:
輸入:s = "art", indices = [1,0,2]
輸出:"rat"
?
提示:
s.length == indices.length == n
1 <= n <= 100
s 僅包含小寫英文字母。
0 <= indices[i] <?n
indices 的所有的值都是唯一的(也就是說,indices 是整數 0 到 n - 1 形成的一組排列)。
思路:模擬直接放
class Solution {public String restoreString(String s, int[] indices) {int length = s.length();char[] result = new char[length];for (int i = 0; i < length; i++) {result[indices[i]] = s.charAt(i);}return new String(result);}
}
?