Skip to content

Commit ceb9d5b

Browse files
committed
Update for swift 3
1 parent 0171357 commit ceb9d5b

File tree

76 files changed

+295
-252
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+295
-252
lines changed

leetcode-swift.xcodeproj/project.pbxproj

+3
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@
379379
TargetAttributes = {
380380
B76B77D61D281C190093657C = {
381381
CreatedOnToolsVersion = 7.3.1;
382+
LastSwiftMigration = 0800;
382383
};
383384
};
384385
};
@@ -574,13 +575,15 @@
574575
isa = XCBuildConfiguration;
575576
buildSettings = {
576577
PRODUCT_NAME = "$(TARGET_NAME)";
578+
SWIFT_VERSION = 3.0;
577579
};
578580
name = Debug;
579581
};
580582
B76B77E01D281C190093657C /* Release */ = {
581583
isa = XCBuildConfiguration;
582584
buildSettings = {
583585
PRODUCT_NAME = "$(TARGET_NAME)";
586+
SWIFT_VERSION = 3.0;
584587
};
585588
name = Release;
586589
};

leetcode-swift/Easy/q007-reverse-integer.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import Foundation
3939
struct q007 {
4040

4141
class Solution {
42-
func reverse(x: Int) -> Int {
42+
func reverse(_ x: Int) -> Int {
4343
var x = x
4444
var reversedX = 0
4545
while x != 0 {
@@ -58,4 +58,4 @@ struct q007 {
5858
print(Solution().reverse(9223372036854774999))
5959
print(Solution().reverse(-2147483412))
6060
}
61-
}
61+
}

leetcode-swift/Easy/q009-palindrome-number.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import Foundation
3535
struct q9 {
3636

3737
class Solution {
38-
func isPalindrome(x: Int) -> Bool {
38+
func isPalindrome(_ x: Int) -> Bool {
3939
if x < 0 || x % 10 == 0 {
4040
return x == 0
4141
}
@@ -57,4 +57,4 @@ struct q9 {
5757
print(Solution().isPalindrome(10))
5858

5959
}
60-
}
60+
}

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

+23-3
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,37 @@
3232

3333

3434
import Foundation
35+
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
36+
switch (lhs, rhs) {
37+
case let (l?, r?):
38+
return l < r
39+
case (nil, _?):
40+
return true
41+
default:
42+
return false
43+
}
44+
}
45+
46+
fileprivate func > <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
47+
switch (lhs, rhs) {
48+
case let (l?, r?):
49+
return l > r
50+
default:
51+
return rhs < lhs
52+
}
53+
}
54+
3555

3656
struct q13 {
3757

3858
class Solution {
39-
func romanToInt(s: String) -> Int {
59+
func romanToInt(_ s: String) -> Int {
4060

4161
let romanUnit = [Character("I"):1, Character("V"):5, Character("X"):10,
4262
Character("L"):50, Character("C"):100, Character("D"):500,
4363
Character("M"):1000]
4464

45-
let reversedChars = s.characters.reverse()
65+
let reversedChars = s.characters.reversed()
4666
var integer = 0
4767
var index = reversedChars.startIndex
4868
while index != reversedChars.endIndex {
@@ -73,4 +93,4 @@ struct q13 {
7393
static func getSolution() -> Void {
7494
print(Solution().romanToInt("MCMXCVI"))
7595
}
76-
}
96+
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import Foundation
2323
struct q14 {
2424

2525
class Solution {
26-
func longestCommonPrefix(strs: [String]) -> String {
26+
func longestCommonPrefix(_ strs: [String]) -> String {
2727
var s: String? //Find the shortest string
2828
var length = Int.max //Shortest string's length
2929

@@ -35,10 +35,10 @@ struct q14 {
3535
}
3636

3737
if var s = s {
38-
var subrange = s.startIndex..<s.endIndex
38+
var subrange = s.characters.indices
3939
for str in strs {
4040
while !s.isEmpty && !str.hasPrefix(s) {
41-
subrange.endIndex = subrange.endIndex.predecessor()
41+
subrange.upperBound = <#T##Collection corresponding to your index##Collection#>.index(before: subrange.upperBound)
4242
s = s[subrange]
4343
}
4444
}
@@ -52,4 +52,4 @@ struct q14 {
5252
static func getSolution() -> Void {
5353
print(Solution().longestCommonPrefix(["ab","ac","abc","abcd"]))
5454
}
55-
}
55+
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import Foundation
3333
struct q19 {
3434

3535
class Solution {
36-
func removeNthFromEnd(head: ListNode?, _ n: Int) -> ListNode? {
36+
func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? {
3737
if head == nil { return nil }
3838

3939
var fastPtr = head, slowPtr = head
@@ -57,4 +57,4 @@ struct q19 {
5757
let head = LinkedListHelper.buildLinkedList(withNodes: [1,2,3,4,5,6,7])
5858
print(Solution().removeNthFromEnd(head, 1))
5959
}
60-
}
60+
}

leetcode-swift/Easy/q020-valid-parentheses.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ import Foundation
2727
struct q20 {
2828

2929
class Solution {
30-
func isValid(s: String) -> Bool {
30+
func isValid(_ s: String) -> Bool {
3131
let rule = Array("()[]{}".characters)
3232
var stack = [Character]()
33-
for (_, char) in s.characters.enumerate() {
34-
if rule.indexOf(char)! % 2 == 0 { // open brackets
33+
for (_, char) in s.characters.enumerated() {
34+
if rule.index(of: char)! % 2 == 0 { // open brackets
3535
stack.append(char)
3636
} else { // close brackets
3737
if stack.isEmpty {
3838
return false
3939
} else {
40-
if rule.indexOf(char)! - rule.indexOf(stack.last!)! == 1 { //barckets matched
40+
if rule.index(of: char)! - rule.index(of: stack.last!)! == 1 { //barckets matched
4141
stack.removeLast()
4242
} else {
4343
return false
@@ -58,4 +58,4 @@ struct q20 {
5858

5959

6060
}
61-
}
61+
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import Foundation
2525
struct q21 {
2626

2727
class Solution {
28-
func mergeTwoLists(l1: ListNode?, _ l2: ListNode?) -> ListNode? {
28+
func mergeTwoLists(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {
2929

3030
if l1 == nil { return l2 }
3131
if l2 == nil { return l1 }
@@ -67,4 +67,4 @@ struct q21 {
6767
print(Solution().mergeTwoLists(l1, l2))
6868

6969
}
70-
}
70+
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import Foundation
1616
struct q24 {
1717

1818
class Solution {
19-
func swapPairs(head: ListNode?) -> ListNode? {
19+
func swapPairs(_ head: ListNode?) -> ListNode? {
2020

2121
var p: ListNode? = nil
2222
var i = head
@@ -44,4 +44,4 @@ struct q24 {
4444

4545
print(Solution().swapPairs(head))
4646
}
47-
}
47+
}

leetcode-swift/Easy/q026-remove-duplicates-from-sorted-array.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import Foundation
3131
struct q26 {
3232

3333
class Solution {
34-
func removeDuplicates(inout nums: [Int]) -> Int {
34+
func removeDuplicates(_ nums: inout [Int]) -> Int {
3535
if nums.isEmpty {
3636
return 0
3737
}
@@ -57,4 +57,4 @@ struct q26 {
5757
print(array[i], terminator: " ")
5858
}
5959
}
60-
}
60+
}

leetcode-swift/Easy/q027-remove-element.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import Foundation
2525
struct q27 {
2626

2727
class Solution {
28-
func removeElement(inout nums: [Int], _ val: Int) -> Int {
28+
func removeElement(_ nums: inout [Int], _ val: Int) -> Int {
2929
var checkIndex = nums.startIndex
3030
var contentIndex = nums.startIndex
3131
while checkIndex < nums.endIndex {
@@ -40,8 +40,8 @@ struct q27 {
4040
}
4141

4242
class Solution2 {
43-
func removeElement(inout nums: [Int], _ val: Int) -> Int {
44-
var lastIndex = nums.endIndex.predecessor()
43+
func removeElement(_ nums: inout [Int], _ val: Int) -> Int {
44+
var lastIndex = (nums.endIndex - 1)
4545
var checkIndex = nums.startIndex
4646
while checkIndex <= lastIndex {
4747
if nums[checkIndex] == val {
@@ -60,4 +60,4 @@ struct q27 {
6060
print(Solution2().removeElement(&nums, 3))
6161
print(nums)
6262
}
63-
}
63+
}

leetcode-swift/Easy/q028-implement-strstr.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ struct q28 {
5050
//KMP find substring
5151
class Solution {
5252

53-
func calculateNext(p: [Character]) -> [Int] {
53+
func calculateNext(_ p: [Character]) -> [Int] {
5454

5555
guard !p.isEmpty else {
5656
return []
5757
}
5858

59-
var next = Array(count:p.count, repeatedValue:0)
59+
var next = Array(repeating: 0, count: p.count)
6060
next[0] = -1
6161
var k = next[0]
6262

@@ -76,7 +76,7 @@ struct q28 {
7676
return next
7777
}
7878

79-
func kmpSearch(p p: [Character], s:[Character], sStart: Int, sEnd: Int, next: [Int]) -> Int {
79+
func kmpSearch(p: [Character], s:[Character], sStart: Int, sEnd: Int, next: [Int]) -> Int {
8080
guard !p.isEmpty else {
8181
return 0
8282
}
@@ -98,7 +98,7 @@ struct q28 {
9898
return ans
9999
}
100100

101-
func strStr(haystack: String, _ needle: String) -> Int {
101+
func strStr(_ haystack: String, _ needle: String) -> Int {
102102

103103
let s = Array(haystack.characters)
104104
let p = Array(needle.characters)
@@ -112,4 +112,4 @@ struct q28 {
112112
static func getSolution() -> Void {
113113
print(Solution().strStr("mississippi", "issip"))
114114
}
115-
}
115+
}

leetcode-swift/Easy/q036-valid-sudoku.swift

+8-8
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ struct q36 {
3232

3333
class Solution {
3434

35-
var ruleRow = [Set<Character>](count: 9, repeatedValue: Set<Character>())
36-
var ruleColumn = [Set<Character>](count: 9, repeatedValue: Set<Character>())
37-
var ruleSquare = [Set<Character>](count: 9, repeatedValue: Set<Character>())
35+
var ruleRow = [Set<Character>](repeating: Set<Character>(), count: 9)
36+
var ruleColumn = [Set<Character>](repeating: Set<Character>(), count: 9)
37+
var ruleSquare = [Set<Character>](repeating: Set<Character>(), count: 9)
3838

39-
func isValidSudoku(board: [[Character]]) -> Bool {
39+
func isValidSudoku(_ board: [[Character]]) -> Bool {
4040

4141
var indexOfRow = board.startIndex
4242
while indexOfRow != board.endIndex {
@@ -54,15 +54,15 @@ struct q36 {
5454
ruleRow[indexOfRow].insert(char)
5555
ruleColumn[indexOfChar].insert(char)
5656
ruleSquare[(indexOfRow / 3) * 3 + indexOfChar / 3].insert(char)
57-
indexOfChar = indexOfChar.successor()
57+
indexOfChar = (indexOfChar + 1)
5858
} else {
5959
return false
6060
}
6161
} else {
62-
indexOfChar = indexOfChar.successor()
62+
indexOfChar = (indexOfChar + 1)
6363
}
6464
}
65-
indexOfRow = indexOfRow.successor()
65+
indexOfRow = (indexOfRow + 1)
6666
}
6767
return true
6868
}
@@ -80,4 +80,4 @@ struct q36 {
8080
let row9 = Array(".........".characters)
8181
print(Solution().isValidSudoku([row1,row2,row3,row4,row5,row6,row7,row8,row9]))
8282
}
83-
}
83+
}

leetcode-swift/Easy/q038-count-and-say.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ import Foundation
3232
struct q38 {
3333

3434
class Solution {
35-
func countAndSay(n: Int) -> String {
35+
func countAndSay(_ n: Int) -> String {
3636
var str = "1"
3737
for _ in 1..<n {
3838
var tmpStr = ""
3939
var pNum = 0
4040
var pChar = str.characters.first
41-
for (_, char) in str.characters.enumerate() {
41+
for (_, char) in str.characters.enumerated() {
4242
if char == pChar {
4343
pNum += 1
4444
} else {
@@ -59,4 +59,4 @@ struct q38 {
5959
static func getSolution() -> Void {
6060
print(Solution().countAndSay(6))
6161
}
62-
}
62+
}

leetcode-swift/Easy/q058-length-of-last-word.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ import Foundation
3232
struct q58 {
3333

3434
class Solution {
35-
func lengthOfLastWord(s: String) -> Int {
36-
if let lastWord = s.componentsSeparatedByString(" ").filter({ $0 != "" }).last {
35+
func lengthOfLastWord(_ s: String) -> Int {
36+
if let lastWord = s.components(separatedBy: " ").filter({ $0 != "" }).last {
3737
return lastWord.characters.count
3838
} else {
3939
return 0
@@ -42,7 +42,7 @@ struct q58 {
4242
}
4343

4444
class Solution2 {
45-
func lengthOfLastWord(s: String) -> Int {
45+
func lengthOfLastWord(_ s: String) -> Int {
4646
if s.isEmpty {
4747
return 0
4848
}
@@ -67,4 +67,4 @@ struct q58 {
6767
static func getSolution() -> Void {
6868
print(Solution2().lengthOfLastWord(" abcdefgh abcdefg "))
6969
}
70-
}
70+
}

0 commit comments

Comments
 (0)