From 343ddb37f9e84284ff8df34f89bed18522244e43 Mon Sep 17 00:00:00 2001 From: bqlin Date: Mon, 20 Dec 2021 20:13:42 +0800 Subject: [PATCH] =?UTF-8?q?1047.=E5=88=A0=E9=99=A4=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E4=B8=AD=E7=9A=84=E6=89=80=E6=9C=89=E7=9B=B8=E9=82=BB?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E9=A1=B9=EF=BC=9A=E4=BC=98=E5=8C=96Swift?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1047.删除字符串中的所有相邻重复项.md | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/problems/1047.删除字符串中的所有相邻重复项.md b/problems/1047.删除字符串中的所有相邻重复项.md index d6eefd07..9309aed8 100644 --- a/problems/1047.删除字符串中的所有相邻重复项.md +++ b/problems/1047.删除字符串中的所有相邻重复项.md @@ -322,14 +322,12 @@ char * removeDuplicates(char * s){ Swift: ```swift func removeDuplicates(_ s: String) -> String { - let array = Array(s) var stack = [Character]() - for c in array { - let last: Character? = stack.last - if stack.isEmpty || last != c { - stack.append(c) - } else { + for c in s { + if stack.last == c { stack.removeLast() + } else { + stack.append(c) } } return String(stack)