21.05.20 기록
-
- 내가 푼 10809 풀이 (메모리 15.8MB, 시간 148ms로 통과)
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; public class B10809 { public static void main(String[] args) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { byte[] word = br.readLine().getBytes(StandardCharsets.UTF_8); char[] alphabet = new char[26]; int[] res = new int[alphabet.length]; for(int i = 0; i < alphabet.length; i++) { alphabet[i] = (char) (97 + i); res[i] = -1; } for(int i = 0; i < word.length; i++) { for(int j = 0; j < alphabet.length; j++) { if(res[j] != -1) continue; if((char)word[i] == alphabet[j]) { res[j] = i; } } } for(int i : res) { System.out.print(i + " "); } } catch (IOException e) { e.printStackTrace(); } } }- 통과는 했으나 더 좋은 방법이 분명 있을 것 같아서 바로 풀이를 찾아보았다.
아래는 풀이코드이다. (메모리 15.8MB, 시간 144ms로 통과)
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class B10809 { public static void main(String[] args) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { String word = br.readLine(); int[] check = new int[26]; for(int i = 0; i < check.length; i++) { check[i] = -1; } for(int i = 0; i < word.length(); i++) { char c = word.charAt(i); if(check[c - 'a'] == -1) { check[c - 'a'] = i; } } for(int i : check) { System.out.print(i + " "); } } catch (IOException e) { e.printStackTrace(); } } }- 원래 내 코드와 비교해보니 풀이 코드는 입력을 String으로 받아서 charAt을 사용했다. 그리고 charAt으로 꺼낸 요소를 ‘a’로 빼줬을 때의 값을 인덱스로 사용하였다. (만약 요소가 a 였으면 인덱스 값은 0이 된다.)
- 내가 푼 10809 풀이 (메모리 15.8MB, 시간 148ms로 통과)