当前位置:网站首页>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~~~~

 Insert picture description here
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’ .

 Insert picture description here
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;
    }
};
原网站

版权声明
本文为[I'm not xiaohaiwa~~~~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111612218195.html