-
我第一次做法
public String restoreString(String s, int[] indices) { if (s == null || indices == null) { return null; } char[] result = new char[indices.length]; for (int i = 0; i < indices.length; i++) { result[indices[i]] = s.charAt(i); } return String.valueOf(result); }
-
其他解法
public static String restoreString1(String s, int[] indices) { if (s == null || indices == null) { return null; } char[] chars = s.toCharArray(); for (int i = 0; i < chars.length; i++) { if (i != indices[i]) { int targetIndex = indices[i]; char tmp = chars[targetIndex]; chars[targetIndex] = chars[i]; chars[i] = tmp; int tmp2 = indices[targetIndex]; indices[targetIndex] = indices[i]; indices[i] = tmp2; // 由于替换完成后,并不能确定,再继续执行遍历会将剩余的元素完全替换,则重置便利游标 // 举例: "aiohn" {3,1,4,2,0} i=0; } } return String.valueOf(chars); }
1528-重新排列字符串
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 直接上代码 #####################生成神经网络模型和训练部分的代码##############...
- 前言 最先接触编程的知识是在大学里面,大学里面学了一些基础的知识,c语言,java语言,单片机的汇编语言等;大学毕...
- 子序列顺序不能变 输入: abc 输出: // 第一个是空串 c b bc a ac ab abc 代码: pub...