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 4e331c5

Browse files
Added new accepted solutions
1 parent 9878204 commit 4e331c5

File tree

26 files changed

+1090
-81
lines changed

26 files changed

+1090
-81
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// LeetCode: https://leetcode.com/problems/climbing-stairs/description/
2+
// Hint: Fibonacci number
3+
4+
import XCTest
5+
6+
class Solution {
7+
func climbStairs(_ n: Int) -> Int {
8+
var arr = Array(repeating: 0, count: n)
9+
arr[0] = 1
10+
guard n > 1 else { return arr[n-1] }
11+
arr[1] = 2
12+
guard n > 2 else { return arr[n-1] }
13+
for i in 2..<n {
14+
arr[i] = arr[i-1] + arr[i-2]
15+
}
16+
return arr[n-1]
17+
}
18+
}
19+
20+
class Tests: XCTestCase {
21+
let s = Solution()
22+
23+
func testSample1(){
24+
let input = 3
25+
let expected = 3
26+
XCTAssertEqual(s.climbStairs(input), expected)
27+
}
28+
29+
func testSample2(){
30+
let input = 4
31+
let expected = 5
32+
XCTAssertEqual(s.climbStairs(input), expected)
33+
}
34+
35+
func testSample3(){
36+
let input = 1
37+
let expected = 1
38+
XCTAssertEqual(s.climbStairs(input), expected)
39+
}
40+
}
41+
42+
Tests.defaultTestSuite.run()
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' executeOnSourceChanges='false'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// LeetCode: https://leetcode.com/problems/keyboard-row/description/
2+
3+
import XCTest
4+
5+
class Solution {
6+
func findWords(_ words: [String]) -> [String] {
7+
guard words.count > 0 else { return [] }
8+
var dict: [Int : String] = [
9+
1:String("QWERTYUIOP".sorted()),
10+
2:String("ASDFGHJKL".sorted()),
11+
3:String("ZXCVBNM".sorted())
12+
]
13+
var output: [String] = []
14+
for str in words {
15+
let processedStr = Set(str.uppercased()).sorted()
16+
guard processedStr.count > 0 else {
17+
output.append(str)
18+
continue
19+
}
20+
var key = 0
21+
if dict[1]!.contains(processedStr[0]) {
22+
key = 1
23+
} else if dict[2]!.contains(processedStr[0]) {
24+
key = 2
25+
} else if dict[3]!.contains(processedStr[0]) {
26+
key = 3
27+
}
28+
var valid = true
29+
for ch in processedStr {
30+
if !dict[key]!.contains(ch) {
31+
valid = false
32+
}
33+
}
34+
if valid {
35+
output.append(str)
36+
}
37+
}
38+
return output
39+
}
40+
}
41+
42+
class Tests: XCTestCase {
43+
let s = Solution()
44+
func testSample1() {
45+
let input = ["Hello", "Alaska", "Dad", "Peace"]
46+
let expected = ["Alaska", "Dad"]
47+
XCTAssertEqual(s.findWords(input), expected)
48+
}
49+
}
50+
51+
Tests.defaultTestSuite.run()
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' executeOnSourceChanges='false'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// LeetCode: https://leetcode.com/problems/reverse-nodes-in-k-group/description/
2+
3+
import XCTest
4+
5+
public class ListNode {
6+
public var val: Int
7+
public var next: ListNode?
8+
public init(_ val: Int) {
9+
self.val = val
10+
self.next = nil
11+
}
12+
}
13+
14+
class Solution {
15+
func reverseKGroup(_ head: ListNode?, _ k: Int) -> ListNode? {
16+
var node = head
17+
var movHead = head
18+
var pre: ListNode?
19+
var newHead: ListNode? = head
20+
var idx = 1
21+
22+
while node != nil {
23+
let next = node?.next
24+
if idx % k == 0 {
25+
let pair = reverseLinkedList(movHead, k)
26+
if pre != nil {
27+
pre?.next = pair.first
28+
} else {
29+
newHead = pair.first
30+
}
31+
32+
// Updates
33+
pre = pair.last
34+
pair.last?.next = next
35+
movHead = pair.last?.next
36+
37+
}
38+
idx += 1
39+
node = next
40+
}
41+
return newHead
42+
}
43+
44+
// Input first node of LinkedList and returns the last node of the new LinkedList
45+
private func reverseLinkedList(_ head: ListNode? ,_ k: Int) -> (first: ListNode?, last: ListNode?) {
46+
var node = head
47+
var pre: ListNode?
48+
var idx = 1
49+
50+
while node != nil {
51+
let next = node?.next
52+
node?.next = pre
53+
pre = node
54+
if idx >= k { break }
55+
node = next
56+
idx += 1
57+
}
58+
return (pre, head)
59+
}
60+
}
61+
62+
class Tests: XCTestCase {
63+
let s = Solution()
64+
65+
func testSample1() {
66+
let input = ([1, 2, 3, 4, 5], 2)
67+
let expected = [2, 1, 4, 3, 5]
68+
let output = s.reverseKGroup(convertArrayToListNodeHead(input.0), input.1)
69+
XCTAssertEqual(convertListNodeToArray(output), expected)
70+
}
71+
72+
func testSample2() {
73+
let input = ([1, 2, 3, 4, 5], 3)
74+
let expected = [3, 2, 1, 4, 5]
75+
let output = s.reverseKGroup(convertArrayToListNodeHead(input.0), input.1)
76+
XCTAssertEqual(convertListNodeToArray(output), expected)
77+
}
78+
79+
func testSample3() {
80+
let input = ([1], 2)
81+
let expected = [1]
82+
let output = s.reverseKGroup(convertArrayToListNodeHead(input.0), input.1)
83+
XCTAssertEqual(convertListNodeToArray(output), expected)
84+
}
85+
86+
func convertArrayToListNodeHead(_ arr: [Int]) -> ListNode? {
87+
guard arr.count > 0 else {
88+
return nil
89+
}
90+
var arrNodes: [ListNode] = []
91+
for i in 0..<arr.count {
92+
let node = ListNode(arr[i])
93+
arrNodes.append(node)
94+
if i - 1 >= 0 {
95+
arrNodes[i-1].next = node
96+
}
97+
}
98+
return arrNodes.first
99+
}
100+
101+
func convertListNodeToArray(_ head: ListNode?) -> [Int] {
102+
var arr: [Int] = []
103+
var node = head
104+
while node != nil {
105+
arr.append((node?.val)!)
106+
node = node?.next
107+
}
108+
return arr
109+
}
110+
}
111+
112+
Tests.defaultTestSuite.run()
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' executeOnSourceChanges='false'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// LeetCode: https://leetcode.com/problems/first-missing-positive/description/
2+
3+
import XCTest
4+
5+
class Solution {
6+
func firstMissingPositive(_ nums: [Int]) -> Int {
7+
var count = 1
8+
while true {
9+
if !nums.contains(count) {
10+
return count
11+
}
12+
count += 1
13+
}
14+
}
15+
}
16+
17+
class Tests: XCTestCase {
18+
let solution = Solution()
19+
20+
func testSample1() {
21+
let input = [1,2,0]
22+
let expected = 3
23+
XCTAssertEqual(solution.firstMissingPositive(input), expected)
24+
}
25+
26+
func testSample2() {
27+
let input = [3,4,-1,1]
28+
let expected = 2
29+
XCTAssertEqual(solution.firstMissingPositive(input), expected)
30+
}
31+
32+
func testSample3() {
33+
let input = [7,8,9,11,12]
34+
let expected = 1
35+
XCTAssertEqual(solution.firstMissingPositive(input), expected)
36+
}
37+
38+
func testSample4() {
39+
let input: [Int] = []
40+
let expected = 1
41+
XCTAssertEqual(solution.firstMissingPositive(input), expected)
42+
}
43+
}
44+
45+
Tests.defaultTestSuite.run()
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' executeOnSourceChanges='false'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,70 @@
11
// LeetCode: https://leetcode.com/problems/3sum-closest/description/
22
// Simple math
33

