-
Notifications
You must be signed in to change notification settings - Fork 4
Open
Labels
Description
题目地址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
};