-
Notifications
You must be signed in to change notification settings - Fork 0
/
TapKeypad.swift
84 lines (76 loc) · 1.67 KB
/
TapKeypad.swift
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import Foundation
func convert(_ numbers: [Int]) -> [[Int]] {
var result: [[Int]] = []
for i in numbers {
switch i {
case 1:
result.append([0, 0])
case 2:
result.append([0, 1])
case 3:
result.append([0, 2])
case 4:
result.append([1, 0])
case 5:
result.append([1, 1])
case 6:
result.append([1, 2])
case 7:
result.append([2, 0])
case 8:
result.append([2, 1])
case 9:
result.append([2, 2])
case 0:
result.append([3, 1])
default:
break
}
}
return result
}
func solution키패드누르기(_ numbers:[Int], _ hand:String) -> String {
let keyPad = [[0, 0], [0, 1], [0, 2],
[1, 0], [1, 1], [1, 2],
[2, 0], [2, 1], [2, 2],
[3, 0], [3, 1], [3, 2]]
var rightH = [3, 2]
var leftH = [3, 0]
var result = ""
var input = convert(numbers)
for i in input {
switch i {
case [0, 0], [1, 0], [2, 0]:
leftH = i
result += "L"
case [0, 2], [1, 2], [2, 2]:
rightH = i
result += "R"
case [0, 1], [1, 1], [2, 1], [3, 1]:
let rDiff = abs(i[0] - rightH[0]) + abs(i[1] - rightH[1])
let lDiff = abs(i[0] - leftH[0]) + abs(i[1] - leftH[1])
if rDiff > lDiff {
leftH = i
result += "L"
}
if lDiff > rDiff {
rightH = i
result += "R"
}
if lDiff == rDiff {
switch hand {
case "right":
rightH = i
result += "R"
default:
leftH = i
result += "L"
}
}
default:
break
}
}
return result
}
//solution([1, 3, 4, 5, 8, 2, 1, 4, 5, 9, 5], "right")