๋ฌธ์ ์ค๋ช
์์ด๊ฐ ์ซ์ ๋จธ์ฑ์ด๋ ์์ด๋ก ํ๊ธฐ๋์ด์๋ ์ซ์๋ฅผ ์๋ก ๋ฐ๊พธ๋ ค๊ณ ํฉ๋๋ค. ๋ฌธ์์ด numbers๊ฐ ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง ๋, numbers๋ฅผ ์ ์๋ก ๋ฐ๊ฟ return ํ๋๋ก solution ํจ์๋ฅผ ์์ฑํด ์ฃผ์ธ์.
์ ํ์ฌํญ
- numbers๋ ์๋ฌธ์๋ก๋ง ๊ตฌ์ฑ๋์ด ์์ต๋๋ค.
- numbers๋ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" ๋ค์ด ๊ณต๋ฐฑ ์์ด ์กฐํฉ๋์ด ์์ต๋๋ค.
- 1 ≤ numbers์ ๊ธธ์ด ≤ 50
- "zero"๋ numbers์ ๋งจ ์์ ์ฌ ์ ์์ต๋๋ค.
์ ์ถ๋ ฅ ์ (numbers / result )
"onetwothreefourfivesixseveneightnine" | 123456789 |
"onefourzerosixseven" | 14067 |
ํ ์คํธ์ผ์ด์ค๋ ๋ค ๋ง์๋๋ฐ, ์๊พธ ์ฑ์ ํ๋ฉด ์์ธ์ผ์ด์ค์์ ๋ช๊ฐ ๋ฐํ์์๋ฌ ๋ ์
๋๋์ฒด ๋ญ๊ฐ ํ๋๋ ๋ฌธ์ ์์ฒด์์ long type์ผ๋ก ๋ฐํํ๋ฌ๋๋ฐ
๋ด๊ฐ Integer.parseInt๋ก ๋ฐํํด์ ......
public class Solution {
public long solution(String numbers) {
String[] s={"zero","one","two","three","four","five","six","seven","eight","nine"};
String[] n={"0","1","2","3","4","5","6","7","8","9"};
String temp="";
String result= "";
for(char x:numbers.toCharArray()){ // ๋ฌธ์์ด ํ๋์ฉ
temp+=x; // temp์ ์ง์ด๋ฃ์ผ๋ฉด์
for(int i=0; i<s.length; i++){
if(temp.equals(s[i])){ // ๋ฐฐ์ด S์ ๊ฐ์๊ฒ์ด ์๊ฒ ๋๋ค๋ฉด
result+=n[i]; // result์ ํด๋น index๋ฅผ ๋ฐฐ์ดn์์ ๊ฐ์ ธ๊ฐ๋ค
temp=""; // temp๋ ์ด๊ธฐํ
}
}
}
return Long.parseLong(result);
}
}
'๐ ์ฝ๋ฉํ ์คํธ > ๋ฐฑ์ค & ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค][JAVA]Level 0 : ์ ์ฃผ์ ์ซ์ 3 (0) | 2022.10.29 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค][JAVA]Level 0 : ๊ณต ๋์ง๊ธฐ (0) | 2022.10.28 |
[ํ๋ก๊ทธ๋๋จธ์ค][JAVA]Level 0 : ๋ชจ์ค๋ถํธ(1) (0) | 2022.10.26 |
[ํ๋ก๊ทธ๋๋จธ์ค][JAVA]Level 1 : ์ต์ ์ง์ฌ๊ฐํ (0) | 2022.10.26 |
[ํ๋ก๊ทธ๋๋จธ์ค][JAVA]Level 0 : ์ง๋ฃ์์ ์ ํ๊ธฐ (0) | 2022.10.26 |