LeetCode 1528 重排字符串 day01

简介: LeetCode 1528 重排字符串 day01

思路: 创建一个新数组,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);
    }
}
目录
相关文章
|
1月前
|
存储 算法
LeetCode第43题字符串相乘
LeetCode第43题"字符串相乘"的解题方法,通过使用数组存储乘积并处理进位,避免了字符串转换数字的复杂性,提高了算法效率。
LeetCode第43题字符串相乘
|
1月前
|
算法 Java
LeetCode第28题找出字符串中第一个匹配项的下标
这篇文章介绍了LeetCode第28题"找出字符串中第一个匹配项的下标"的两种解法:暴力解法和KMP算法,并解释了KMP算法通过构建前缀表来提高字符串搜索的效率。
LeetCode第28题找出字符串中第一个匹配项的下标
|
1月前
|
算法
LeetCode第8题字符串转换整数 (atoi)
该文章介绍了 LeetCode 第 8 题字符串转换整数 (atoi)的解法,需要对字符串进行格式解析与校验,去除前导空格和处理正负号,通过从高位到低位的计算方式将字符串转换为整数,并处理越界情况。同时总结了这几道题都需要对数字的表示有理解。
LeetCode第8题字符串转换整数 (atoi)
|
3月前
|
算法
力扣每日一题 6/23 字符串/模拟
力扣每日一题 6/23 字符串/模拟
26 1
|
3月前
力扣经典150题第四十题:同构字符串
力扣经典150题第四十题:同构字符串
24 1
|
3月前
|
索引
力扣每日一题 6/27 字符串 贪心
力扣每日一题 6/27 字符串 贪心
24 0
|
3月前
|
Python
力扣随机一题 模拟+字符串
力扣随机一题 模拟+字符串
22 0
|
3月前
力扣每日一题 6/22 字符串/贪心
力扣每日一题 6/22 字符串/贪心
21 0
|
3月前
力扣每日一题 6/18 字符串/模拟
力扣每日一题 6/18 字符串/模拟
20 0
|
3月前
|
算法
力扣每日一题 6/16 字符串 + 随机一题 动态规划/数学
力扣每日一题 6/16 字符串 + 随机一题 动态规划/数学
32 0