Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Sri Hari: Batch-6/Neetcode-150/Added-SwiftCode #3936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Mar 23, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Batch-6/Neetcode-150/Added-swiftcode
  • Loading branch information
Srihari2222 committed Mar 12, 2025
commit d3d3398faaf825c20d049734db700182a4948d1f
74 changes: 73 additions & 1 deletion articles/add-two-numbers.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,40 @@ class Solution {
}
```

```swift
/**
* 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 {
private func add(_ l1: ListNode?, _ l2: ListNode?, _ carry: Int) -> ListNode? {
if l1 == nil && l2 == nil && carry == 0 {
return nil
}

let v1 = l1?.val ?? 0
let v2 = l2?.val ?? 0

let sum = v1 + v2 + carry
let newCarry = sum / 10
let val = sum % 10

let nextNode = add(l1?.next, l2?.next, newCarry)
return ListNode(val, nextNode)
}

func addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {
return add(l1, l2, 0)
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -577,11 +611,49 @@ class Solution {
}
```

```swift
/**
* 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 {
func addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {
let dummy = ListNode(0)
var cur = dummy
var l1 = l1, l2 = l2
var carry = 0

while l1 != nil || l2 != nil || carry != 0 {
let v1 = l1?.val ?? 0
let v2 = l2?.val ?? 0

let sum = v1 + v2 + carry
carry = sum / 10
let val = sum % 10
cur.next = ListNode(val)

cur = cur.next!
l1 = l1?.next
l2 = l2?.next
}
return dummy.next
}
}
```

::tabs-end

### Time & Space Complexity

* Time complexity: $O(m + n)$
* Space complexity: $O(1)$
* Space complexity:
* $O(1)$ extra space.
* $O(max(m, n))$ for the output list.

> Where $m$ is the length of $l1$ and $n$ is the length of $l2$.
90 changes: 90 additions & 0 deletions articles/binary-search.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,29 @@ class Solution {
}
```

```swift
class Solution {
func binarySearch(_ l: Int, _ r: Int, _ nums: [Int], _ target: Int) -> Int {
if l > r {
return -1
}
let m = l + (r - l) / 2

if nums[m] == target {
return m
}
if nums[m] < target {
return binarySearch(m + 1, r, nums, target)
}
return binarySearch(l, m - 1, nums, target)
}

func search(_ nums: [Int], _ target: Int) -> Int {
return binarySearch(0, nums.count - 1, nums, target)
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -296,6 +319,28 @@ class Solution {
}
```

```swift
class Solution {
func search(_ nums: [Int], _ target: Int) -> Int {
var l = 0, r = nums.count - 1

while l <= r {
// (l + r) // 2 can lead to overflow
let m = l + (r - l) / 2

if nums[m] > target {
r = m - 1
} else if nums[m] < target {
l = m + 1
} else {
return m
}
}
return -1
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -439,6 +484,24 @@ class Solution {
}
```

```swift
class Solution {
func search(_ nums: [Int], _ target: Int) -> Int {
var l = 0, r = nums.count

while l < r {
let m = l + (r - l) / 2
if nums[m] > target {
r = m
} else {
l = m + 1
}
}
return (l > 0 && nums[l - 1] == target) ? l - 1 : -1
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -582,6 +645,24 @@ class Solution {
}
```

```swift
class Solution {
func search(_ nums: [Int], _ target: Int) -> Int {
var l = 0, r = nums.count

while l < r {
let m = l + (r - l) / 2
if nums[m] >= target {
r = m
} else {
l = m + 1
}
}
return (l < nums.count && nums[l] == target) ? l : -1
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -664,6 +745,15 @@ class Solution {
}
```

```swift
class Solution {
func search(_ nums: [Int], _ target: Int) -> Int {
let index = nums.partitioningIndex { $0 >= target }
return (index < nums.count && nums[index] == target) ? index : -1
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down
Loading