๋ฌธ์
์ฝ๋
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
char mapping(string number){
if(number =="zero") return '0';
if(number =="one") return '1';
if(number =="two") return '2';
if(number =="three") return '3';
if(number =="four") return '4';
if(number =="five") return '5';
if(number =="six") return '6';
if(number =="seven") return '7';
if(number =="eight") return '8';
if(number =="nine") return '9';
return '0';
}
bool check(string number){
for(int i=0; i<11; i++){
if(number =="zero") return true;
if(number =="one") return true;
if(number =="two") return true;
if(number =="three") return true;
if(number =="four") return true;
if(number =="five") return true;
if(number =="six") return true;
if(number =="seven") return true;
if(number =="eight") return true;
if(number =="nine") return true;
}
return false;
}
int solution(string s) {
int answer = 0;
string temp = "", number ="";
for(int i=0; i<s.length()+1; i++){
bool flag = check(temp);
char mapping_num;
if(flag){
number+=mapping(temp);
temp.clear();
}
if(s[i]>='a'&&s[i]<='z') temp+=s[i];
else{
if(!temp.empty()){
number +=mapping_num;
}
number+=s[i];
temp.clear();
}
}
answer = stoi(number);
return answer;
ํ์ด(19๋ถ)
๋ฌธ์ ์์ฒด๋ ์ฌ์ ์ง๋ง ์ฝ๋๊ฐ ๋๋ฌด ์ง์ ๋ถํ์ต๋๋ค. ๊ทธ๋์ ๋ค๋ฅธ ์ฌ๋์ ํ์ด๋ฅผ ๋ณด๋ค๊ฐ c++์ regex ์ ๊ท์ ๋ฉ์๋๋ฅผ ์๊ฒ ๋์๊ณ , ์๋์ ๊ฐ์ด ์ฝ๋๋ฅผ ์์ฑํ ์ ์์์ต๋๋ค.
#include <string>
#include <vector>
#include <regex>
using namespace std;
int solution(string s) {
int answer = 0;
s = regex_replace(s, regex("zero"),"0");
s = regex_replace(s, regex("one"),"1");
s = regex_replace(s, regex("two"),"2");
s = regex_replace(s, regex("three"),"3");
s = regex_replace(s, regex("four"),"4");
s = regex_replace(s, regex("five"),"5");
s = regex_replace(s, regex("six"),"6");
s = regex_replace(s, regex("seven"),"7");
s = regex_replace(s, regex("eight"),"8");
s = regex_replace(s, regex("nine"),"9");
answer = stoi(s);
return answer;
}
์ฝ๋๊ฐ ํจ์ฌ ๊ฐ๊ฒฐํด์ก์ต๋๋ค.
'Algorithm ๐ง๐ปโ๐ป > ํ๋ก๊ทธ๋๋จธ์ค(Programmers)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ์ ๊ท ์์ด๋ ์ถ์ฒ( Level 1, 2021 ์นด์นด์ค ๋ธ๋ผ์ธ๋) (0) | 2021.10.22 |
---|---|
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ์์ ์ํธ( Level 1) (0) | 2021.10.21 |
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ์คํฌํธ๋ฆฌ( Level 2) (0) | 2021.10.21 |
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ์์ธ์์ ๊น์๋ฐฉ ์ฐพ๊ธฐ( Level 1) (0) | 2021.10.21 |
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ์ํธํ๊ฐ( Level 1) (0) | 2021.10.21 |
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ๋ถ์กฑํ ๊ธ์ก ๊ณ์ฐํ๊ธฐ( Level 1) (0) | 2021.10.21 |
๋๊ธ