题解 | 手机键盘
手机键盘
https://www.nowcoder.com/practice/20082c12f1ec43b29cd27c805cd476cd?tpId=40&tqId=21337&rp=1&difficulty=&judgeStatus=&tags=/question-ranking
import sys
def func(s):
alphbet=['abc','def','ghi','jkl','mno','pqrs','tuv','wxyz']
count=0
pregroup=-1
for i in s:
for group in alphbet:
if i in group:
if alphbet.index(group)!=pregroup:
loc=group.index(i)
pregroup=alphbet.index(group)
press=loc+1
count+=press
else:
count+=2
loc=group.index(i)
pregroup=alphbet.index(group)
press=loc+1
count+=press
print(count)
for line in sys.stdin:
func(line.strip('\n'))
双指针法记录前驱组的位置,判断是否要等待

查看9道真题和解析