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 9878204

Browse files
Added new solutions
1 parent b7b8ff2 commit 9878204

File tree

8 files changed

+289
-0
lines changed

8 files changed

+289
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// LeetCode: https://leetcode.com/problems/merge-sorted-array/description/
2+
3+
import XCTest
4+
5+
class Solution {
6+
func merge(_ nums1: inout [Int], _ m: Int, _ nums2: [Int], _ n: Int) {
7+
nums1 = (Array(nums1[0..<m]) + Array(nums2[0..<n])).sorted()
8+
}
9+
}
10+
11+
class Tests: XCTestCase {
12+
let s = Solution()
13+
14+
func testSample() {
15+
var nums1 = [1,2,3,0,0,0]
16+
let nums2 = [2,5,6]
17+
let m = 3
18+
let n = 3
19+
let expected = [1,2,2,3,5,6]
20+
s.merge(&nums1, m, nums2, n)
21+
XCTAssertEqual(nums1, expected)
22+
}
23+
}
24+
25+
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: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// LeetCode: https://leetcode.com/problems/surface-area-of-3d-shapes/description/
2+
3+
import XCTest
4+
5+
class Solution {
6+
func surfaceArea(_ grid: [[Int]]) -> Int {
7+
var output = 0
8+
for i in 0..<grid.count {
9+
for j in 0..<grid[i].count {
10+
if grid[i][j] == 0 {
11+
continue
12+
}
13+
output += grid[i][j] * 4 + 2
14+
if i-1 >= 0 {
15+
output -= min(grid[i][j], grid[i-1][j])
16+
}
17+
if i+1 < grid.count {
18+
output -= min(grid[i][j], grid[i+1][j])
19+
}
20+
if j-1 >= 0 {
21+
output -= min(grid[i][j], grid[i][j-1])
22+
}
23+
if j+1 < grid[i].count {
24+
output -= min(grid[i][j], grid[i][j+1])
25+
}
26+
}
27+
}
28+
return output
29+
}
30+
}
31+
32+
class Tests: XCTestCase {
33+
let s = Solution()
34+
35+
func testSample1() {
36+
let input = [[2]]
37+
let expected = 10
38+
XCTAssertEqual(s.surfaceArea(input), expected)
39+
}
40+
41+
func testSample2() {
42+
let input = [[1,2],[3,4]]
43+
let expected = 34
44+
XCTAssertEqual(s.surfaceArea(input), expected)
45+
}
46+
47+
func testSample3() {
48+
let input = [[1,0],[0,2]]
49+
let expected = 16
50+
XCTAssertEqual(s.surfaceArea(input), expected)
51+
}
52+
53+
func testSample4() {
54+
let input = [[1,1,1],[1,0,1],[1,1,1]]
55+
let expected = 32
56+
XCTAssertEqual(s.surfaceArea(input), expected)
57+
}
58+
59+
func testSample5() {
60+
let input = [[2,2,2],[2,1,2],[2,2,2]]
61+
let expected = 46
62+
XCTAssertEqual(s.surfaceArea(input), expected)
63+
}
64+
}
65+
66+
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: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// LeetCode: https://leetcode.com/problems/insert-interval/description/
2+
3+
import XCTest
4+
5+
public class Interval {
6+
public var start: Int
7+
public var end: Int
8+
9+
public init(_ start: Int, _ end: Int) {
10+
self.start = start
11+
self.end = end
12+
}
13+
14+
public init(_ array: [Int]) {
15+
self.start = array[0]
16+
self.end = array[1]
17+
}
18+
19+
public func toArray() -> [Int] {
20+
return [self.start, self.end]
21+
}
22+
}
23+
24+
class Solution {
25+
func insert(_ intervals: [Interval], _ newInterval: Interval) -> [Interval] {
26+
guard intervals.count > 0 else {
27+
return [newInterval]
28+
}
29+
var i = 0
30+
var output: [Interval] = []
31+
var newInterval = newInterval
32+
var appendNew = true
33+
while i < intervals.count {
34+
if (intervals[i].end < newInterval.start) || (intervals[i].start > newInterval.end) {
35+
output.append(intervals[i])
36+
i += 1
37+
continue
38+
}
39+
var j = i
40+
while j < intervals.count, !((intervals[j].end < newInterval.start) || (intervals[j].start > newInterval.end)) {
41+
appendNew = false
42+
i = j
43+
newInterval = Interval(min(intervals[j].start, newInterval.start), max(intervals[j].end, newInterval.end))
44+
j += 1
45+
}
46+
i += 1
47+
output.append(newInterval)
48+
}
49+
if appendNew {
50+
output.append(newInterval)
51+
output.sort(by: { 0ドル.start < 1ドル.start })
52+
}
53+
return output
54+
}
55+
}
56+
57+
class Tests: XCTestCase {
58+
let s = Solution()
59+
60+
func testSample1() {
61+
let input = ([Interval(1,3), Interval(6,9)], Interval(2,5))
62+
let expected = [[1,5],[6,9]]
63+
var output: [[Int]] = []
64+
for i in s.insert(input.0, input.1) {
65+
output.append(i.toArray())
66+
}
67+
XCTAssertEqual(output, expected)
68+
}
69+
70+
func testSample2() {
71+
let input = ([Interval(1,2), Interval(3,5), Interval(6,7), Interval(8,10), Interval(12,16)], Interval(4,8))
72+
let expected = [[1,2],[3,10],[12,16]]
73+
var output: [[Int]] = []
74+
for i in s.insert(input.0, input.1) {
75+
output.append(i.toArray())
76+
}
77+
XCTAssertEqual(output, expected)
78+
}
79+
80+
func testSample3() {
81+
let input = ([Interval(1,5)], Interval(6,8))
82+
let expected = [[1,5],[6,8]]
83+
var output: [[Int]] = []
84+
for i in s.insert(input.0, input.1) {
85+
output.append(i.toArray())
86+
}
87+
XCTAssertEqual(output, expected)
88+
}
89+
90+
func testSample4() {
91+
92+
}
93+
}
94+
95+
Tests.defaultTestSuite.run()
96+
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: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// LeetCode: https://leetcode.com/problems/merge-intervals/description/
2+
3+
import XCTest
4+
5+
public class Interval {
6+
public var start: Int
7+
public var end: Int
8+
9+
public init(_ start: Int, _ end: Int) {
10+
self.start = start
11+
self.end = end
12+
}
13+
14+
public init(_ array: [Int]) {
15+
self.start = array[0]
16+
self.end = array[1]
17+
}
18+
19+
public func toArray() -> [Int] {
20+
return [self.start, self.end]
21+
}
22+
}
23+
24+
class Solution {
25+
func merge(_ intervals: [Interval]) -> [Interval] {
26+
var output: [Interval] = []
27+
var sorted = intervals.sorted(by: { 0ドル.start < 1ドル.start })
28+
var i = 0
29+
while i < sorted.count {
30+
var j = i + 1
31+
while j < sorted.count, sorted[i].end >= sorted[j].start {
32+
sorted[i].end = max(sorted[j].end, sorted[i].end)
33+
j += 1
34+
}
35+
output.append(sorted[i])
36+
i = j
37+
}
38+
return output
39+
}
40+
}
41+
42+
class Tests: XCTestCase {
43+
let s = Solution()
44+
45+
func testSample1() {
46+
let input = [Interval(1,3), Interval(2,6), Interval(8,10), Interval(15,18)]
47+
let expected = [[1,6],[8,10],[15,18]]
48+
var output: [[Int]] = []
49+
for i in s.merge(input) {
50+
output.append(i.toArray())
51+
}
52+
XCTAssertEqual(output, expected)
53+
}
54+
55+
func testSample2() {
56+
let input = [Interval(1,4), Interval(4,5)]
57+
let expected = [[1,5]]
58+
var output: [[Int]] = []
59+
for i in s.merge(input) {
60+
output.append(i.toArray())
61+
}
62+
XCTAssertEqual(output, expected)
63+
}
64+
65+
func testSample3() {
66+
let input = [Interval(1,4), Interval(2,3)]
67+
let expected = [[1,4]]
68+
var output: [[Int]] = []
69+
for i in s.merge(input) {
70+
output.append(i.toArray())
71+
}
72+
XCTAssertEqual(output, expected)
73+
}
74+
75+
func testSample4() {
76+
let input = [Interval(1,4), Interval(0,2), Interval(3,5)]
77+
let expected = [[0,5]]
78+
var output: [[Int]] = []
79+
for i in s.merge(input) {
80+
output.append(i.toArray())
81+
}
82+
XCTAssertEqual(output, expected)
83+
}
84+
}
85+
86+
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>

0 commit comments

Comments
(0)

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