File tree Expand file tree Collapse file tree 2 files changed +45
-0
lines changed
longest-substring-without-repeating-characters Expand file tree Collapse file tree 2 files changed +45
-0
lines changed Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments