From ee54db5b5dbbac4196b94e7f007a7f4cd3ddc8aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=99=E6=9D=9C=E6=9E=97?= Date: Sun, 8 Aug 2021 16:51:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A00977.=E6=9C=89=E5=BA=8F?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E7=9A=84=E5=B9=B3=E6=96=B9=20Swift=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0977.有序数组的平方.md | 30 ++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/problems/0977.有序数组的平方.md b/problems/0977.有序数组的平方.md index 0f9007d7..7206fd6f 100644 --- a/problems/0977.有序数组的平方.md +++ b/problems/0977.有序数组的平方.md @@ -224,6 +224,36 @@ const sortedSquares = function (nums) { } ``` +Swift: + +```swift +func sortedSquares(_ nums: [Int]) -> [Int] { + // 指向新数组最后一个元素 + var k = nums.count - 1 + // 指向原数组第一个元素 + var i = 0 + // 指向原数组最后一个元素 + var j = nums.count - 1 + // 初始化新数组(用-1填充) + var result = Array(repeating: -1, count: nums.count) + + for _ in 0..