1528. 重新排列字符串
1528. 重新排列字符串
- 重新排列字符串
给你一个字符串 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”
思路:考察hashmap :
先用一个hashmap映射字符和数字,把数组重新排序一下,
打印Hashmap
package 期末;import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashMap;/** 1528. 重新排列字符串
给你一个字符串 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"思路:考察hashmap :
先用一个hashmap映射字符和数字,把数组重新排序一下,
打印Hashmap*/
public class a1528重新排列字符串 {public static String restoreString(String s, int[] indices) {HashMap<Integer, Character> ha =new HashMap<Integer, Character>();char []ch =s.toCharArray();int index=0;for (char c : ch) {ha.put(indices[index], c);//index++;}Arrays.sort(indices); //数组重新排序StringBuilder res =new StringBuilder();for (int i = 0; i < indices.length; i++) {res.append(ha.get(indices[i]));}// System.out.println(res.toString());return res.toString();}public static void main(String[] args) {// restoreString("codeleet",new int[]{4,5,6,7,0,2,1,3});restoreString("aiohn",new int[]{3,1,4,2,0});}
}
官方答案
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);}
}
来源:力扣leetcode
1528. 重新排列字符串相关推荐
- LeetCode 1528. 重新排列字符串
给你一个字符串 s 和一个 长度相同 的整数数组 indices . 请你重新排列字符串 s ,其中第 i 个字符需要移动到 indices[i] 指示的位置. 返回重新排列后的字符串. 输入:s = ...
- 算法题-1528. 重新排列字符串
leetcode: https://leetcode-cn.com/problems/shuffle-string/ 给你一个字符串 s 和一个 长度相同 的整数数组 indices . 请你重新排列 ...
- 【排序-简单】1528. 重新排列字符串
[题目] 给你一个字符串 s 和一个 长度相同 的整数数组 indices . 请你重新排列字符串 s ,其中第 i 个字符需要移动到 indices[i] 指示的位置. 返回重新排列后的字符串. [ ...
- leetcode题目1528. 重新排列字符串
题目链接:https://leetcode-cn.com/problems/shuffle-string/ 给你一个字符串 s 和一个 长度相同 的整数数组 indices . 请你重新排列字符串 s ...
- 字符串题目:重新排列字符串
文章目录 题目 标题和出处 难度 题目描述 要求 示例 数据范围 解法 思路和算法 代码 复杂度分析 题目 标题和出处 标题:重新排列字符串 出处:1528. 重新排列字符串 难度 3 级 题目描述 ...
- 面试题 01.03. URL化 + 重新排列字符串
面试题 01.03. URL化 题目链接:https://leetcode-cn.com/problems/string-to-url-lcci/ URL化.编写一种方法,将字符串中的空格全部替换为% ...
- 按阅读习惯来高效排列字符串的脚本
对于字符串的排序,我们知道是比较相同index位置的字符的值来进行的.这样的排序有个问题,就是当被排序的字符串的内容是有数字顺序的,而且还不等长就比较的郁闷了.我们在xp以前的操作系统的资源管理器里, ...
- leetcode1528. 重新排列字符串
给你一个字符串 s 和一个 长度相同 的整数数组 indices . 请你重新排列字符串 s ,其中第 i 个字符需要移动到 indices[i] 指示的位置. 返回重新排列后的字符串. 示例 1: ...
- LeetCode之重新排列字符串(五千四百七十二)
目录 题目 解题 方法一.直接法 方法二.转换法 题目 (原题链接:https://leetcode-cn.com/problems/shuffle-string/) 给你一个字符串 s 和一个 长度 ...
最新文章
- AttributeError: ‘NoneType‘ object has no attribute ‘group‘
- 我为什么喜欢Go语言
- mysql怎么看实例名_南方“中看不中吃”的前4名水果,莲雾只是垫底,你怎么看?...
- Java即时类| toString()方法与示例
- python参数检验框架_基于python的人员识别检测框架研究
- SpringCloud集成Security安全(Eureka注册中心)
- 基于 Gitlab 交付 Go 程序的 Docker 镜像
- 中科大计算机考研录取分数线_中科大计算机考研 | 跨考CS上岸经验分享!
- (转)被AI改变的风投模式:数据驱动使效率提高10倍
- win10把AppData目录挪到其他盘的方法
- OPPO小布4.0:软件定义硬件,智能定义“助手”
- IOS生物信息验证(Touch IDFace ID)
- 账号密码登录(Account password authentication)
- 非递归遍历二叉树(后序遍历)
- 少儿编程课程设计理论(可用做论文)
- 周易六十四卦——复卦
- 如何在网页中插网页的代码?
- 【第五届集创赛备赛】五、紫光FPGA Cortex-M1 SoC快速上手
- Python接口自动化测试实战系列
- 微服务之间的服务治理 方案
热门文章
- discuz论坛出现“请求来路不明”提示的解决方法
- 将 confirm的确定取消修改为是否
- 如何将java集合中重复的元素取出来
- TH-OCR文字识别SDK 12.X介绍
- 张萌韩墨羽——Ument_Share集成
- 软件开发工期估算系列(3)——見積もり値の「幼虫」「サナギ」「成虫」
- apache php 内存,不再为Apache进程淤积、耗尽内存而困扰
- 英文期刊投稿指南模板(通用版)-----以IEEE Wireless Communications为例
- 调用Python时出现Exception ignored in: module 'threading' from 'xxx:\\Anaconda3\\Lib\\threading.py'
- 移植Linux 3.4.2内核到JZ2440开发板(超精讲)