Skip to content

Commit 678ecce

Browse files
committed
Added swift solutions
1 parent 58c9aff commit 678ecce

File tree

72 files changed

+5907
-56
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+5907
-56
lines changed

README.md

+53-53
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-All?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-All/fork)
33
> ["For coding interview preparation, LeetCode is one of the best online resource providing a rich library of more than 300 real coding interview questions for you to practice from using one of the 7 supported languages - C, C++, Java, Python, C#, JavaScript, Ruby."](https://www.quora.com/How-effective-is-Leetcode-for-preparing-for-technical-interviews)
44
5-
* [SQL I](#sql-i)
65
* [Level 1](#level-1)
76
* [Level 2](#level-2)
87
* [Udemy](#udemy)
@@ -16,58 +15,7 @@
1615
* [Programming Skills I](#programming-skills-i)
1716
* [Programming Skills II](#programming-skills-ii)
1817
* [Graph Theory I](#graph-theory-i)
19-
20-
### SQL I
21-
22-
#### Day 1 Select
23-
24-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
25-
|-|-|-|-|-|-|-
26-
27-
#### Day 2 Select and Order
28-
29-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
30-
|-|-|-|-|-|-|-
31-
32-
#### Day 3 String Processing Functions
33-
34-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
35-
|-|-|-|-|-|-|-
36-
37-
#### Day 4 Union and Select
38-
39-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
40-
|-|-|-|-|-|-|-
41-
42-
#### Day 5 Union
43-
44-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
45-
|-|-|-|-|-|-|-
46-
47-
#### Day 6 Union
48-
49-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
50-
|-|-|-|-|-|-|-
51-
52-
#### Day 7 Function
53-
54-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
55-
|-|-|-|-|-|-|-
56-
57-
#### Day 8 Function
58-
59-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
60-
|-|-|-|-|-|-|-
61-
62-
#### Day 9 Control of Flow
63-
64-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
65-
|-|-|-|-|-|-|-
66-
67-
#### Day 10 Where
68-
69-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
70-
|-|-|-|-|-|-|-
18+
* [SQL I](#sql-i)
7119

7220
### Level 1
7321

@@ -1398,6 +1346,58 @@
13981346
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
13991347
|-|-|-|-|-|-|-
14001348

1349+
### SQL I
1350+
1351+
#### Day 1 Select
1352+
1353+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1354+
|-|-|-|-|-|-|-
1355+
1356+
#### Day 2 Select and Order
1357+
1358+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1359+
|-|-|-|-|-|-|-
1360+
1361+
#### Day 3 String Processing Functions
1362+
1363+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1364+
|-|-|-|-|-|-|-
1365+
1366+
#### Day 4 Union and Select
1367+
1368+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1369+
|-|-|-|-|-|-|-
1370+
1371+
#### Day 5 Union
1372+
1373+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1374+
|-|-|-|-|-|-|-
1375+
1376+
#### Day 6 Union
1377+
1378+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1379+
|-|-|-|-|-|-|-
1380+
1381+
#### Day 7 Function
1382+
1383+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1384+
|-|-|-|-|-|-|-
1385+
1386+
#### Day 8 Function
1387+
1388+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1389+
|-|-|-|-|-|-|-
1390+
1391+
#### Day 9 Control of Flow
1392+
1393+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1394+
|-|-|-|-|-|-|-
1395+
1396+
#### Day 10 Where
1397+
1398+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1399+
|-|-|-|-|-|-|-
1400+
14011401
## Algorithms
14021402

14031403
| # | Title | Difficulty | Tag | Time, ms | Time, %

src/main/swift/g0001_0100/s0002_add_two_numbers/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ To solve the Add Two Numbers problem in Swift using a `Solution` class, we'll fo
5353
Here's the implementation:
5454

5555
```swift
56-
/*
56+
/**
5757
* Definition for singly-linked list.
5858
* public class ListNode {
5959
* public var val: Int

src/main/swift/g0001_0100/s0024_swap_nodes_in_pairs/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ To solve the "Swap Nodes in Pairs" problem in Swift with a `Solution` class, we
5050
Here's the implementation:
5151

5252
```swift
53-
/*
53+
/**
5454
* Definition for singly-linked list.
5555
* public class ListNode {
5656
* public var val: Int

src/main/swift/g0001_0100/s0025_reverse_nodes_in_k_group/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ To solve the "Reverse Nodes in k-Group" problem in Swift with a `Solution` class
6666
Here's the implementation:
6767

6868
```swift
69-
/*
69+
/**
7070
* Definition for singly-linked list.
7171
* public class ListNode {
7272
* public var val: Int
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-All?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-All)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-All?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-All/fork)
3+
4+
## 62\. Unique Paths
5+
6+
Medium
7+
8+
A robot is located at the top-left corner of a `m x n` grid (marked 'Start' in the diagram below).
9+
10+
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
11+
12+
How many possible unique paths are there?
13+
14+
**Example 1:**
15+
16+
![](https://assets.leetcode.com/uploads/2018/10/22/robot_maze.png)
17+
18+
**Input:** m = 3, n = 7
19+
20+
**Output:** 28
21+
22+
**Example 2:**
23+
24+
**Input:** m = 3, n = 2
25+
26+
**Output:** 3
27+
28+
**Explanation:**
29+
30+
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
31+
1. Right -> Down -> Down
32+
2. Down -> Down -> Right
33+
3. Down -> Right -> Down
34+
35+
**Example 3:**
36+
37+
**Input:** m = 7, n = 3
38+
39+
**Output:** 28
40+
41+
**Example 4:**
42+
43+
**Input:** m = 3, n = 3
44+
45+
**Output:** 6
46+
47+
**Constraints:**
48+
49+
* `1 <= m, n <= 100`
50+
* It's guaranteed that the answer will be less than or equal to <code>2 * 10<sup>9</sup></code>.
51+
52+
To solve the "Unique Paths" problem in Swift with the Solution class, follow these steps:
53+
54+
1. Define a method `uniquePaths` in the `Solution` class that takes two integers `m` and `n` as input and returns the number of unique paths from the top-left corner to the bottom-right corner of an `m x n` grid.
55+
2. Initialize a 2D array `dp` of size `m x n` to store the number of unique paths for each position in the grid.
56+
3. Initialize the first row and first column of `dp` to 1 since there is only one way to reach any position in the first row or column (by moving only right or down).
57+
4. Iterate over each position `(i, j)` in the grid, starting from the second row and second column:
58+
- Update `dp[i][j]` by adding the number of unique paths from the cell above `(i-1, j)` and the cell to the left `(i, j-1)`.
59+
5. Return the value of `dp[m-1][n-1]`, which represents the number of unique paths to reach the bottom-right corner of the grid.
60+
61+
Here's the implementation of the `uniquePaths` method in Swift:
62+
63+
```swift
64+
public class Solution {
65+
public func uniquePaths(_ m: Int, _ n: Int) -> Int {
66+
var dp = Array(repeating: Array(repeating: 0, count: n), count: m)
67+
68+
for i in 0..<m {
69+
dp[i][0] = 1
70+
}
71+
72+
for j in 0..<n {
73+
dp[0][j] = 1
74+
}
75+
76+
for i in 1..<m {
77+
for j in 1..<n {
78+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
79+
}
80+
}
81+
82+
return dp[m - 1][n - 1]
83+
}
84+
}
85+
```
86+
87+
This implementation efficiently calculates the number of unique paths using dynamic programming, with a time complexity of O(m * n) and a space complexity of O(m * n).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-All?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-All)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-All?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-All/fork)
3+
4+
## 64\. Minimum Path Sum
5+
6+
Medium
7+
8+
Given a `m x n` `grid` filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path.
9+
10+
**Note:** You can only move either down or right at any point in time.
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2020/11/05/minpath.jpg)
15+
16+
**Input:** grid = \[\[1,3,1],[1,5,1],[4,2,1]]
17+
18+
**Output:** 7
19+
20+
**Explanation:** Because the path 1 → 3 → 1 → 1 → 1 minimizes the sum.
21+
22+
**Example 2:**
23+
24+
**Input:** grid = \[\[1,2,3],[4,5,6]]
25+
26+
**Output:** 12
27+
28+
**Constraints:**
29+
30+
* `m == grid.length`
31+
* `n == grid[i].length`
32+
* `1 <= m, n <= 200`
33+
* `0 <= grid[i][j] <= 100`
34+
35+
To solve the "Minimum Path Sum" problem in Swift with the Solution class, follow these steps:
36+
37+
1. Define a method `minPathSum` in the `Solution` class that takes a 2D grid of non-negative numbers as input and returns the minimum sum of all numbers along the path from the top-left corner to the bottom-right corner of the grid.
38+
2. Initialize a 2D array `dp` of size `m x n`, where `dp[i][j]` represents the minimum sum of the path from the top-left corner to position `(i, j)` in the grid.
39+
3. Initialize `dp[0][0]` to the value of the top-left cell in the grid.
40+
4. Initialize the first row and first column of `dp` based on the grid values and the previous cells in the same row or column.
41+
5. Iterate over each position `(i, j)` in the grid, starting from the second row and second column:
42+
- Update `dp[i][j]` by adding the current grid value at `(i, j)` to the minimum of the values of the previous cells `(i-1, j)` and `(i, j-1)` in `dp`.
43+
6. Return `dp[m-1][n-1]`, which represents the minimum path sum from the top-left corner to the bottom-right corner of the grid.
44+
45+
Here's the implementation of the `minPathSum` method in Swift:
46+
47+
```swift
48+
class Solution {
49+
func minPathSum(_ grid: [[Int]]) -> Int {
50+
var matrix: [[Int]] = grid
51+
let n = grid.count - 1
52+
let m = grid[0].count - 1
53+
54+
for i in 0...n {
55+
for j in 0...m {
56+
var step = matrix[i][j]
57+
58+
if i > 0 && j > 0 {
59+
step += min(matrix[i - 1][j], matrix[i][j - 1])
60+
} else if i > 0 && j == 0 {
61+
step += matrix[i - 1][j]
62+
} else if j > 0 && i == 0 {
63+
step += matrix[i][j - 1]
64+
}
65+
66+
matrix[i][j] = step
67+
}
68+
}
69+
70+
return matrix[n][m]
71+
}
72+
}
73+
```
74+
75+
This implementation efficiently calculates the minimum path sum using dynamic programming, with a time complexity of O(m * n) and a space complexity of O(m * n).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-All?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-All)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-All?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-All/fork)
3+
4+
## 70\. Climbing Stairs
5+
6+
Easy
7+
8+
You are climbing a staircase. It takes `n` steps to reach the top.
9+
10+
Each time you can either climb `1` or `2` steps. In how many distinct ways can you climb to the top?
11+
12+
**Example 1:**
13+
14+
**Input:** n = 2
15+
16+
**Output:** 2
17+
18+
**Explanation:** There are two ways to climb to the top. 1. 1 step + 1 step 2. 2 steps
19+
20+
**Example 2:**
21+
22+
**Input:** n = 3
23+
24+
**Output:** 3
25+
26+
**Explanation:** There are three ways to climb to the top. 1. 1 step + 1 step + 1 step 2. 1 step + 2 steps 3. 2 steps + 1 step
27+
28+
**Constraints:**
29+
30+
* `1 <= n <= 45`
31+
32+
To solve the "Climbing Stairs" problem in Swift with the Solution class, follow these steps:
33+
34+
1. Define a method `climbStairs` in the `Solution` class that takes an integer `n` as input and returns the number of distinct ways to climb to the top of the staircase with `n` steps.
35+
2. Initialize an array `dp` of size `n+1` to store the number of distinct ways to reach each step.
36+
3. Set `dp[0] = 1` and `dp[1] = 1` since there is only one way to reach the first and second steps.
37+
4. Iterate over the steps from `2` to `n`:
38+
- At each step `i`, the number of distinct ways to reach step `i` is the sum of the number of ways to reach steps `i-1` and `i-2`.
39+
- Store this sum in `dp[i]`.
40+
5. Return `dp[n]`, which represents the number of distinct ways to climb to the top of the staircase with `n` steps.
41+
42+
Here's the implementation of the `climbStairs` method in Swift:
43+
44+
```swift
45+
public class Solution {
46+
public func climbStairs(_ n: Int) -> Int {
47+
if n < 2 {
48+
return n
49+
}
50+
var cache = [Int](repeating: 0, count: n)
51+
52+
// Initializing the base cases
53+
cache[0] = 1
54+
cache[1] = 2
55+
56+
for i in 2..<n {
57+
cache[i] = cache[i - 1] + cache[i - 2]
58+
}
59+
60+
return cache[n - 1]
61+
}
62+
}
63+
```
64+
65+
This implementation efficiently calculates the number of distinct ways to climb the stairs using dynamic programming, with a time complexity of O(n) and a space complexity of O(n).

0 commit comments

Comments
 (0)