4+
import XCTest
5+
46
class Solution {
57
func threeSumClosest(_ nums: [Int], _ target: Int) -> Int {
6-
var sortedN = nums.sorted()
7-
var pairs:[Int:[Int]]=[:]
8-
var i =0
9-
while i < sortedN.count {
10-
var k = i + 1
11-
while k < sortedN.count {
12-
let comp = target - sortedN[k]- sortedN[i]
13-
if nil == pairs[comp] {
14-
pairs[comp]=[i, k]
8+
var sorted = nums.sorted()
9+
var current:Int?
10+
for i in0..<sorted.count {
11+
varstart= i +1
12+
var end = sorted.count - 1
13+
while start < end {
14+
let sum = sorted[i]+ sorted[start]+ sorted[end]
15+
if target == sum {
16+
return sum
1517
}
16-
k += 1
17-
}
18-
i += 1
19-
}
20-
var output: Int?
21-
var third = 0
22-
while third < sortedN.count {
23-
for pair in pairs {
24-
if !pair.value.contains(third) {
25-
if pair.key == sortedN[third] {
26-
return target
18+
if let curVal = current {
19+
if abs(target - sum) < abs(target - curVal) {
20+
current = sum
21+
}
22+
if sum > target {
23+
end -= 1
2724
} else {
28-
if nil == output {
29-
output = target - pair.key + sortedN[third]
30-
} else if abs(pair.key - sortedN[third]) < abs(target - output!) {
31-
output = target - pair.key + sortedN[third]
32-
}
25+
start += 1
3326
}
27+
} else {
28+
current = sum
3429
}
3530
}
36-
third += 1
3731
}
38-
return output!
32+
return current!
33+
}
34+
}
35+
36+
class Tests: XCTestCase {
37+
let s = Solution()
38+
39+
func testSample1() {
40+
let input = ([-1, 2, 1, -4], 1)
41+
let expected = 2
42+
XCTAssertEqual(s.threeSumClosest(input.0, input.1), expected)
43+
}
44+
45+
func testSample2() {
46+
let input = ([-3,-2,-5,3,-4], -1)
47+
let expected = -2
48+
XCTAssertEqual(s.threeSumClosest(input.0, input.1), expected)
49+
}
50+
51+
func testSample3() {
52+
let input = ([0,2,1,-3], 1)
53+
let expected = 0
54+
XCTAssertEqual(s.threeSumClosest(input.0, input.1), expected)
55+
}
56+
57+
func testSample4() {
58+
let input = ([1,1,-1,-1,3], -1)
59+
let expected = -1
60+
XCTAssertEqual(s.threeSumClosest(input.0, input.1), expected)
61+
}
62+
63+
func testSample5() {
64+
let input = ([1,2,4,8,16,32,64,128], 82)
65+
let expected = 82
66+
XCTAssertEqual(s.threeSumClosest(input.0, input.1), expected)
3967
}
4068
}
4169

42-
let solution = Solution()
43-
print("\(solution.threeSumClosest([-1, 2, 1, -4], 1))") // 2
44-
print("\(solution.threeSumClosest([-3,-2,-5,3,-4], -1))") // -2
70+
Tests.defaultTestSuite.run()

0 commit comments

Comments
(0)

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