原文链接: https://leetcode-cn.com/problems/minimum-time-to-type-word-using-special-typewriter
英文原文
There is a special typewriter with lowercase English letters 'a'
to 'z'
arranged in a circle with a pointer. A character can only be typed if the pointer is pointing to that character. The pointer is initially pointing to the character 'a'
.
Each second, you may perform one of the following operations:
- Move the pointer one character counterclockwise or clockwise.
- Type the character the pointer is currently on.
Given a string word
, return the minimum number of seconds to type out the characters in word
.
Example 1:
Input: word = "abc" Output: 5 Explanation: The characters are printed as follows: - Type the character 'a' in 1 second since the pointer is initially on 'a'. - Move the pointer clockwise to 'b' in 1 second. - Type the character 'b' in 1 second. - Move the pointer clockwise to 'c' in 1 second. - Type the character 'c' in 1 second.
Example 2:
Input: word = "bza" Output: 7 Explanation: The characters are printed as follows: - Move the pointer clockwise to 'b' in 1 second. - Type the character 'b' in 1 second. - Move the pointer counterclockwise to 'z' in 2 seconds. - Type the character 'z' in 1 second. - Move the pointer clockwise to 'a' in 1 second. - Type the character 'a' in 1 second.
Example 3:
Input: word = "zjpc" Output: 34 Explanation: The characters are printed as follows: - Move the pointer counterclockwise to 'z' in 1 second. - Type the character 'z' in 1 second. - Move the pointer clockwise to 'j' in 10 seconds. - Type the character 'j' in 1 second. - Move the pointer clockwise to 'p' in 6 seconds. - Type the character 'p' in 1 second. - Move the pointer counterclockwise to 'c' in 13 seconds. - Type the character 'c' in 1 second.
Constraints:
1 <= word.length <= 100
word
consists of lowercase English letters.
中文题目
有一个特殊打字机,它由一个 圆盘 和一个 指针 组成, 圆盘上标有小写英文字母 '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
只包含小写英文字母。
通过代码
高赞题解
使用特殊打字机键入单词的最少时间
第一道题。
对于一个字母,要么顺时针转过去,要么逆时针转过去。
第一题还是拼手速的
模拟
class Solution {
public:
int minTimeToType(string word) {
char tmp = 'a';
int ans = 0;
for(char ch: word){
int t = abs(tmp - ch);
ans += min(t, 26 - t) + 1; //+1是键入时间,min里面判断的是顺时针和逆时针转的时间
tmp = ch; //当前停留的字母
}
return ans;
}
};
统计信息
通过次数 | 提交次数 | AC比率 |
---|---|---|
4459 | 6083 | 73.3% |
提交历史
提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
---|