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
26 changes: 26 additions & 0 deletions longest-substring-without-repeating-characters/delight010.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
// Time complexity O(N)
// Space complexity O(min(m,n))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let charArray = Array(s)를 하고 있기 때문에 공간 복잡도에서 무조건 String의 길이는 사용이 될거라 O(n) 이지 않을까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다시 보니 그러네요!! 아직 복잡도 계산하는 데 익숙치않아서 클로드의 도움을 받았는 데, 한 번 더 유심히 봐야겠어요. 감사합니다

func lengthOfLongestSubstring(_ s: String) -> Int {
if s.isEmpty {
return 0
}
var maxLength = 0
var startIndex = 0
var charSet: Set<Character> = []
let charArray = Array(s)

for right in 0..<charArray.count {
while charSet.contains(charArray[right]) {
charSet.remove(charArray[startIndex])
startIndex += 1
}

charSet.insert(charArray[right])
maxLength = max(maxLength, right - startIndex + 1)
}

return maxLength
}
}

23 changes: 23 additions & 0 deletions reverse-linked-list/delight010.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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 {
// Time complexity O(n)
// Space complexity O(1)
func reverseList(_ head: ListNode?) -> ListNode? {
var reverseList: ListNode? = nil
var currentHead = head
while let node = currentHead {
currentHead = node.next
node.next = reverseList
reverseList = node
}
return reverseList
}
}

63 changes: 63 additions & 0 deletions set-matrix-zeroes/delight010.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
class Solution {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저와는 다른 접근법으로 푸셔서 많이 배워갑니다..!!! 공간 복잡도나 시간 복잡도를 올려주셔도 좋을 것 같아요!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

추가하였습니다!!

// Time complexity O(MN)
// Space complexity O(1)
func setZeroes(_ matrix: inout [[Int]]) {
var firstRowHasZero = false
var firstColHasZero = false

for col in 0..<matrix[0].count {
if matrix[0][col] == 0 {
firstRowHasZero = true
break
}
}

for row in 0..<matrix.count {
if matrix[row][0] == 0 {
firstColHasZero = true
break
}
}

// marking
for row in 0..<matrix.count {
for col in 0..<matrix[row].count {
if matrix[row][col] == 0 {
matrix[row][0] = 0
matrix[0][col] = 0
}
}
}

// row
for row in 1..<matrix.count {
if matrix[row][0] == 0 {
for col in 0..<matrix[row].count {
matrix[row][col] = 0
}
}
}

// column
for col in 1..<matrix[0].count {
if matrix[0][col] == 0 {
for row in 0..<matrix.count {
matrix[row][col] = 0
}
}
}

if firstRowHasZero {
for col in 0..<matrix[0].count {
matrix[0][col] = 0
}
}

if firstColHasZero {
for row in 0..<matrix.count {
matrix[row][0] = 0
}
}
}
}

15 changes: 15 additions & 0 deletions unique-paths/delight010.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
// Time complexity O(MN)
// Space complexity O(MN)
func uniquePaths(_ m: Int, _ n: Int) -> Int {
let column = Array(repeating: 1, count: n)
var grid: [[Int]] = Array(repeating: column, count: m)
for i in 1..<m {
for j in 1..<n {
grid[i][j] = grid[i - 1][j] + grid[i][j - 1]
}
}
return grid.last!.last!
}
}