题解 | 手机键盘
手机键盘
https://www.nowcoder.com/practice/20082c12f1ec43b29cd27c805cd476cd
def solution(s):
keys = "22233344455566677778889999"
# 每个字母在键上的位置(第几次按键)
press = [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3,
1, 2, 3, 4, 1, 2, 3, 1, 2, 3, 4]
total = 0
prev_key = ''
for ch in s:
idx = ord(ch) - ord('a')
cur_key = keys[idx]
total += press[idx]
if cur_key == prev_key:
total += 2
prev_key = cur_key
return total
while True:
try:
s = input()
print(solution(s))
except :
break
查看21道真题和解析