Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Swift implementation for LCCI 01.08, 01.09 & 02.01 #2604

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 4 commits into from
Apr 17, 2024
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
29 changes: 29 additions & 0 deletions lcci/01.08.Zero Matrix/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,35 @@ void setZeroes(int** matrix, int matrixSize, int* matrixColSize) {
}
```

```swift
class Solution {
func setZeroes(_ matrix: inout [[Int]]) {
let m = matrix.count
guard m > 0 else { return }
let n = matrix[0].count
var rows = Array(repeating: false, count: m)
var cols = Array(repeating: false, count: n)

for i in 0..<m {
for j in 0..<n {
if matrix[i][j] == 0 {
rows[i] = true
cols[j] = true
}
}
}

for i in 0..<m {
for j in 0..<n {
if rows[i] || cols[j] {
matrix[i][j] = 0
}
}
}
}
}
```

<!-- tabs:end -->

### 方法二:原地标记
Expand Down
29 changes: 29 additions & 0 deletions lcci/01.08.Zero Matrix/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,35 @@ void setZeroes(int** matrix, int matrixSize, int* matrixColSize) {
}
```

```swift
class Solution {
func setZeroes(_ matrix: inout [[Int]]) {
let m = matrix.count
guard m > 0 else { return }
let n = matrix[0].count
var rows = Array(repeating: false, count: m)
var cols = Array(repeating: false, count: n)

for i in 0..<m {
for j in 0..<n {
if matrix[i][j] == 0 {
rows[i] = true
cols[j] = true
}
}
}

for i in 0..<m {
for j in 0..<n {
if rows[i] || cols[j] {
matrix[i][j] = 0
}
}
}
}
}
```

<!-- tabs:end -->

### Solution 2: In-place Marking
Expand Down
26 changes: 26 additions & 0 deletions lcci/01.08.Zero Matrix/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
func setZeroes(_ matrix: inout [[Int]]) {
let m = matrix.count
guard m > 0 else { return }
let n = matrix[0].count
var rows = Array(repeating: false, count: m)
var cols = Array(repeating: false, count: n)

for i in 0..<m {
for j in 0..<n {
if matrix[i][j] == 0 {
rows[i] = true
cols[j] = true
}
}
}

for i in 0..<m {
for j in 0..<n {
if rows[i] || cols[j] {
matrix[i][j] = 0
}
}
}
}
}
8 changes: 8 additions & 0 deletions lcci/01.09.String Rotation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ impl Solution {
}
```

```swift
class Solution {
func isFlippedString(_ s1: String, _ s2: String) -> Bool {
return (s1.isEmpty && s2.isEmpty) || (s1.count == s2.count && (s1 + s1).contains(s2))
}
}
```

<!-- tabs:end -->

<!-- end -->
8 changes: 8 additions & 0 deletions lcci/01.09.String Rotation/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ impl Solution {
}
```

```swift
class Solution {
func isFlippedString(_ s1: String, _ s2: String) -> Bool {
return (s1.isEmpty && s2.isEmpty) || (s1.count == s2.count && (s1 + s1).contains(s2))
}
}
```

<!-- tabs:end -->

<!-- end -->
5 changes: 5 additions & 0 deletions lcci/01.09.String Rotation/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Solution {
func isFlippedString(_ s1: String, _ s2: String) -> Bool {
return (s1.isEmpty && s2.isEmpty) || (s1.count == s2.count && (s1 + s1).contains(s2))
}
}
32 changes: 32 additions & 0 deletions lcci/02.01.Remove Duplicate Node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,38 @@ var removeDuplicateNodes = function (head) {
};
```

```swift
/**
* Definition for singly-linked list.
* public class ListNode {
* var val: Int
* var next: ListNode?
* init(_ x: Int, _ next: ListNode? = nil) {
* self.val = x
* self.next = next
* }
* }
*/

class Solution {
func removeDuplicateNodes(_ head: ListNode?) -> ListNode? {
var vis = Set<Int>()
let pre = ListNode(0, head)
var current: ListNode? = pre

while current?.next != nil {
if vis.insert(current!.next!.val).inserted {
current = current?.next
} else {
current?.next = current?.next?.next
}
}

return head
}
}
```

<!-- tabs:end -->

<!-- end -->
32 changes: 32 additions & 0 deletions lcci/02.01.Remove Duplicate Node/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,38 @@ var removeDuplicateNodes = function (head) {
};
```

```swift
/**
* Definition for singly-linked list.
* public class ListNode {
* var val: Int
* var next: ListNode?
* init(_ x: Int, _ next: ListNode? = nil) {
* self.val = x
* self.next = next
* }
* }
*/

class Solution {
func removeDuplicateNodes(_ head: ListNode?) -> ListNode? {
var vis = Set<Int>()
let pre = ListNode(0, head)
var current: ListNode? = pre

while current?.next != nil {
if vis.insert(current!.next!.val).inserted {
current = current?.next
} else {
current?.next = current?.next?.next
}
}

return head
}
}
```

<!-- tabs:end -->

<!-- end -->
29 changes: 29 additions & 0 deletions lcci/02.01.Remove Duplicate Node/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Definition for singly-linked list.
* public class ListNode {
* var val: Int
* var next: ListNode?
* init(_ x: Int, _ next: ListNode? = nil) {
* self.val = x
* self.next = next
* }
* }
*/

class Solution {
func removeDuplicateNodes(_ head: ListNode?) -> ListNode? {
var vis = Set<Int>()
let pre = ListNode(0, head)
var current: ListNode? = pre

while current?.next != nil {
if vis.insert(current!.next!.val).inserted {
current = current?.next
} else {
current?.next = current?.next?.next
}
}

return head
}
}