C# 숫자 문자열과 영단어
2022. 3. 22. 13:47ㆍ코딩테스트 문제 풀이/[프로그래머스]
728x90
반응형
using System;
using System.Collections.Generic;
public class Solution {
public int solution(string s)
{
Dictionary<string, int> num = new Dictionary<string, int>();
num.Add("zero", 0);
num.Add("one", 1);
num.Add("two", 2);
num.Add("three", 3);
num.Add("four", 4);
num.Add("five", 5);
num.Add("six", 6);
num.Add("seven", 7);
num.Add("eight", 8);
num.Add("nine", 9);
string s2 = "";
foreach (var item in num)
{
s = s.Replace(item.Key.ToString(), item.Value.ToString());
}
int answer = Convert.ToInt32(s);
return answer;
}
}
문제풀이 :
문자열 영단어 하나당 하나의 숫자로 대응되기 때문에 Dictionary를 사용하였다.
728x90
반응형
'코딩테스트 문제 풀이 > [프로그래머스]' 카테고리의 다른 글
[PCCP 기출문제] 2번 / 퍼즐 게임 챌린지 (0) | 2024.10.26 |
---|---|
[PCCP 기출문제] 1번 / 동영상 재생기 (0) | 2024.10.25 |
C# 신고 결과 받기 (0) | 2022.03.22 |