Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions longest-substring-without-repeating-characters/sonjh1217.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
// O(n) time / O(n) space
func lengthOfLongestSubstring(_ s: String) -> Int {
var lastIndexByCharacter = [Character: Int]()
var start = 0
var maxLenth = 0

for (i, character) in s.enumerated() {
if let lastIndex = lastIndexByCharacter[character],
lastIndex >= start {
start = lastIndex + 1
}
lastIndexByCharacter[character] = i
maxLenth = max(maxLenth, i - start + 1)
}
return maxLenth
}
}

26 changes: 26 additions & 0 deletions reverse-linked-list/sonjh1217.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Definition for singly-linked list.
* public class ListNode {
* public var val: Int
* public var next: ListNode?
* public init() { self.val = 0; self.next = nil; }
* public init(_ val: Int) { self.val = val; self.next = nil; }
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
* }
*/
class Solution {
// O(n) time / O(1) space
func reverseList(_ head: ListNode?) -> ListNode? {
var node = head
var lastNode: ListNode? = nil

while node != nil {
let next = node?.next
node?.next = lastNode
lastNode = node
node = next
}

return lastNode
}
}