-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEx01_07.java
More file actions
60 lines (51 loc) ยท 2.1 KB
/
Copy pathEx01_07.java
File metadata and controls
60 lines (51 loc) ยท 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package study.inflearn.lecture02.section01;
/**
* ๋น๋ฐ๋ฒํธ - ์๋ฎฌ๋ ์ด์
& ๊ตฌํ
*
*/
public class Ex01_07 {
public int solution(int[] keypad, String password){
int answer = 0;
char[] pwdChar = password.toCharArray();
// ํคํจ๋ ์ซ์๋ค ๋ฐฐ์ด์ ์ด๊ธฐํ
int[][] keypadArr = new int[3][3];
int index = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
keypadArr[i][j] = keypad[index];
index++;
}
}
// ๋น๋ฐ๋ฒํธ๊ฐ ์
๋ ฅ๋๋ ์ด ์๊ฐ
int first = 0, tempi = 0, tempj = 0;
for (char c : pwdChar) { // ํจ์ค์๋ ์ซ์์ ๋์ผํ ํคํจ๋ ํ์ด ์ฐพ๊ธฐ
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (keypadArr[i][j] == Character.getNumericValue(c)) {
if (first != 0) {
int a = Math.abs(tempi - i);
int b = Math.abs(tempj - j);
int max = Math.max(a, b);
answer += max;
} // ํคํจ๋ ์ฒซ ํจ์ค์๋ 0์ด ์ด๊ธฐ ๋๋ฌธ์ ๊ทธ ๋ค์ ํจ์ค์๋๋ถํฐ ๊ฑธ๋ฆฌ๋ ์๊ฐ ์นด์ดํ
tempi = i;
tempj = j;
first++;
}
}
}
}
return answer;
}
public static void main(String[] args){
Ex01_07 T = new Ex01_07();
System.out.println(T.solution(new int[]{2, 5, 3, 7, 1, 6, 4, 9, 8}, "7596218"));
System.out.println(T.solution(new int[]{1, 5, 7, 3, 2, 8, 9, 4, 6}, "63855526592"));
System.out.println(T.solution(new int[]{2, 9, 3, 7, 8, 6, 4, 5, 1}, "323254677"));
System.out.println(T.solution(new int[]{1, 6, 7, 3, 8, 9, 4, 5, 2}, "3337772122"));
}
}
/*
- char to int : Character.getNumericValue(); // char 7 -> int ๋ฐ๊พธ๋ฉด ์์คํค์ฝ๋๊ฐ์ผ๋ก ๋ณ๊ฒฝ๋๋๋ฐ char 7์ด๋ผ๋ ๋ฌธ์๊ฐ ๊ทธ๋๋ก ์ซ์๋ก ๋ณํํด์ค
- ์ ๋๊ฐ : Math.abs();
*/