From 589a6a97298746fe52797f4e79f818b9494efa6c Mon Sep 17 00:00:00 2001 From: Huy Vo Date: Sun, 7 Jun 2020 14:09:37 -0400 Subject: [PATCH 1/3] [Stack] Using popLast (no peeking). --- Stack/ValidParentheses.swift | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Stack/ValidParentheses.swift b/Stack/ValidParentheses.swift index 302ee9f2..4784419d 100644 --- a/Stack/ValidParentheses.swift +++ b/Stack/ValidParentheses.swift @@ -12,15 +12,15 @@ class ValidParentheses { if char == "(" || char == "[" || char == "{" { stack.append(char) } else if char == ")" { - guard stack.count != 0 && stack.removeLast() == "(" else { + guard stack.popLast() == "(" else { return false } } else if char == "]" { - guard stack.count != 0 && stack.removeLast() == "[" else { + guard stack.popLast() == "[" else { return false } } else if char == "}" { - guard stack.count != 0 && stack.removeLast() == "{" else { + guard stack.popLast() == "{" else { return false } } @@ -28,4 +28,4 @@ class ValidParentheses { return stack.isEmpty } -} \ No newline at end of file +} From 28e609e80fd2384cb3d2707de44f350b3c65e629 Mon Sep 17 00:00:00 2001 From: Huy Vo Date: Mon, 8 Jun 2020 12:48:56 -0400 Subject: [PATCH 2/3] [Array] 'inout' before a parameter name is not allowed, place it before parameter type --- Array/RemoveDuplicatesFromSortedArray.swift | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/Array/RemoveDuplicatesFromSortedArray.swift b/Array/RemoveDuplicatesFromSortedArray.swift index af593d3c..9e2f7036 100644 --- a/Array/RemoveDuplicatesFromSortedArray.swift +++ b/Array/RemoveDuplicatesFromSortedArray.swift @@ -6,18 +6,17 @@ */ class RemoveDuplicatesFromSortedArray { - func removeDuplicates(inout nums: [Int]) -> Int { - guard nums.count > 0 else { - return 0 - } - - var index = 0 + func removeDuplicates(_ nums: inout [Int]) -> Int { + if nums.isEmpty { return 0 } - for num in nums where num != nums[index] { - index += 1 - nums[index] = num + var i = 0 + for j in 1.. Date: Mon, 8 Jun 2020 14:59:01 -0400 Subject: [PATCH 3/3] using shorthand arguments --- Array/RemoveElement.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Array/RemoveElement.swift b/Array/RemoveElement.swift index 84162ac8..dab9443a 100644 --- a/Array/RemoveElement.swift +++ b/Array/RemoveElement.swift @@ -6,8 +6,8 @@ */ class RemoveElement { - func removeElement(inout nums: [Int], _ val: Int) -> Int { - nums = nums.filter { (num) in num != val } + func removeElement(_ nums: inout [Int], _ val: Int) -> Int { + nums = nums.filter { $0 != val } return nums.count } -} \ No newline at end of file +}