Skip to content

Commit fd7e6f7

Browse files
authoredApr 17, 2024
feat: add swift implementation to lcci problems: No.01.08,01.09,02.01 (doocs#2604)

File tree

9 files changed

+198
-0
lines changed

9 files changed

+198
-0
lines changed
 

‎lcci/01.08.Zero Matrix/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,35 @@ void setZeroes(int** matrix, int matrixSize, int* matrixColSize) {
252252
}
253253
```
254254
255+
```swift
256+
class Solution {
257+
func setZeroes(_ matrix: inout [[Int]]) {
258+
let m = matrix.count
259+
guard m > 0 else { return }
260+
let n = matrix[0].count
261+
var rows = Array(repeating: false, count: m)
262+
var cols = Array(repeating: false, count: n)
263+
264+
for i in 0..<m {
265+
for j in 0..<n {
266+
if matrix[i][j] == 0 {
267+
rows[i] = true
268+
cols[j] = true
269+
}
270+
}
271+
}
272+
273+
for i in 0..<m {
274+
for j in 0..<n {
275+
if rows[i] || cols[j] {
276+
matrix[i][j] = 0
277+
}
278+
}
279+
}
280+
}
281+
}
282+
```
283+
255284
<!-- tabs:end -->
256285

257286
### 方法二:原地标记

‎lcci/01.08.Zero Matrix/README_EN.md

+29
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,35 @@ void setZeroes(int** matrix, int matrixSize, int* matrixColSize) {
279279
}
280280
```
281281
282+
```swift
283+
class Solution {
284+
func setZeroes(_ matrix: inout [[Int]]) {
285+
let m = matrix.count
286+
guard m > 0 else { return }
287+
let n = matrix[0].count
288+
var rows = Array(repeating: false, count: m)
289+
var cols = Array(repeating: false, count: n)
290+
291+
for i in 0..<m {
292+
for j in 0..<n {
293+
if matrix[i][j] == 0 {
294+
rows[i] = true
295+
cols[j] = true
296+
}
297+
}
298+
}
299+
300+
for i in 0..<m {
301+
for j in 0..<n {
302+
if rows[i] || cols[j] {
303+
matrix[i][j] = 0
304+
}
305+
}
306+
}
307+
}
308+
}
309+
```
310+
282311
<!-- tabs:end -->
283312

284313
### Solution 2: In-place Marking

‎lcci/01.08.Zero Matrix/Solution.swift

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
func setZeroes(_ matrix: inout [[Int]]) {
3+
let m = matrix.count
4+
guard m > 0 else { return }
5+
let n = matrix[0].count
6+
var rows = Array(repeating: false, count: m)
7+
var cols = Array(repeating: false, count: n)
8+
9+
for i in 0..<m {
10+
for j in 0..<n {
11+
if matrix[i][j] == 0 {
12+
rows[i] = true
13+
cols[j] = true
14+
}
15+
}
16+
}
17+
18+
for i in 0..<m {
19+
for j in 0..<n {
20+
if rows[i] || cols[j] {
21+
matrix[i][j] = 0
22+
}
23+
}
24+
}
25+
}
26+
}

‎lcci/01.09.String Rotation/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ impl Solution {
102102
}
103103
```
104104

105+
```swift
106+
class Solution {
107+
func isFlippedString(_ s1: String, _ s2: String) -> Bool {
108+
return (s1.isEmpty && s2.isEmpty) || (s1.count == s2.count && (s1 + s1).contains(s2))
109+
}
110+
}
111+
```
112+
105113
<!-- tabs:end -->
106114

107115
<!-- end -->

‎lcci/01.09.String Rotation/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ impl Solution {
102102
}
103103
```
104104

105+
```swift
106+
class Solution {
107+
func isFlippedString(_ s1: String, _ s2: String) -> Bool {
108+
return (s1.isEmpty && s2.isEmpty) || (s1.count == s2.count && (s1 + s1).contains(s2))
109+
}
110+
}
111+
```
112+
105113
<!-- tabs:end -->
106114

107115
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution {
2+
func isFlippedString(_ s1: String, _ s2: String) -> Bool {
3+
return (s1.isEmpty && s2.isEmpty) || (s1.count == s2.count && (s1 + s1).contains(s2))
4+
}
5+
}

‎lcci/02.01.Remove Duplicate Node/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,38 @@ var removeDuplicateNodes = function (head) {
238238
};
239239
```
240240

241+
```swift
242+
/**
243+
* Definition for singly-linked list.
244+
* public class ListNode {
245+
* var val: Int
246+
* var next: ListNode?
247+
* init(_ x: Int, _ next: ListNode? = nil) {
248+
* self.val = x
249+
* self.next = next
250+
* }
251+
* }
252+
*/
253+
254+
class Solution {
255+
func removeDuplicateNodes(_ head: ListNode?) -> ListNode? {
256+
var vis = Set<Int>()
257+
let pre = ListNode(0, head)
258+
var current: ListNode? = pre
259+
260+
while current?.next != nil {
261+
if vis.insert(current!.next!.val).inserted {
262+
current = current?.next
263+
} else {
264+
current?.next = current?.next?.next
265+
}
266+
}
267+
268+
return head
269+
}
270+
}
271+
```
272+
241273
<!-- tabs:end -->
242274

243275
<!-- end -->

‎lcci/02.01.Remove Duplicate Node/README_EN.md

+32
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,38 @@ var removeDuplicateNodes = function (head) {
243243
};
244244
```
245245

246+
```swift
247+
/**
248+
* Definition for singly-linked list.
249+
* public class ListNode {
250+
* var val: Int
251+
* var next: ListNode?
252+
* init(_ x: Int, _ next: ListNode? = nil) {
253+
* self.val = x
254+
* self.next = next
255+
* }
256+
* }
257+
*/
258+
259+
class Solution {
260+
func removeDuplicateNodes(_ head: ListNode?) -> ListNode? {
261+
var vis = Set<Int>()
262+
let pre = ListNode(0, head)
263+
var current: ListNode? = pre
264+
265+
while current?.next != nil {
266+
if vis.insert(current!.next!.val).inserted {
267+
current = current?.next
268+
} else {
269+
current?.next = current?.next?.next
270+
}
271+
}
272+
273+
return head
274+
}
275+
}
276+
```
277+
246278
<!-- tabs:end -->
247279

248280
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* var val: Int
5+
* var next: ListNode?
6+
* init(_ x: Int, _ next: ListNode? = nil) {
7+
* self.val = x
8+
* self.next = next
9+
* }
10+
* }
11+
*/
12+
13+
class Solution {
14+
func removeDuplicateNodes(_ head: ListNode?) -> ListNode? {
15+
var vis = Set<Int>()
16+
let pre = ListNode(0, head)
17+
var current: ListNode? = pre
18+
19+
while current?.next != nil {
20+
if vis.insert(current!.next!.val).inserted {
21+
current = current?.next
22+
} else {
23+
current?.next = current?.next?.next
24+
}
25+
}
26+
27+
return head
28+
}
29+
}

0 commit comments

Comments
 (0)
Please sign in to comment.