Leetcode 1629. Slowest Key

2021-07-23 16:22:45 浏览数 (1)

1. Description

2. Solution

**解析:**Version 1,遍历每个字符,计算其时间间隔,如果比之前的大,则更新时间间隔及字符,如果相等,则比较字符大小,判断是否更新字符。

  • Version 1
代码语言:javascript复制
class Solution:
    def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:
        n = len(keysPressed)
        key = keysPressed[0]
        time = releaseTimes[0]
        for i in range(1, n):
            temp = releaseTimes[i] - releaseTimes[i-1]
            if temp > time:
                key = keysPressed[i]
                time = temp
            elif temp == time and keysPressed[i] > key:
                key = keysPressed[i]
        return key

Reference

  1. https://leetcode.com/problems/slowest-key/

0 人点赞