Skip to content

Commit fcb0715

Browse files
author
Michael Ho
committed
Added new accepted solutions
1 parent fc41080 commit fcb0715

File tree

10 files changed

+192
-19
lines changed

10 files changed

+192
-19
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// LeetCode: https://leetcode.com/problems/plus-one/description/
2+
// Difficulty: Easy
3+
4+
class Solution {
5+
func plusOne(_ digits: [Int]) -> [Int] {
6+
var output: [Int] = digits
7+
var index = digits.count - 1
8+
output[index] += 1 // Add one
9+
var addOne: Bool = output[index] >= 10
10+
while index >= 0 {
11+
output[index] += addOne ? 1 : 0
12+
addOne = output[index] >= 10
13+
output[index] = output[index] >= 10 ? 0 : output[index]
14+
index -= 1
15+
}
16+
if addOne {
17+
output.insert(1, at: 0)
18+
}
19+
return output
20+
}
21+
}
22+
23+
let solution = Solution()
24+
print("\(solution.plusOne([1,2,3]))") // [1,2,4]
25+
print("\(solution.plusOne([4,3,2,2]))") // [4,3,2,3]
26+
print("\(solution.plusOne([4,5,9,9]))") // [4,6,0,0]
27+
print("\(solution.plusOne([7,2,8,5,0,9,1,2,9,5,3,6,6,7,3,2,8,4,3,7,9,5,7,7,4,7,4,9,4,7,0,1,1,1,7,4,0,0,6]))") // Check runtime
28+
print("\(solution.plusOne([9]))") // [1,0]
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// LeetCode: https://leetcode.com/problems/toeplitz-matrix/description/
2+
3+
class Solution {
4+
func isToeplitzMatrix(_ matrix: [[Int]]) -> Bool {
5+
guard matrix.count > 0 else {
6+
fatalError()
7+
}
8+
guard matrix.count > 1 else {
9+
return true
10+
}
11+
var index = 0
12+
var arrayIdx = 0
13+
while arrayIdx < matrix.count {
14+
while index < matrix[arrayIdx].count {
15+
let num = matrix[arrayIdx][index]
16+
var movingArrIdx = arrayIdx + 1
17+
var movingIdx = index + 1
18+
while movingArrIdx < matrix.count, movingIdx < matrix[movingArrIdx].count {
19+
guard matrix[movingArrIdx][movingIdx] == num else {
20+
return false
21+
}
22+
movingArrIdx += 1
23+
movingIdx += 1
24+
}
25+
index += 1
26+
}
27+
index = 0
28+
arrayIdx += 1
29+
}
30+
return true
31+
}
32+
}
33+
34+
let solution = Solution()
35+
print("\(solution.isToeplitzMatrix([[1,2,3,4],[5,1,2,3],[9,5,1,2]]))") // True
36+
print("\(solution.isToeplitzMatrix([[1,2],[2,2]]))") // False
37+
print("\(solution.isToeplitzMatrix([[36,59,71,15,26,82,87],[56,36,59,71,15,26,82],[15,0,36,59,71,15,26]]))") // False
38+
/**
39+
For the input above:
40+
[36,59,71,15,26,82,87]
41+
[56,36,59,71,15,26,82]
42+
[15,0,36,59,71,15,26]
43+
*/
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='macos'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// LeetCode: https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/
1+
// LeetCode: https://leetcode.com/problems/swap-nodes-in-pairs/description/
22

33
public class ListNode {
44
public var val: Int
@@ -10,29 +10,32 @@ public class ListNode {
1010
}
1111

1212
class Solution {
13-
func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? {
13+
func swapPairs(_ head: ListNode?) -> ListNode? {
1414
guard let head = head else {
1515
return nil
1616
}
17-
var arr = convertToArray(head)
18-
let index = arr.count - n - 1
19-
guard index > 0 else {
20-
return nil
17+
// Handle edge case
18+
guard let _ = head.next else {
19+
return head
2120
}
22-
arr[index].next = arr[index + 2]
23-
return head
24-
}
25-
26-
private func convertToArray(_ head: ListNode) -> [ListNode] {
2721
var node: ListNode? = head
28-
var arr: [ListNode] = []
29-
while nil != node {
30-
arr.append(node!)
31-
node = node?.next
22+
let newHead = node?.next
23+
var prev: ListNode?
24+
// Loop every two nodes
25+
while let current = node, let next = node?.next {
26+
// Change first node
27+
current.next = next.next
28+
// Move 2 indices
29+
node = next.next
30+
// Change second node
31+
next.next = current
32+
// Change the previous node of this pair
33+
prev?.next = next
34+
// Capture current node
35+
prev = current
3236
}
33-
return arr
37+
return newHead
3438
}
35-
3639
func printLinkedList(_ head: ListNode) {
3740
var printedStr = ""
3841
var currentNode: ListNode? = head
@@ -54,5 +57,7 @@ let node3 = ListNode(3, node4)
5457
let node2 = ListNode(2, node3)
5558
let node1 = ListNode(1, node2)
5659
solution.printLinkedList(node1)
57-
solution.removeNthFromEnd(node1, 2)
58-
solution.printLinkedList(node1)
60+
61+
// Reverse Linked List
62+
print("swap below:")
63+
solution.printLinkedList(solution.swapPairs(node1)!)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// LeetCode: https://leetcode.com/problems/search-in-rotated-sorted-array/description/
2+
// Medium
3+
4+
class Solution {
5+
func search(_ nums: [Int], _ target: Int) -> Int {
6+
return search(nums, target, 0)
7+
}
8+
func search(_ nums: [Int], _ target: Int, _ startIdx: Int) -> Int {
9+
var output = -1
10+
if nums.count == 1, nums.contains(target) {
11+
return startIdx
12+
}
13+
let midIndex = nums.count / 2
14+
let leftNums = Array(nums[0..<midIndex])
15+
let rightNums = Array(nums[midIndex..<nums.count])
16+
if leftNums.contains(target) {
17+
output = search(leftNums, target, startIdx)
18+
} else if rightNums.contains(target) {
19+
output = search(rightNums, target, startIdx + midIndex)
20+
}
21+
return output
22+
}
23+
}
24+
25+
let solution = Solution()
26+
print("\(solution.search([4,5,6,7,0,1,2], 0))") // 4
27+
print("\(solution.search([4,5,6,7,0,1,2], 3))") // -1
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='macos'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// LeetCode: https://leetcode.com/problems/rotate-image/description/
2+
// Reference: https://leetcode.com/problems/rotate-image/discuss/18872/A-common-method-to-rotate-the-image
3+
// Difficulty: Medium
4+
5+
class Solution {
6+
func rotate(_ matrix: inout [[Int]]) {
7+
guard matrix.count > 1 else {
8+
return
9+
}
10+
matrix.reverse()
11+
var i = 0
12+
var j = 0
13+
while i < matrix.count {
14+
j = i
15+
while j < matrix[i].count {
16+
(matrix[i][j], matrix[j][i]) = (matrix[j][i], matrix[i][j])
17+
j += 1
18+
}
19+
i += 1
20+
}
21+
}
22+
}
23+
24+
let solution = Solution()
25+
var image1 = [
26+
[1,2,3],
27+
[4,5,6],
28+
[7,8,9]
29+
]
30+
solution.rotate(&image1)
31+
print("\(image1))")
32+
/** output
33+
[
34+
[7,4,1],
35+
[8,5,2],
36+
[9,6,3]
37+
]
38+
*/
39+
var image2 = [
40+
[ 5, 1, 9,11],
41+
[ 2, 4, 8,10],
42+
[13, 3, 6, 7],
43+
[15,14,12,16]
44+
]
45+
solution.rotate(&image2)
46+
print("\(image2)")
47+
/** output
48+
[
49+
[15,13, 2, 5],
50+
[14, 3, 4, 1],
51+
[12, 6, 8, 9],
52+
[16, 7,10,11]
53+
]
54+
*/
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='macos'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

0 commit comments

Comments
 (0)