Skip to content

leetcode-1165 单行键盘 #109

@GuoLizhi

Description

@GuoLizhi

题目地址https://leetcode-cn.com/problems/single-row-keyboard/

1.哈希表

时间复杂度O(n) 空间复杂度O(1)

var calculateTime = function(keyboard, word) {
    let prev = 0
    let result = 0
    let map = {}
    for (let i = 0; i < keyboard.length; i++) {
        map[keyboard[i]] = i
    }
    for (let i = 0; i < word.length; i++) {
        result += Math.abs(map[word[i]] - prev)
        prev = map[word[i]]
    }
    return result
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions