leetcode 1528. 重新排列字符串

在这里插入图片描述
在这里插入图片描述

class Solution {
public:
    string restoreString(string s, vector<int>& indices) {
        string temp = s;
        for(int i = 0; i < indices.size(); i++)
        {
            char c = s[i];
            s[i] = temp[indices[i]];
            temp[indices[i]] = c;
        }
        return temp;
    }
};

或者是

class Solution {
public:
    string restoreString(string s, vector<int>& indices) {
        string temp = s;
        for(int i = 0; i < indices.size(); i++)
        {
            swap(s[i], temp[indices[i]]);
        }
        return temp;
    }
};

猜你喜欢

转载自blog.csdn.net/weixin_43956456/article/details/107634803
今日推荐