当前位置:网站首页>Leetcode 1974. 使用特殊打字机键入单词的最少时间(可以,一次过)
Leetcode 1974. 使用特殊打字机键入单词的最少时间(可以,一次过)
2022-06-11 16:12:00 【我不是萧海哇~~~~】

有一个特殊打字机,它由一个 圆盘 和一个 指针 组成, 圆盘上标有小写英文字母 ‘a’ 到 ‘z’。只有 当指针指向某个字母时,它才能被键入。指针 初始时 指向字符 ‘a’ 。

每一秒钟,你可以执行以下操作之一:
将指针 顺时针 或者 逆时针 移动一个字符。
键入指针 当前 指向的字符。
给你一个字符串 word ,请你返回键入 word 所表示单词的 最少 秒数 。
示例 1:
输入:word = "abc"
输出:5
解释:
单词按如下操作键入:
- 花 1 秒键入字符 'a' in 1 ,因为指针初始指向 'a' ,故不需移动指针。
- 花 1 秒将指针顺时针移到 'b' 。
- 花 1 秒键入字符 'b' 。
- 花 1 秒将指针顺时针移到 'c' 。
- 花 1 秒键入字符 'c' 。
示例 2:
输入:word = "bza"
输出:7
解释:
单词按如下操作键入:
- 花 1 秒将指针顺时针移到 'b' 。
- 花 1 秒键入字符 'b' 。
- 花 2 秒将指针逆时针移到 'z' 。
- 花 1 秒键入字符 'z' 。
- 花 1 秒将指针顺时针移到 'a' 。
- 花 1 秒键入字符 'a' 。
示例 3:
输入:word = "zjpc"
输出:34
解释:
单词按如下操作键入:
- 花 1 秒将指针逆时针移到 'z' 。
- 花 1 秒键入字符 'z' 。
- 花 10 秒将指针顺时针移到 'j' 。
- 花 1 秒键入字符 'j' 。
- 花 6 秒将指针顺时针移到 'p' 。
- 花 1 秒键入字符 'p' 。
- 花 13 秒将指针逆时针移到 'c' 。
- 花 1 秒键入字符 'c' 。
提示:
- 1 <= word.length <= 100
- word 只包含小写英文字母。
主要思路:每次考虑顺时针和逆时针即可
Code:
class Solution {
public:
int minTimeToType(string word) {
// char str[26]={'a','b','c','d','e'};
int res=0;
for(int i=0;i<word.size();i++)
{
if(i==0)
{
int minnum=min(word[i]-'a',abs(26-(word[i]-'a')));
res+=minnum;
res++;
}
else
{
int minnum=min(abs(word[i]-word[i-1]),abs(26-(abs(word[i]-word[i-1]))));
res+=minnum;
res++;
}
}
return res;
}
};
边栏推荐
- JDBC debugging error, ask for guidance
- 什么是泛型?为什么要使用泛型?泛型怎么用?那包装类呢?
- How can the project manager repel the fear of being dominated by work reports?
- Detailed explanation of opengauss multi thread architecture startup process
- DHCP协议实例化分析
- How to optimize the performance of compose? Find the answer through "underlying principles" | developers say · dtalk
- Graduation project wechat applet OUC campus navigation (I) topic selection and opening report
- leetcode785. 判断二分图(中等)
- Ai4db: AI slow SQL root cause analysis
- Zhenxiang, Huawei gives n+1 for voluntary resignation
猜你喜欢

Laravel listening mode

Heartless sword English Chinese bilingual poem 001 Spring outing

Talk about data center network again

If you want to learn ArrayList well, it is enough to read this article

Classmate, have you heard of mot?

什么是泛型?为什么要使用泛型?泛型怎么用?那包装类呢?

What if you can't access the desktop after the computer is turned on

leetcode684. Redundant connection (medium)
![[sword finger offer] 22 The penultimate node in the linked list](/img/66/630ae9762f9d87817a14cb1c96015b.png)
[sword finger offer] 22 The penultimate node in the linked list

Laravel 2020-01-01t00:00:00.000000z date conversion
随机推荐
Question ac: Horse Vaulting in Chinese chess
Enterprise purchase, sales and inventory management system based on SSM framework [source code + database + design]
laravel 8 使用passport 进行Auth验证及颁发token
完整的测试流程【杭州多测师】【杭州多测师_王sir】
Database resource load management (Part 2)
Search and graph theory: Dijkstra finding the shortest path i-dijkstra (shortest path)
[learn FPGA programming from scratch -18]: quick start chapter - operation steps 2-6- VerilogHDL sequential circuit syntax analysis (taking the counter as an example)
09-最小生成树 公路村村通
Yiwenjiaohui your database system tuning
PostgreSQL create database
What is a generic? Why use generics? How do I use generics? What about packaging?
基于ssm框架实现的企业进销存管理系统【源码+数据库+毕设】
Graduation project wechat applet OUC campus navigation (I) topic selection and opening report
postgresql源码编译
【sql语句基础】——删(delete) /改(update)
PostgreSQL startup process
Project workspace creation steps - Zezhong ar automated test tool
(湖南科技大学oj作业)问题 G: 串的模式匹配
What happened to the frequent disconnection of the computer at home
[sword finger offer] 22 The penultimate node in the linked list