Skip to content

Commit 7235f24

Browse files
committed
Update for swift 3.0
1 parent ceb9d5b commit 7235f24

25 files changed

+61
-58
lines changed

leetcode-swift.xcodeproj/project.pbxproj

+13-8
Original file line numberDiff line numberDiff line change
@@ -236,16 +236,19 @@
236236
B76B78591D2A2DBE0093657C /* q007-reverse-integer.swift */,
237237
B76B78571D2A24630093657C /* q009-palindrome-number.swift */,
238238
B76B77E51D281C7C0093657C /* q013-roman-to-integer.swift */,
239+
B7E9B29F1D2EA75500D78ECC /* q014-longest-common-prefix.swift */,
239240
B7B3207C1D2BAA5C004920E2 /* q019-remove-nth-node-from-end-of-list.swift */,
240241
B7B320811D2BC6B0004920E2 /* q020-valid-parentheses.swift */,
241242
B76B77E61D281C7C0093657C /* q021-merge-two-sorted-lists.swift */,
242243
B76B77E71D281C7C0093657C /* q024-swap-nodes-in-pairs.swift */,
243244
B76B78511D2A0B2A0093657C /* q026-remove-duplicates-from-sorted-array.swift */,
244245
B76B78451D294D910093657C /* q027-remove-element.swift */,
246+
B7E9B2A31D2F4B1E00D78ECC /* q028-implement-strstr.swift */,
245247
B76B785E1D2A5C330093657C /* q036-valid-sudoku.swift */,
246248
B7E9B2951D2CB1A200D78ECC /* q038-count-and-say.swift */,
247249
B7E9B2971D2CC3D600D78ECC /* q058-length-of-last-word.swift */,
248250
B76B78491D2974370093657C /* q066-plus-one.swift */,
251+
B7E9B2A11D2EAEA900D78ECC /* q067-add-binary.swift */,
249252
B76B77E81D281C7C0093657C /* q070-climbing-stairs.swift */,
250253
B76B77E91D281C7C0093657C /* q083-remove-duplicates-from-sorted-list.swift */,
251254
B7B320781D2B8A77004920E2 /* q088-merge-sorted-array.swift */,
@@ -265,11 +268,13 @@
265268
B76B77EE1D281C7C0093657C /* q160-majority-element.swift */,
266269
B76B77EF1D281C7C0093657C /* q171-excel-sheet-column-number.swift */,
267270
B76B78531D2A0EB00093657C /* q172-factorial-trailing-zeroes.swift */,
271+
B7E9B29B1D2CE93D00D78ECC /* q190-reverse-bits.swift */,
268272
B76B77F01D281C7C0093657C /* q191-number-of-1-bits.py */,
269273
B76B77F11D281C7C0093657C /* q191-number-of-1-bits.swift */,
270274
B76B78371D28B3A90093657C /* q198-house-robber.swift */,
271275
B76B77F21D281C7C0093657C /* q202-happy-number.swift */,
272276
B7E9B2991D2CD6DD00D78ECC /* q203-remove-linked-list-elements.swift */,
277+
B70C2AAF1D30E810002AC496 /* q204-count-primes.swift */,
273278
B7B320761D2B6DD0004920E2 /* q205-isomorphic-strings.swift */,
274279
B76B77F31D281C7C0093657C /* q206-reverse-linked-list.swift */,
275280
B76B77F41D281C7C0093657C /* q217-contains-duplicate.swift */,
@@ -279,6 +284,7 @@
279284
B76B77F51D281C7C0093657C /* q226-invert-binary-tree.swift */,
280285
B76B77F61D281C7C0093657C /* q231-power-of-two.swift */,
281286
B76B784D1D2A017F0093657C /* q232-implement-queue-using-stacks.swift */,
287+
B7E9B29D1D2E5C4600D78ECC /* q234-palindrome-linked-list.swift */,
282288
B76B77F71D281C7C0093657C /* q235-lowest-common-ancestor-of-a-binary-search-tree.py */,
283289
B76B77F81D281C7C0093657C /* q235-lowest-common-ancestor-of-a-binary-search-tree.swift */,
284290
B76B77F91D281C7C0093657C /* q237-delete-node-in-a-linked-list.py */,
@@ -291,20 +297,14 @@
291297
B7B3207E1D2BB75E004920E2 /* q290-word-pattern.swift */,
292298
B76B77FF1D281C7C0093657C /* q292-nim-games.swift */,
293299
B7B320701D2A8EA6004920E2 /* q299-bulls-and-cows.swift */,
300+
B72E3A901D2FC2AF003595EE /* q303-range-sum-query-immutable.swift */,
294301
B76B78001D281C7C0093657C /* q326-power-of-three.swift */,
295302
B76B78011D281C7C0093657C /* q334-reverse-string.swift */,
296303
B76B78431D29323D0093657C /* q342-power-of-four.swift */,
297304
B76B78021D281C7C0093657C /* q345-reverse-vowels-of-a-string.swift */,
298305
B76B78031D281C7C0093657C /* q349-Intersection-of-two-arrays.swift */,
299306
B76B78041D281C7C0093657C /* q350-intersection-of-two-arrays-ii.swift */,
300307
B76B78051D281C7C0093657C /* q371-sum-of-two-integers.swift */,
301-
B7E9B29B1D2CE93D00D78ECC /* q190-reverse-bits.swift */,
302-
B7E9B29D1D2E5C4600D78ECC /* q234-palindrome-linked-list.swift */,
303-
B7E9B29F1D2EA75500D78ECC /* q014-longest-common-prefix.swift */,
304-
B7E9B2A11D2EAEA900D78ECC /* q067-add-binary.swift */,
305-
B7E9B2A31D2F4B1E00D78ECC /* q028-implement-strstr.swift */,
306-
B72E3A901D2FC2AF003595EE /* q303-range-sum-query-immutable.swift */,
307-
B70C2AAF1D30E810002AC496 /* q204-count-primes.swift */,
308308
);
309309
path = Easy;
310310
sourceTree = "<group>";
@@ -374,7 +374,7 @@
374374
isa = PBXProject;
375375
attributes = {
376376
LastSwiftUpdateCheck = 0730;
377-
LastUpgradeCheck = 0730;
377+
LastUpgradeCheck = 0810;
378378
ORGANIZATIONNAME = "Rain Wang";
379379
TargetAttributes = {
380380
B76B77D61D281C190093657C = {
@@ -503,8 +503,10 @@
503503
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
504504
CLANG_WARN_EMPTY_BODY = YES;
505505
CLANG_WARN_ENUM_CONVERSION = YES;
506+
CLANG_WARN_INFINITE_RECURSION = YES;
506507
CLANG_WARN_INT_CONVERSION = YES;
507508
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
509+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
508510
CLANG_WARN_UNREACHABLE_CODE = YES;
509511
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
510512
CODE_SIGN_IDENTITY = "-";
@@ -548,8 +550,10 @@
548550
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
549551
CLANG_WARN_EMPTY_BODY = YES;
550552
CLANG_WARN_ENUM_CONVERSION = YES;
553+
CLANG_WARN_INFINITE_RECURSION = YES;
551554
CLANG_WARN_INT_CONVERSION = YES;
552555
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
556+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
553557
CLANG_WARN_UNREACHABLE_CODE = YES;
554558
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
555559
CODE_SIGN_IDENTITY = "-";
@@ -568,6 +572,7 @@
568572
MACOSX_DEPLOYMENT_TARGET = 10.11;
569573
MTL_ENABLE_DEBUG_INFO = NO;
570574
SDKROOT = macosx;
575+
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
571576
};
572577
name = Release;
573578
};

leetcode-swift.xcodeproj/xcuserdata/Rain.xcuserdatad/xcschemes/leetcode-swift.xcscheme

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0730"
3+
LastUpgradeVersion = "0810"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

leetcode-swift/Easy/q013-roman-to-integer.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,21 @@ struct q13 {
6767
var index = reversedChars.startIndex
6868
while index != reversedChars.endIndex {
6969
let char1 = reversedChars[index]
70-
if index.successor() == reversedChars.endIndex {
70+
if reversedChars.index(after: index) == reversedChars.endIndex {
7171
integer += romanUnit[char1]!
7272
break
7373
}
74-
let char2 = reversedChars[index.successor()]
74+
let char2 = reversedChars[reversedChars.index(after: index)]
7575

7676
if romanUnit[char1] > romanUnit[char2] && ["I", "C", "X"].contains(String(char2)) {
7777

7878
integer = integer + romanUnit[char1]! - romanUnit[char2]!
79-
index = index.successor().successor()
79+
index = reversedChars.index(index, offsetBy: 2)
8080

8181
} else {
8282

8383
integer += romanUnit[char1]!
84-
index = index.successor()
84+
index = reversedChars.index(after: index)
8585

8686
}
8787
}

leetcode-swift/Easy/q014-longest-common-prefix.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ struct q14 {
3535
}
3636

3737
if var s = s {
38-
var subrange = s.characters.indices
38+
var endIndex = s.endIndex
3939
for str in strs {
4040
while !s.isEmpty && !str.hasPrefix(s) {
41-
subrange.upperBound = <#T##Collection corresponding to your index##Collection#>.index(before: subrange.upperBound)
42-
s = s[subrange]
41+
endIndex = s.index(before: endIndex)
42+
s = s.substring(to: endIndex)
4343
}
4444
}
4545
return s
@@ -50,6 +50,6 @@ struct q14 {
5050
}
5151

5252
static func getSolution() -> Void {
53-
print(Solution().longestCommonPrefix(["ab","ac","abc","abcd"]))
53+
print(Solution().longestCommonPrefix(["ab","ab","abc","abcd"]))
5454
}
5555
}

leetcode-swift/Easy/q019-remove-nth-node-from-end-of-list.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@ struct q19 {
5555

5656
static func getSolution() -> Void {
5757
let head = LinkedListHelper.buildLinkedList(withNodes: [1,2,3,4,5,6,7])
58-
print(Solution().removeNthFromEnd(head, 1))
58+
print(Solution().removeNthFromEnd(head, 1) ?? "")
5959
}
6060
}

leetcode-swift/Easy/q021-merge-two-sorted-lists.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,16 @@ struct q21 {
5555
var l1 = LinkedListHelper.buildLinkedList(withNodes: [1,5,8,9,13])
5656
var l2 = LinkedListHelper.buildLinkedList(withNodes: [0,2,3,6,11,14])
5757

58-
print(Solution().mergeTwoLists(l1,l2))
58+
print(Solution().mergeTwoLists(l1,l2) ?? "")
5959

6060
l1 = LinkedListHelper.buildLinkedList(withNodes: [1,2,3,4,5])
6161
l2 = LinkedListHelper.buildLinkedList(withNodes: [1,2,3,4,5,6])
62-
print(Solution().mergeTwoLists(l1, l2))
62+
print(Solution().mergeTwoLists(l1, l2) ?? "")
6363

6464

6565
l1 = LinkedListHelper.buildLinkedList(withNodes: [1,2,3,4,5])
6666
l2 = nil
67-
print(Solution().mergeTwoLists(l1, l2))
67+
print(Solution().mergeTwoLists(l1, l2) ?? "")
6868

6969
}
7070
}

leetcode-swift/Easy/q024-swap-nodes-in-pairs.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ struct q24 {
4242

4343
let head = LinkedListHelper.buildLinkedList(withNodes: [1,2,3,4,5,6,7,8,9])
4444

45-
print(Solution().swapPairs(head))
45+
print(Solution().swapPairs(head) ?? "")
4646
}
4747
}

leetcode-swift/Easy/q083-remove-duplicates-from-sorted-list.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct q83 {
4545

4646
static func getSolution() -> Void {
4747
let head = LinkedListHelper.buildLinkedList(withNodes: [1,1,3,4,4,5,5,6,6,7])
48-
print(head)
49-
print(Solution().deleteDuplicates(head))
48+
print(head ?? "")
49+
print(Solution().deleteDuplicates(head) ?? "")
5050
}
5151
}

leetcode-swift/Easy/q101-symmetric-tree.swift

-2
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,6 @@ struct q101 {
122122

123123
static func getSolution() -> Void {
124124
let root = BinaryTreeHelper.buildTree(withNodes: [6,82,82,nil,53,53,nil,-58,nil,nil,-58,nil,-85,-85,nil,-9,nil,nil,-9,nil,48,48,nil,33,nil,nil,33,81,nil,nil,81,5,nil,nil,5,61,nil,nil,61,nil,9,9,nil,91,nil,nil,91,72,7,7,72,89,nil,94,nil,nil,94,nil,89,-27,nil,-30,36,36,-30,nil,-27,50,36,nil,-80,34,nil,nil,34,-80,nil,36,50,18,nil,nil,91,77,nil,nil,95,95,nil,nil,77,91,nil,nil,18,-19,65,nil,94,nil,-53,nil,-29,-29,nil,-53,nil,94,nil,65,-19,-62,-15,-35,nil,nil,-19,43,nil,-21,nil,nil,-21,nil,43,-19,nil,nil,-35,-15,-62,86,nil,nil,-70,nil,19,nil,55,-79,nil,nil,-96,-96,nil,nil,-79,55,nil,19,nil,-70,nil,nil,86,49,nil,25,nil,-19,nil,nil,8,30,nil,82,-47,-47,82,nil,30,8,nil,nil,-19,nil,25,nil,49])
125-
print(root)
126-
127125
print(Solution().isSymmetric(root))
128126
print(SolutionRecursively().isSymmetric(root))
129127
}

leetcode-swift/Easy/q102-binary-tree-level-order-traversal.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ struct q102 {
6565

6666
static func getSolution() -> Void {
6767
let root = BinaryTreeHelper.buildTree(withNodes: [3,9,20,nil,nil,15,7])
68-
print(root)
68+
print(root ?? "")
6969

7070
print(Solution().levelOrder(root))
7171
}

leetcode-swift/Easy/q104-maximum-depth-of-binary-tree.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct q104 {
3838
static func getSolution() -> Void {
3939
let root = BinaryTreeHelper.buildTree(withNodes: [1,2,3,4,nil,6,7,8,nil,10,nil,nil,nil,14,15])
4040

41-
print(root)
41+
print(root ?? "")
4242

4343
print(Solution().maxDepth(root))
4444
}

leetcode-swift/Easy/q107-binary-tree-level-order-traversal-ii.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ struct q107 {
6565

6666
static func getSolution() -> Void {
6767
let root = BinaryTreeHelper.buildTree(withNodes: [3,9,20,nil,nil,15,7])
68-
print(root)
68+
print(root ?? "")
6969
print(Solution().levelOrderBottom(root))
7070
}
7171
}

leetcode-swift/Easy/q110-balanced-binary-tree.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ struct q110 {
5050

5151
static func getSolution() -> Void {
5252
let root = BinaryTreeHelper.buildTree(withNodes: [1,2,3,4,5,6,7,8])
53-
print(root)
53+
print(root ?? "")
5454
print(Solution().isBalanced(root))
5555
}
5656
}

leetcode-swift/Easy/q111-minimum-depth-of-binary-tree.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ struct q111 {
5757

5858
static func getSolution() -> Void {
5959
let root = BinaryTreeHelper.buildTree(withNodes: [3,9,20,nil,nil,15,7])
60-
print(root)
60+
print(root ?? "")
6161

6262
print(Solution().minDepth(root))
6363
}

leetcode-swift/Easy/q112-path-sum.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ struct q112 {
8282

8383
static func getSolution() -> Void {
8484
let root = BinaryTreeHelper.buildTree(withNodes: [5,4,8,11,nil,13,4,7,2,nil,1])
85-
print(root)
85+
print(root ?? "")
8686

8787
print(Solution().hasPathSum(root, 22))
8888
print(Solution().hasPathSum(root, 9))

leetcode-swift/Easy/q160-intersection-of-two-linked-lists.swift

+10-10
Original file line numberDiff line numberDiff line change
@@ -78,25 +78,25 @@ struct q160 {
7878
var headA = LinkedListHelper.buildLinkedList(withNodes: [1,2,3,7,8,9])
7979
var headB = LinkedListHelper.buildLinkedList(withNodes: [4,5,6])
8080
headB?.next?.next?.next = headA?.next?.next?.next
81-
print(headA)
82-
print(headB)
81+
print(headA ?? "")
82+
print(headB ?? "")
8383

84-
print(Solution().getIntersectionNode(headA, headB))
84+
print(Solution().getIntersectionNode(headA, headB) ?? "")
8585

8686
headA = LinkedListHelper.buildLinkedList(withNodes: [1,2,3,7,8,9])
8787
headB = headA?.next?.next
88-
print(headA)
89-
print(headB)
88+
print(headA ?? "")
89+
print(headB ?? "")
9090

91-
print(Solution().getIntersectionNode(headA, headB))
91+
print(Solution().getIntersectionNode(headA, headB) ?? "")
9292

9393
headA = LinkedListHelper.buildLinkedList(withNodes: [1,2,3,7,8,9])
9494
headB = LinkedListHelper.buildLinkedList(withNodes: [4,5,6])
95-
print(headA)
96-
print(headB)
95+
print(headA ?? "")
96+
print(headB ?? "")
9797

98-
print(Solution().getIntersectionNode(headA, headB))
98+
print(Solution().getIntersectionNode(headA, headB) ?? "")
9999

100-
print(Solution().getIntersectionNode(nil, nil))
100+
print(Solution().getIntersectionNode(nil, nil) ?? "")
101101
}
102102
}

leetcode-swift/Easy/q203-remove-linked-list-elements.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ struct q203 {
5252

5353
static func getSolution() -> Void {
5454
let head = LinkedListHelper.buildLinkedList(withNodes: [1,2,3,1,1,2,1,2,3,1])
55-
print(head)
56-
print(Solution().removeElements(head, 1))
55+
print(head ?? "")
56+
print(Solution().removeElements(head, 1) ?? "")
5757
}
5858
}

leetcode-swift/Easy/q206-reverse-linked-list.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ struct q206 {
7979

8080
static func getSolution() -> Void {
8181
let head = LinkedListHelper.buildLinkedList(withNodes: [1,2])
82-
print(head)
82+
print(head ?? "")
8383

8484
let reversedListHead = Solution2().reverseList(head)
85-
print(reversedListHead)
85+
print(reversedListHead ?? "")
8686
}
8787
}

leetcode-swift/Easy/q226-invert-binary-tree.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ struct q226 {
4444
class Solution {
4545
func invertTree(_ root: TreeNode?) -> TreeNode? {
4646
if let node = root {
47-
invertTree(node.left)
48-
invertTree(node.right)
47+
_ = invertTree(node.left)
48+
_ = invertTree(node.right)
4949

5050
(node.left, node.right) = (node.right, node.left)
5151
}
@@ -60,6 +60,6 @@ struct q226 {
6060
root.left!.right = TreeNode(2)
6161
root.left!.right!.right = TreeNode(3)
6262

63-
print(Solution().invertTree(root))
63+
print(Solution().invertTree(root) ?? "")
6464
}
6565
}

leetcode-swift/Easy/q234-palindrome-linked-list.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ struct q234 {
9797
}
9898
static func getSolution() -> Void {
9999
let head = LinkedListHelper.buildLinkedList(withNodes: [1,1,2,1,1])
100-
print(head)
100+
print(head ?? "")
101101
print(Solution().isPalindrome(head))
102102
}
103103
}

leetcode-swift/Easy/q235-lowest-common-ancestor-of-a-binary-search-tree.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ struct q235 {
110110
let root = BinaryTreeHelper.buildTree(withNodes: [10,5,14,1,8,12,15,nil,4,7,nil,11,13,nil,nil,nil,nil,2])
111111
let p = TreeNode(1), q = TreeNode(8)
112112

113-
print(root)
113+
print(root ?? "")
114114

115115
print(Solution2().lowestCommonAncestor(root!, p, q).val)
116116
}

leetcode-swift/Easy/q257-binary-tree-paths.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ struct q257 {
6969

7070
let root = BinaryTreeHelper.buildTree(withNodes: [1,2,3,4,5,6,7,8,9,10])
7171

72-
print(root)
72+
print(root ?? "")
7373
print(Solution().binaryTreePaths(root))
7474
}
7575
}

leetcode-swift/Easy/q345-reverse-vowels-of-a-string.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ struct q345 {
4343

4444
while headIndex < tailIndex{
4545
while !vowels.contains(s[headIndex]) && headIndex < tailIndex {
46-
headIndex = <#T##Collection corresponding to `headIndex`##Collection#>.index(after: headIndex)
46+
headIndex = s.index(after: headIndex)
4747
}
4848

4949
while !vowels.contains(s[tailIndex]) && headIndex < tailIndex {
50-
tailIndex = <#T##Collection corresponding to `tailIndex`##Collection#>.index(before: tailIndex)
50+
tailIndex = s.index(before: tailIndex)
5151
}
5252

5353
if headIndex == tailIndex {
@@ -58,8 +58,8 @@ struct q345 {
5858
s.replaceSubrange(headIndex...headIndex, with: "\(s[tailIndex])")
5959
s.replaceSubrange(tailIndex...tailIndex, with: "\(tmp)")
6060

61-
headIndex = <#T##Collection corresponding to `headIndex`##Collection#>.index(after: headIndex)
62-
tailIndex = <#T##Collection corresponding to `tailIndex`##Collection#>.index(before: tailIndex)
61+
headIndex = s.index(after: headIndex)
62+
tailIndex = s.index(before: tailIndex)
6363
}
6464
return s
6565
}

0 commit comments

Comments
 (0)