Deep_Dev
article thumbnail

๋ฌธ์ œ ์„ค๋ช…

์˜์–ด๊ฐ€ ์‹ซ์€ ๋จธ์“ฑ์ด๋Š” ์˜์–ด๋กœ ํ‘œ๊ธฐ๋˜์–ด์žˆ๋Š” ์ˆซ์ž๋ฅผ ์ˆ˜๋กœ ๋ฐ”๊พธ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด 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);
    }
}