From 34f4a7b7ba3c240db160d710df054c0f8b7732ab Mon Sep 17 00:00:00 2001 From: yqq Date: Sun, 19 Sep 2021 18:13:54 +0800 Subject: [PATCH] =?UTF-8?q?925.=E9=95=BF=E6=8C=89=E9=94=AE=E5=85=A5=20,=20?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0Golang=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0925.长按键入.md | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/problems/0925.长按键入.md b/problems/0925.长按键入.md index 3502f2fb..70597508 100644 --- a/problems/0925.长按键入.md +++ b/problems/0925.长按键入.md @@ -134,7 +134,7 @@ Python: class Solution: def isLongPressedName(self, name: str, typed: str) -> bool: i, j = 0, 0 - m, n = len(name) , len(typed) + m, n = len(name) , len(typed) while i< m and j < n: if name[i] == typed[j]: # 相同时向后匹配 i += 1 @@ -155,8 +155,32 @@ class Solution: else: return False return True ``` + Go: +```go + +func isLongPressedName(name string, typed string) bool { + if(name[0] != typed[0] || len(name) > len(typed)) { + return false; + } + + idx := 0 // name的索引 + var last byte // 上个匹配字符 + for i := 0; i < len(typed); i++ { + if idx < len(name) && name[idx] == typed[i] { + last = name[idx] + idx++ + } else if last == typed[i] { + continue + } else { + return false + } + } + return idx == len(name) +} +``` + JavaScript: -----------------------