Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 04e3816

Browse files
Added solutions for 1, 2, 4, 7, 69
1 parent 2b2e54f commit 04e3816

File tree

8 files changed

+154
-0
lines changed

8 files changed

+154
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// LeetCode: https://leetcode.com/problems/two-sum/description/
2+
// Solution: https://github.com/zhubofei/LeetCode-Swift/blob/master/0001-two-sum.playground/Contents.swift
3+
4+
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
5+
var dict: [Int: Int] = [:] // [number : indexPosition]
6+
for (index, num) in nums.enumerated() {
7+
let complement = target - num
8+
if let compIndex = dict[complement] {
9+
return [index, compIndex]
10+
}
11+
dict[num] = index
12+
}
13+
fatalError()
14+
}
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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// LeetCode: https://leetcode.com/problems/add-two-numbers/description/
2+
// Solution: https://github.com/zhubofei/LeetCode-Swift/blob/master/0002-add-two-numbers.playground/Contents.swift
3+
4+
import Foundation
5+
6+
public class ListNode {
7+
public var val: Int
8+
public var next: ListNode?
9+
public init(_ val: Int) {
10+
self.val = val
11+
self.next = nil
12+
}
13+
}
14+
15+
class Solution {
16+
func addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {
17+
guard let l1 = l1, let l2 = l2 else {
18+
fatalError()
19+
}
20+
if l1.next == nil, l2.next == nil {
21+
let node = ListNode((l1.val + l2.val) % 10)
22+
if l1.val + l2.val >= 10 {
23+
node.next = ListNode(1)
24+
}
25+
return node
26+
}
27+
var node1: ListNode? = l1
28+
var node2: ListNode? = l2
29+
var temp = ListNode((l1.val + l2.val) % 10)
30+
let head = temp
31+
var shouldAdd: Bool = ((l1.val + l2.val) / 10) > 0
32+
33+
while nil != node1?.next || nil != node2?.next || shouldAdd {
34+
var val1 = 0
35+
var val2 = 0
36+
if let node = node1?.next {
37+
val1 = node.val
38+
node1 = node
39+
}
40+
41+
if let node = node2?.next {
42+
val2 = node.val
43+
node2 = node
44+
}
45+
46+
let newValue = (val1 + val2 + (shouldAdd ? 1 : 0)) % 10
47+
shouldAdd = (val1 + val2) + (shouldAdd ? 1 : 0) >= 10
48+
49+
temp.next = ListNode(newValue)
50+
temp = temp.next!
51+
}
52+
return head
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='ios'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// LeetCode: https://leetcode.com/problems/median-of-two-sorted-arrays/description/
2+
// Hint: merge sort
3+
4+
class Solution {
5+
func findMedianSortedArrays(_ nums1: [Int], _ nums2: [Int]) -> Double {
6+
var leftIndex = 0
7+
var rightIndex = 0
8+
var results: [Int] = []
9+
10+
var shouldSort = true
11+
while shouldSort {
12+
if leftIndex >= nums1.count {
13+
results.append(contentsOf: Array(nums2[rightIndex..<nums2.count]))
14+
shouldSort = false
15+
break
16+
}
17+
18+
if rightIndex >= nums2.count {
19+
results.append(contentsOf: Array(nums1[leftIndex..<nums1.count]))
20+
shouldSort = false
21+
break
22+
}
23+
24+
if nums1[leftIndex] > nums2[rightIndex] {
25+
results.append(nums2[rightIndex])
26+
rightIndex += 1
27+
} else {
28+
results.append(nums1[leftIndex])
29+
leftIndex += 1
30+
}
31+
}
32+
33+
if results.count % 2 == 0 {
34+
return Double(results[results.count/2 - 1] + results[results.count/2])/2
35+
} else {
36+
return Double(results[results.count/2])
37+
}
38+
}
39+
}
40+
41+
// Test
42+
let solution = Solution()
43+
print("\(solution.findMedianSortedArrays([1, 3], [2, 4]))")
44+
print("\(solution.findMedianSortedArrays([1, 3], [2]))")
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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// LeetCode: https://leetcode.com/problems/reverse-integer/description/
2+
import Foundation
3+
4+
class Solution {
5+
func reverse(_ x: Int) -> Int {
6+
var temp = abs(x)
7+
var output = 0
8+
while temp > 0 {
9+
var tail = temp % 10
10+
temp = temp / 10
11+
output = output * 10 + tail
12+
if (output > Int32.max || output < Int32.min) { return 0 }
13+
}
14+
15+
return output * x.signum() // Times back the sign of the original number
16+
}
17+
18+
func mySqrt(_ x: Int) -> Int {
19+
return Int(sqrt(Double(x)))
20+
21+
}
22+
}
23+
24+
let solution = Solution()
25+
print("\(solution.reverse(-123))")
26+
print("\(solution.mySqrt(8))")
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)

AltStyle によって変換されたページ (->オリジナル) /