TreeviewCopyright © aleen42 all right reserved, powered by aleen42
1528. 重新排列字符串
Java
/*
* @Author: Goog Tech
* @Date: 2020-08-14 13:47:08
* @LastEditTime: 2020-08-14 13:54:14
* @Description: https://leetcode-cn.com/problems/shuffle-string/
* @FilePath: \leetcode-googtech\#1528. Shuffle String\Solution.java
* @WebSite: https://algorithm.show/
*/
/*
* @lc app=leetcode.cn id=5472 lang=java
*
* [5472] 重新排列字符串
*/
// @lc code=start
class Solution {
public String restoreString(String s, int[] indices) {
// 创建一个与字符串相同长度的数组
int length = s.length();
char[] result = new char[length];
// 循环填充数组,其中indices[i]作为索引,s.charAt(i)作为索引值
for(int i = 0; i < length; i++) {
result[indices[i]] = s.charAt(i);
}
// 将数组装换为字符串并返回
return new String(result);
}
}
// @lc code=end
Python
'''
Author: Goog Tech
Date: 2020-08-14 13:24:04
LastEditTime: 2020-08-14 13:54:33
Description: https://leetcode-cn.com/problems/shuffle-string/
FilePath: \leetcode-googtech\#1528. Shuffle String\Solution.py
WebSite: https://algorithm.show/
'''
#
# @lc app=leetcode.cn id=5472 lang=python
#
# [5472] 重新排列字符串
#
# @lc code=start
class Solution(object):
def restoreString(self, s, indices):
"""
:type s: str
:type indices: List[int]
:rtype: str
"""
# 创建一个长度为len(s)的数组
result = ["" for i in range(len(s))]
# 循环填充数组,其中indices[i]作为索引,s[i]作为索引值
for i in range(len(s)):
result[indices[i]] = s[i]
# 将数组转换为字符串并返回
return "".join(result)
# @lc code=end