zoukankan      html  css  js  c++  java
  • [Swift]LeetCode1165. 单行键盘 | Single-Row Keyboard

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(shanqingyongzhi)
    ➤博客园地址:山青咏芝(www.zengqiang.org
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址:https://www.cnblogs.com/strengthen/p/11407048.html 
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

    There is a special keyboard with all keys in a single row.

    Given a string keyboard of length 26 indicating the layout of the keyboard (indexed from 0 to 25), initially your finger is at index 0. To type a character, you have to move your finger to the index of the desired character. The time taken to move your finger from index i to index j is |i - j|.

    You want to type a string word. Write a function to calculate how much time it takes to type it with one finger. 

    Example 1:

    Input: keyboard = "abcdefghijklmnopqrstuvwxyz", word = "cba"
    Output: 4
    Explanation: The index moves from 0 to 2 to write 'c' then to 1 to write 'b' then to 0 again to write 'a'.
    Total time = 2 + 1 + 1 = 4. 
    

    Example 2:

    Input: keyboard = "pqrstuvwxyzabcdefghijklmno", word = "leetcode"
    Output: 73 

    Constraints:

    • keyboard.length == 26
    • keyboard contains each English lowercase letter exactly once in some order.
    • 1 <= word.length <= 10^4
    • word[i] is an English lowercase letter.

    我们定制了一款特殊的力扣键盘,所有的键都排列在一行上。

    我们可以按从左到右的顺序,用一个长度为 26 的字符串 keyboard (索引从 0 开始,到 25 结束)来表示该键盘的键位布局。

    现在需要测试这个键盘是否能够有效工作,那么我们就需要个机械手来测试这个键盘。

    最初的时候,机械手位于左边起第一个键(也就是索引为 0 的键)的上方。当机械手移动到某一字符所在的键位时,就会在终端上输出该字符。

    机械手从索引 i 移动到索引 j 所需要的时间是 |i - j|

    当前测试需要你使用机械手输出指定的单词 word,请你编写一个函数来计算机械手输出该单词所需的时间。 

    示例 1:

    输入:keyboard = "abcdefghijklmnopqrstuvwxyz", word = "cba"
    输出:4
    解释:
    机械手从 0 号键移动到 2 号键来输出 'c',又移动到 1 号键来输出 'b',接着移动到 0 号键来输出 'a'。
    总用时 = 2 + 1 + 1 = 4. 
    

    示例 2:

    输入:keyboard = "pqrstuvwxyzabcdefghijklmno", word = "leetcode"
    输出:73 

    提示:

    • keyboard.length == 26
    • keyboard 按某种特定顺序排列,并包含每个小写英文字母一次。
    • 1 <= word.length <= 10^4
    • word[i] 是一个小写英文字母

    Runtime: 24 ms
    Memory Usage: 21.1 MB
     1 class Solution {
     2     func calculateTime(_ keyboard: String, _ word: String) -> Int {
     3         let arr:[Character] = Array(keyboard)
     4         var map:[Character:Int] = [Character:Int]()
     5         for i in 0..<arr.count
     6         {
     7             map[arr[i]] = i
     8         }
     9         var currPos:Int = 0
    10         var totalTime:Int = 0
    11         for c in word
    12         {
    13             totalTime += abs(currPos - map[c]!)
    14             currPos = map[c]!
    15         }
    16         return totalTime        
    17     }
    18 }
  • 相关阅读:
    在spring中该如何使用DTO,以及DTO和Entity的关系
    AngularJs踩过的坑
    springMVC正确使用GET POST PUT和DELETE方法,如何传递参数
    Mac下使用终端连接远程使用ssh协议的git服务器
    springMVC的异常处理
    根据业务规则分析业务对象,然后生成表结构
    在线资源--图片/json等
    js中的Hook
    diamond源码阅读-目录监控
    maven常用命令
  • 原文地址:https://www.cnblogs.com/strengthen/p/11407048.html
Copyright © 2011-2022 走看看