Notice
Recent Posts
Recent Comments
Today
Total
01-26 23:33
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[2021 ์นด์นด์˜ค ์ฑ„์šฉ์—ฐ๊ณ„ํ˜• ์ธํ„ด์‹ญ] ์ˆซ์ž ๋ฌธ์ž์—ด๊ณผ ์˜๋‹จ์–ด ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm || ๋ฌธ์ œํ’€์ด/PS

[2021 ์นด์นด์˜ค ์ฑ„์šฉ์—ฐ๊ณ„ํ˜• ์ธํ„ด์‹ญ] ์ˆซ์ž ๋ฌธ์ž์—ด๊ณผ ์˜๋‹จ์–ด

WonderJay 2022. 6. 28. 12:07
728x90
๋ฐ˜์‘ํ˜•
SMALL

https://programmers.co.kr/learn/courses/30/lessons/81301

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์ˆซ์ž ๋ฌธ์ž์—ด๊ณผ ์˜๋‹จ์–ด

๋„ค์˜ค์™€ ํ”„๋กœ๋„๊ฐ€ ์ˆซ์ž๋†€์ด๋ฅผ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋„ค์˜ค๊ฐ€ ํ”„๋กœ๋„์—๊ฒŒ ์ˆซ์ž๋ฅผ ๊ฑด๋„ฌ ๋•Œ ์ผ๋ถ€ ์ž๋ฆฟ์ˆ˜๋ฅผ ์˜๋‹จ์–ด๋กœ ๋ฐ”๊พผ ์นด๋“œ๋ฅผ ๊ฑด๋„ค์ฃผ๋ฉด ํ”„๋กœ๋„๋Š” ์›๋ž˜ ์ˆซ์ž๋ฅผ ์ฐพ๋Š” ๊ฒŒ์ž„์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์ˆซ์ž์˜ ์ผ๋ถ€ ์ž

programmers.co.kr

 

์œ„ ๊ด€๊ณ„๋ฅผ Key(์ˆซ์ž) - value(์˜๋‹จ์–ด) ๋ผ๊ณ  ์ƒ๊ฐํ•˜๊ณ  map ์— ์ €์žฅํ•œ๋‹ค.

string s ๋ฅผ ์ˆœํšŒํ•˜๋Š”๋ฐ, digit ์ด๋ฉด answer ์— push_back ํ•ด์ฃผ๊ณ ,

digit ์ด ์•„๋‹ˆ๋ฉด temp ์— ์ €์žฅํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  temp ์™€ map ์˜ value ๋ฅผ ๋น„๊ตํ•ด์„œ ์ผ์น˜ํ•œ๋‹ค๋ฉด key ๊ฐ’์„ ans ์— push_back ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

#include <string>
#include <vector>
#include <unordered_map>
using namespace std;


int solution(string s) {
    int answer = 0;

    unordered_map <string, string> table;

    table.insert(make_pair("0", "zero"));
    table.insert(make_pair("1", "one"));
    table.insert(make_pair("2", "two"));
    table.insert(make_pair("3", "three"));
    table.insert(make_pair("4", "four"));
    table.insert(make_pair("5", "five"));
    table.insert(make_pair("6", "six"));
    table.insert(make_pair("7", "seven"));
    table.insert(make_pair("8", "eight"));
    table.insert(make_pair("9", "nine"));

    string ans = ""; string temp = "";
    for (char& c : s)
    {
        if (isdigit(c))
        {
            ans.push_back(c);
        }
        else
        {
            temp.push_back(c);

            for (auto& i : table)
            {
                if (temp == i.second)
                {
                    ans.append(i.first);
                    temp.clear();
                    break;
                }
            }
        }
    }
    answer = stoi(ans);

    return answer;
}

728x90
๋ฐ˜์‘ํ˜•
LIST
Comments