euisblue
1528. Shuffle String

C++

1/** 2 * Runtime: 8 ms, faster than 94.80% 3 * Memory Usage: 15.4 MB, less than 63.23% 4 */ 5class Solution { 6public: 7 string restoreString(string s, vector<int>& indices) { 8 string result (indices.size(), 'a'); 9 int j=0; 10 for(int i : indices) 11 { 12 result[i] = s[j++]; 13 } 14 15 return result; 16 } 17};

JavaScript

1/** 2 * Runtime: 88 ms, faster than 73.88% 3 * Memory Usage: 39.2 MB, less than 79.04% 4 */ 5var restoreString = function(s, indices) { 6 let result = new Array(indices.length); 7 let j = 0; 8 indices.forEach(i => { 9 result[i] = s[j]; 10 j+=1 11 }) 12 13 return result.join(''); 14};

Ruby

1# Runtime: 68 ms, faster than 41.18% 2# Memory Usage: 210.4 MB, less than 36.97% 3def restore_string(s, indices) 4 result = 'a' * indices.size 5 j = 0 6 indices.each do |i| 7 result[i] = s[j] 8 j += 1 9 end 10 11 return result 12end