์ฒด๊ฐ Level : โ
โ
โ Review: ๋ฌธ์์ด ๊ณต๋ฐฑ ๋ฌธ์ ๋ ์์ฃผ ํ์ด๋ดค์ง๋ง, ๊ณต๋ฐฑ ์๋ ๊ฒฝ์ฐ๋ ์ด์ง ๋ฒ๊ฑฐ๋ก์ ์ง๋ง ๋ฌธ์์ด ๋ฉ์๋๋ฅผ ์ด์ฉํ๋ฉด ์ฝ๊ฒ ํ ์ ์์๋ค! |
๐ก์ซ์๊ฐ ์์ด๋ก ํํ๋์ด์๋ ๋ฌธ์์ด์ ์ซ์๋ก ๋ณํํ๋ ๋ฌธ์ ์ด๋ค. (๊ณต๋ฐฑ์ด ์๋ ๊ฒฝ์ฐ๋ค)
๐ฌ numbers.substring(i).startsWith(str[j]) : numbers ๋ฌธ์์ด i ๋ถํฐ ๋๊น์ง + str[j] ๋ฌธ์์ด๋ก ์์ํ๋์ง ํ์ธํจ.
[์ฑ๊ณต]
๋ฌธ์์ด str2๋ฅผ long ํ์ ์ ์ซ์๋ก ๋ณํํ๋ค.
๋ฆฌํดํ์ ์ Long.parseLong(str2); ์ด๋ ๊ฒ ๋ฐ๊ฟ์ฃผ๋ ํต๊ณผํ๋ค!
class Solution {
public long solution(String numbers) {
long answer = 0;
String[] str = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
String str2 = "";
for (int i = 0; i<numbers.length(); i++){
for (int j = 0; j < str.length; j++){
if(numbers.substring(i).startsWith(str[j])){
str2 += String.valueOf(j) ;
break;
}
}
}
return Long.parseLong(str2);
}
}
[์คํจ] ํ ์คํธ ์ผ์ด์ค ์ฑ๊ณต / 1,3, 9 ์ผ์ด์ค ์คํจ
import java.util.HashMap;
class Solution {
public long solution(String numbers) {
long answer = 0;
String[] str = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
String str2 = "";
for (int i = 0; i<numbers.length(); i++){
for (int j = 0; j < str.length; j++){
if(numbers.substring(i).startsWith(str[j])){
str2 += String.valueOf(j) ;
break;
}
}
}
return Integer.parseInt(str2);
}
}