当前位置:网站首页>Leetcode 1974. Minimum time to type words using a special typewriter (yes, once)
Leetcode 1974. Minimum time to type words using a special typewriter (yes, once)
2022-06-11 16:29:00 【I'm not xiaohaiwa~~~~】

There is a special typewriter , It consists of a The disk And a The pointer form , The disc is marked with lowercase English letters ‘a’ To ‘z’. Only When the pointer points to a letter , It can be typed . The pointer At the beginning Pointing character ‘a’ .

Every second , You can do one of the following :
Put the pointer Clockwise perhaps Anti-clockwise Move a character .
Type pointer At present The character pointed to .
Give you a string word , Please go back and type word Of the word represented least Number of seconds .
Example 1:
Input :word = "abc"
Output :5
explain :
Type words as follows :
- flowers 1 Seconds to type characters 'a' in 1 , Because the pointer initially points to 'a' , Therefore, there is no need to move the pointer .
- flowers 1 Second, move the pointer clockwise to 'b' .
- flowers 1 Seconds to type characters 'b' .
- flowers 1 Second, move the pointer clockwise to 'c' .
- flowers 1 Seconds to type characters 'c' .
Example 2:
Input :word = "bza"
Output :7
explain :
Type words as follows :
- flowers 1 Second, move the pointer clockwise to 'b' .
- flowers 1 Seconds to type characters 'b' .
- flowers 2 Second, move the pointer counterclockwise to 'z' .
- flowers 1 Seconds to type characters 'z' .
- flowers 1 Second, move the pointer clockwise to 'a' .
- flowers 1 Seconds to type characters 'a' .
Example 3:
Input :word = "zjpc"
Output :34
explain :
Type words as follows :
- flowers 1 Second, move the pointer counterclockwise to 'z' .
- flowers 1 Seconds to type characters 'z' .
- flowers 10 Second, move the pointer clockwise to 'j' .
- flowers 1 Seconds to type characters 'j' .
- flowers 6 Second, move the pointer clockwise to 'p' .
- flowers 1 Seconds to type characters 'p' .
- flowers 13 Second, move the pointer counterclockwise to 'c' .
- flowers 1 Seconds to type characters 'c' .
Tips :
- 1 <= word.length <= 100
- word Only lowercase letters .
Main idea : Consider clockwise and counterclockwise each time
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;
}
};
边栏推荐
- Memory image of various data types in C language
- Enterprise purchase, sales and inventory management system based on SSM framework [source code + database + design]
- JDBC debugging error, ask for guidance
- PyQt5 使QPlainTextEdit控件支持行号显示
- Laravel 2020-01-01t00:00:00.000000z date conversion
- 问题 AC: 中国象棋中的跳马问题
- 最近7天,最近10天,最近90天时间处理逻辑
- DHCP协议实例化分析
- 面试经典题目:怎么做的性能测试?【杭州多测师】【杭州多测师_王sir】
- 项目经理如何击退被工作汇报支配的恐惧感?
猜你喜欢
![[sword finger offer] 21 Adjust array order so that odd numbers precede even numbers](/img/ba/8fa84520bacbc56ce7cbe02ee696c8.png)
[sword finger offer] 21 Adjust array order so that odd numbers precede even numbers

Leetcode 1974. 使用特殊打字机键入单词的最少时间(可以,一次过)

R1 Quick Open Pressure Vessel Operation test Library and Simulation Test in 2022

利用 MATLAB 和 DCRAW 处理数码相机 RAW 文件的完整流程

Student website template brown cake dessert website design - brown cake dessert shop (4 pages) gourmet dessert website production final assignment finished product_ Fresh fruit web design final assign

开关电源电路图及原理12v分析-详细版

2022G1工业锅炉司炉考题及模拟考试

How can the project manager repel the fear of being dominated by work reports?

Customized thread communication (lock) of JUC

【opencvsharp】opencvsharp_samples.core示例代码笔记
随机推荐
Production problem troubleshooting reference
What if you can't access the desktop after the computer is turned on
[ISITDTU 2019]EasyPHP
WGet command use
[golang] leetcode special training - array and slice
laravel 2020-01-01T00:00:00.000000Z 日期转化
(OJ assignment of Hunan University of science and Technology) problem g: pattern matching of strings
wget命令使用
[从零开始学习FPGA编程-17]:快速入门篇 - 操作步骤2-5- VerilogHDL硬件描述语言符号系统与程序框架(软件程序员和硬件工程师都能看懂)
Go语言之Go 快速入门篇(一):第一个 Go 程序
Complete test process [Hangzhou multi tester] [Hangzhou multi tester \wang Sir]
2022年R1快开门式压力容器操作考试题库及模拟考试
Laravel 8 realizes database backup through task scheduling
List和Dict数据类型作用详解
基于ssm框架实现的企业进销存管理系统【源码+数据库+毕设】
药物评价指标
Princeton Dengjia student's personal account: must I have a doctorate? No, I can also be an applied scientist in a large factory as an undergraduate
Will you be punished for not wearing seat belts in the back row?
Step 4 of installation in RF: an error is reported when installing the robotframework-selenium 2library
[LeetCode每日一题] |686.重复叠加字符串匹配