题解 | 手机键盘

手机键盘

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'))

双指针法记录前驱组的位置,判断是否要等待

全部评论

相关推荐

03-04 07:14
门头沟学院 C++
黑皮白袜臭脚体育生:老板:都给工作机会了还想要工资,哪来这么多好事
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务