Skip to content

Commit 6e22536

Browse files
authored
Merge pull request #1883 from sonjh1217/main
[sonjh1217] WEEK 07 solutions
2 parents a9a1306 + f97fc0a commit 6e22536

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
// O(n) time / O(n) space
3+
func lengthOfLongestSubstring(_ s: String) -> Int {
4+
var lastIndexByCharacter = [Character: Int]()
5+
var start = 0
6+
var maxLenth = 0
7+
8+
for (i, character) in s.enumerated() {
9+
if let lastIndex = lastIndexByCharacter[character],
10+
lastIndex >= start {
11+
start = lastIndex + 1
12+
}
13+
lastIndexByCharacter[character] = i
14+
maxLenth = max(maxLenth, i - start + 1)
15+
}
16+
return maxLenth
17+
}
18+
}
19+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* public var val: Int
5+
* public var next: ListNode?
6+
* public init() { self.val = 0; self.next = nil; }
7+
* public init(_ val: Int) { self.val = val; self.next = nil; }
8+
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
// O(n) time / O(1) space
13+
func reverseList(_ head: ListNode?) -> ListNode? {
14+
var node = head
15+
var lastNode: ListNode? = nil
16+
17+
while node != nil {
18+
let next = node?.next
19+
node?.next = lastNode
20+
lastNode = node
21+
node = next
22+
}
23+
24+
return lastNode
25+
}
26+
}

0 commit comments

Comments
 (0)