Skip to content

Commit 378f82a

Browse files
committed
Improved swift
1 parent 17d2bc6 commit 378f82a

File tree

3 files changed

+42
-40
lines changed

3 files changed

+42
-40
lines changed

pom.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<groupId>com.github.javadev</groupId>
66
<artifactId>leetcode-in-all</artifactId>
77
<packaging>jar</packaging>
8-
<version>1.0</version>
8+
<version>1.1</version>
99
<name>leetcode-in-all</name>
1010
<description>104 LeetCode algorithm problem solutions</description>
1111
<url>https://github.com/javadev/LeetCode-in-All</url>
@@ -88,6 +88,8 @@
8888
<source>src/main/ruby</source>
8989
<source>src/main/scala</source>
9090
<source>src/main/ts</source>
91+
<source>src/main/python</source>
92+
<source>src/main/swift</source>
9193
</sources>
9294
</configuration>
9395
</execution>
Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,36 @@
1-
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Linked_List #Recursion
2-
// #Data_Structure_I_Day_8_Linked_List #Algorithm_I_Day_10_Recursion_Backtracking
3-
// #Level_1_Day_3_Linked_List #Udemy_Linked_List #Big_O_Time_O(N)_Space_O(1)
4-
// #2024_06_28_Time_4_ms_(89.47%)_Space_16_MB_(24.52%)
1+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Depth_First_Search
2+
// #Breadth_First_Search #Matrix #Union_Find
3+
// #Algorithm_II_Day_6_Breadth_First_Search_Depth_First_Search
4+
// #Graph_Theory_I_Day_1_Matrix_Related_Problems #Level_1_Day_9_Graph/BFS/DFS #Udemy_Graph
5+
// #Big_O_Time_O(M*N)_Space_O(M*N) #2024_07_04_Time_152_ms_(70.92%)_Space_19.2_MB_(44.62%)
56

6-
/**
7-
* Definition for singly-linked list.
8-
* public class ListNode {
9-
* public var val: Int
10-
* public var next: ListNode?
11-
* public init() { self.val = 0; self.next = nil; }
12-
* public init(_ val: Int) { self.val = val; self.next = nil; }
13-
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
14-
* }
15-
*/
167
class Solution {
17-
func reverseList(_ head: ListNode?) -> ListNode? {
18-
var prev: ListNode? = nil
19-
var curr = head
20-
var next = head?.next
8+
func numIslands(_ grid: [[Character]]) -> Int {
9+
var grid = grid
10+
var islands = 0
2111

22-
while curr != nil {
12+
if !grid.isEmpty && !grid[0].isEmpty {
13+
for i in 0..<grid.count {
14+
for j in 0..<grid[0].count {
15+
if grid[i][j] == "1" {
16+
dfs(&grid, i, j)
17+
islands += 1
18+
}
19+
}
20+
}
21+
}
22+
return islands
23+
}
2324

24-
// reverse the current nodes link
25-
curr?.next = prev
26-
27-
// update variables
28-
prev = curr
29-
curr = next
30-
next = next?.next
25+
private func dfs(_ grid: inout [[Character]], _ x: Int, _ y: Int) {
26+
if x < 0 || x >= grid.count || y < 0 || y >= grid[0].count || grid[x][y] != "1" {
27+
return
3128
}
3229

33-
return prev
30+
grid[x][y] = "x"
31+
dfs(&grid, x + 1, y)
32+
dfs(&grid, x - 1, y)
33+
dfs(&grid, x, y + 1)
34+
dfs(&grid, x, y - 1)
3435
}
3536
}

src/main/swift/g0501_0600/s0560_subarray_sum_equals_k/Solution.swift

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,18 @@
33

44
class Solution {
55
func subarraySum(_ nums: [Int], _ k: Int) -> Int {
6-
var result = 0
7-
var dict = [Int: Int]()
8-
dict[0] = 1
6+
var result = 0
7+
var dict = [Int: Int]()
8+
dict[0] = 1
99

10-
var sum = 0
11-
for num in nums {
12-
sum += num
13-
if let val = dict[sum - k] {
14-
result += val
10+
var sum = 0
11+
for num in nums {
12+
sum += num
13+
if let val = dict[sum - k] {
14+
result += val
15+
}
16+
dict[sum, default: 0] += 1
1517
}
16-
dict[sum, default: 0] += 1
17-
}
18-
19-
return result
18+
return result
2019
}
2120
}

0 commit comments

Comments
 (0)