思路: 创建一个新数组,indices[i]为新数组存放数值的index。
package com.stan.algo.leetcode; public class RestoreString { /** * 输入:s = "codeleet", indices = [4,5,6,7,0,2,1,3] * 输出:"leetcode" * 解释:如图所示,"codeleet" 重新排列后变为 "leetcode" 。 * <p> * 来源:力扣(LeetCode) * 链接:https://leetcode-cn.com/problems/shuffle-string * 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 * * @param s * @param indices * @return */ public static String restoreString(String s, int[] indices) { char[] chars = s.toCharArray(); char[] newChars = new char[chars.length]; for (int i = 0; i < chars.length; i++) { newChars[indices[i]] = chars[i]; } return new String(newChars); } }