From c33f8452a881236e5fabc09fb5c30a0c962cd593 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=81=E5=AE=A2=E5=AD=A6=E4=BC=9F?= Date: Fri, 10 Sep 2021 17:21:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20344.=E5=8F=8D=E8=BD=AC?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=20Swift=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0344.反转字符串.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/problems/0344.反转字符串.md b/problems/0344.反转字符串.md index a5f18d58..a02c27d5 100644 --- a/problems/0344.反转字符串.md +++ b/problems/0344.反转字符串.md @@ -206,6 +206,7 @@ var reverseString = function(s) { Swift: ```swift +// 双指针 - 元组 func reverseString(_ s: inout [Character]) { var l = 0 var r = s.count - 1 @@ -216,11 +217,18 @@ func reverseString(_ s: inout [Character]) { r -= 1 } } + +// 双指针法 - 库函数 +func reverseString(_ s: inout [Character]) { + var j = s.count - 1 + for i in 0 ..< Int(Double(s.count) * 0.5) { + s.swapAt(i, j) + j -= 1 + } +} ``` - - ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321)