Skip to content

Commit 672aeba

Browse files
authored
Merge pull request #1884 from delight010/main
[SeongA] WEEK 07 solutions
2 parents 426b8a7 + 3469daf commit 672aeba

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
// Time complexity O(N)
3+
// Space complexity O(min(m,n))
4+
func lengthOfLongestSubstring(_ s: String) -> Int {
5+
if s.isEmpty {
6+
return 0
7+
}
8+
var maxLength = 0
9+
var startIndex = 0
10+
var charSet: Set<Character> = []
11+
let charArray = Array(s)
12+
13+
for right in 0..<charArray.count {
14+
while charSet.contains(charArray[right]) {
15+
charSet.remove(charArray[startIndex])
16+
startIndex += 1
17+
}
18+
19+
charSet.insert(charArray[right])
20+
maxLength = max(maxLength, right - startIndex + 1)
21+
}
22+
23+
return maxLength
24+
}
25+
}
26+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class ListNode {
2+
public var val: Int
3+
public var next: ListNode?
4+
public init() { self.val = 0; self.next = nil; }
5+
public init(_ val: Int) { self.val = val; self.next = nil; }
6+
public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
7+
}
8+
9+
class Solution {
10+
// Time complexity O(n)
11+
// Space complexity O(1)
12+
func reverseList(_ head: ListNode?) -> ListNode? {
13+
var reverseList: ListNode? = nil
14+
var currentHead = head
15+
while let node = currentHead {
16+
currentHead = node.next
17+
node.next = reverseList
18+
reverseList = node
19+
}
20+
return reverseList
21+
}
22+
}
23+

set-matrix-zeroes/delight010.swift

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
class Solution {
2+
// Time complexity O(MN)
3+
// Space complexity O(1)
4+
func setZeroes(_ matrix: inout [[Int]]) {
5+
var firstRowHasZero = false
6+
var firstColHasZero = false
7+
8+
for col in 0..<matrix[0].count {
9+
if matrix[0][col] == 0 {
10+
firstRowHasZero = true
11+
break
12+
}
13+
}
14+
15+
for row in 0..<matrix.count {
16+
if matrix[row][0] == 0 {
17+
firstColHasZero = true
18+
break
19+
}
20+
}
21+
22+
// marking
23+
for row in 0..<matrix.count {
24+
for col in 0..<matrix[row].count {
25+
if matrix[row][col] == 0 {
26+
matrix[row][0] = 0
27+
matrix[0][col] = 0
28+
}
29+
}
30+
}
31+
32+
// row
33+
for row in 1..<matrix.count {
34+
if matrix[row][0] == 0 {
35+
for col in 0..<matrix[row].count {
36+
matrix[row][col] = 0
37+
}
38+
}
39+
}
40+
41+
// column
42+
for col in 1..<matrix[0].count {
43+
if matrix[0][col] == 0 {
44+
for row in 0..<matrix.count {
45+
matrix[row][col] = 0
46+
}
47+
}
48+
}
49+
50+
if firstRowHasZero {
51+
for col in 0..<matrix[0].count {
52+
matrix[0][col] = 0
53+
}
54+
}
55+
56+
if firstColHasZero {
57+
for row in 0..<matrix.count {
58+
matrix[row][0] = 0
59+
}
60+
}
61+
}
62+
}
63+

unique-paths/delight010.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
// Time complexity O(MN)
3+
// Space complexity O(MN)
4+
func uniquePaths(_ m: Int, _ n: Int) -> Int {
5+
let column = Array(repeating: 1, count: n)
6+
var grid: [[Int]] = Array(repeating: column, count: m)
7+
for i in 1..<m {
8+
for j in 1..<n {
9+
grid[i][j] = grid[i - 1][j] + grid[i][j - 1]
10+
}
11+
}
12+
return grid.last!.last!
13+
}
14+
}
15+

0 commit comments

Comments
 (0)