zoukankan      html  css  js  c++  java
  • 1528. 重新排列字符串

    题目:给你一个字符串 s 和一个 长度相同 的整数数组 indices 。请你重新排列字符串 s ,其中第 i 个字符需要移动到 indices[i] 指示的位置。返回重新排列后的字符串。

    示例 :

     

    输入:s = "codeleet", indices = [4,5,6,7,0,2,1,3]
    输出:"leetcode"
    解释:如图所示,"codeleet" 重新排列后变为 "leetcode" 。

    1.原创

    class Solution {
    public:
        string restoreString(string s, vector<int>& indices) {
            int n = s.size();
            char temp[n+1];
            string res;
            for (int i=0;i<n;++i){
                temp[indices[i]] = s[i];
            }
            temp[n] ='';
    
            for (int i=0;i<strlen(temp);++i){ //strlen是查找字符串中的“”,即结束符
                res += temp[i];
            }
            return res;
        }
    };

    2.题解

    class Solution {
    public:
        string restoreString(string s, vector<int>& indices) {
            int length = s.length();
            string result(length, 0);
    
            for(int i = 0; i < length; i++) {
                result[indices[i]] = s[i];
            }
            return result;
        }
    };
    
    作者:LeetCode-Solution
    链接:https://leetcode-cn.com/problems/shuffle-string/solution/zhong-xin-pai-lie-zi-fu-chuan-by-leetcode-solution/
  • 相关阅读:
    c语言命名规则 [转载]
    [转贴]C编译过程概述
    [转贴]漫谈C语言及如何学习C语言
    Semaphore源码分析
    如何快速转行大数据
    web前端到底怎么学?
    Code Review怎样做好
    SDK与API的理解
    分析消费者大数据
    程序员的搞笑段子
  • 原文地址:https://www.cnblogs.com/USTC-ZCC/p/14468325.html
Copyright © 2011-2022 走看看