반응형
    
    
    
  https://leetcode.com/problems/shuffle-string/
map 을 이용한 문제였습니다.
📕 풀이방법
📔 입력 및 초기화
key 는 index, value로 문자를 저장할 map 변수 m과 정답 변수 ans를 선언해줍니다.
📔 풀이과정
indices에 대해 loop를 수행하며 현재 위치 indices[i]의 값은 s[i]임을 m에 key, value로 저장해줍니다.
📔 정답 출력 | 반환
ans를 반환해줍니다.
📕 Code
📔 C++
class Solution {
public:
    string restoreString(string s, vector<int>& indices) {
        map<int, char> m;
        string ans;
        for(int i = 0; i <indices.size(); i++) {
            m[indices[i]] = s[i]; 
        }
        for(auto el : m) {
            ans += el.second;
        }
        return ans;
    }
};*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.
'Algorithm > 자료구조' 카테고리의 다른 글
| (C++) - LeetCode (easy) 1614. Maximum Nesting Depth of the Parentheses (0) | 2024.05.11 | 
|---|---|
| (C++) - LeetCode (easy) 1544. Make The String Great (0) | 2024.04.23 | 
| (C++) - LeetCode (easy) 1507. Reformat Date (0) | 2024.04.09 | 
| (C++) - LeetCode (easy) 1408. String Matching in an Array (0) | 2024.03.13 | 
| (C++) - LeetCode (easy) 1389. Create Target Array in the Given Order (0) | 2024.03.05 |