Skip to content

Commit e1beca0

Browse files
committed
feat: add Spiral Matrix
1 parent 4cde71e commit e1beca0

5 files changed

Lines changed: 140 additions & 0 deletions

File tree

Help.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
5E2CF7C6266E242E007BB2EB /* segue-001.png in Resources */ = {isa = PBXBuildFile; fileRef = 5E2CF7C4266E242E007BB2EB /* segue-001.png */; };
1818
5E2CF7C8266E61A0007BB2EB /* PrepareSegue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E2CF7C7266E61A0007BB2EB /* PrepareSegue.swift */; };
1919
5E2CF7CA266EB745007BB2EB /* PerformSegue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E2CF7C9266EB745007BB2EB /* PerformSegue.swift */; };
20+
5E4D3FCB278FA2F800A0861A /* SpiralMatrix.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E4D3FCA278FA2F800A0861A /* SpiralMatrix.swift */; };
21+
5E4D3FCE278FA3FB00A0861A /* spiral1.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 5E4D3FCC278FA3FB00A0861A /* spiral1.jpg */; };
22+
5E4D3FCF278FA3FB00A0861A /* spiral.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 5E4D3FCD278FA3FB00A0861A /* spiral.jpg */; };
2023
5E5974F526AAAA46006DD0DC /* CustomView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E5974F426AAAA46006DD0DC /* CustomView.swift */; };
2124
5E5FE82426959E5100C5A396 /* Equatable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E5FE82326959E5100C5A396 /* Equatable.swift */; };
2225
5E5FE8262695A27B00C5A396 /* Comparable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E5FE8252695A27B00C5A396 /* Comparable.swift */; };
@@ -68,6 +71,9 @@
6871
5E2CF7C4266E242E007BB2EB /* segue-001.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "segue-001.png"; sourceTree = "<group>"; };
6972
5E2CF7C7266E61A0007BB2EB /* PrepareSegue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrepareSegue.swift; sourceTree = "<group>"; };
7073
5E2CF7C9266EB745007BB2EB /* PerformSegue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PerformSegue.swift; sourceTree = "<group>"; };
74+
5E4D3FCA278FA2F800A0861A /* SpiralMatrix.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpiralMatrix.swift; sourceTree = "<group>"; };
75+
5E4D3FCC278FA3FB00A0861A /* spiral1.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = spiral1.jpg; sourceTree = "<group>"; };
76+
5E4D3FCD278FA3FB00A0861A /* spiral.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = spiral.jpg; sourceTree = "<group>"; };
7177
5E5974F426AAAA46006DD0DC /* CustomView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomView.swift; sourceTree = "<group>"; };
7278
5E5FE82326959E5100C5A396 /* Equatable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Equatable.swift; sourceTree = "<group>"; };
7379
5E5FE8252695A27B00C5A396 /* Comparable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Comparable.swift; sourceTree = "<group>"; };
@@ -133,6 +139,7 @@
133139
isa = PBXGroup;
134140
children = (
135141
5E1F30D5278C9C5E009446ED /* Pascal'sTriangle.swift */,
142+
5E4D3FCA278FA2F800A0861A /* SpiralMatrix.swift */,
136143
);
137144
path = LeetCode;
138145
sourceTree = "<group>";
@@ -157,6 +164,8 @@
157164
5E2CF7C2266E23D7007BB2EB /* Screenshots */ = {
158165
isa = PBXGroup;
159166
children = (
167+
5E4D3FCD278FA3FB00A0861A /* spiral.jpg */,
168+
5E4D3FCC278FA3FB00A0861A /* spiral1.jpg */,
160169
5E2CF7C4266E242E007BB2EB /* segue-001.png */,
161170
5E2CF7C3266E242E007BB2EB /* segue-002.png */,
162171
5E84F23A26FC424B00BA06D3 /* home-screen-quick-actions.jpeg */,
@@ -357,10 +366,12 @@
357366
5E2CF7C5266E242E007BB2EB /* segue-002.png in Resources */,
358367
5E83856F266767D600ACA770 /* .gitignore in Resources */,
359368
5E84F23B26FC424B00BA06D3 /* home-screen-quick-actions.jpeg in Resources */,
369+
5E4D3FCE278FA3FB00A0861A /* spiral1.jpg in Resources */,
360370
5E83855F2667635400ACA770 /* LaunchScreen.storyboard in Resources */,
361371
5E8385712667681600ACA770 /* README.md in Resources */,
362372
5E83855C2667635400ACA770 /* Assets.xcassets in Resources */,
363373
5E709213278CA0BA0060FB31 /* PascalTriangleAnimated2.gif in Resources */,
374+
5E4D3FCF278FA3FB00A0861A /* spiral.jpg in Resources */,
364375
5ED4FE63270920F700E3B850 /* License in Resources */,
365376
5E83855A2667635000ACA770 /* Main.storyboard in Resources */,
366377
5E2CF7C6266E242E007BB2EB /* segue-001.png in Resources */,
@@ -375,6 +386,7 @@
375386
buildActionMask = 2147483647;
376387
files = (
377388
5EDE65CE26E29E0000DB71C0 /* SwipeScreen.swift in Sources */,
389+
5E4D3FCB278FA2F800A0861A /* SpiralMatrix.swift in Sources */,
378390
5E8385862667BE7500ACA770 /* Int+Random.swift in Sources */,
379391
5E1F2F4927873121009446ED /* FactoryMethod.swift in Sources */,
380392
5EAC92C726BD35E700A6198C /* SaveAndLoadDataOnDevice.swift in Sources */,

Help/LeetCode/SpiralMatrix.swift

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//
2+
// SpiralMatrix.swift
3+
// Help
4+
//
5+
// Created by Sergey Lukaschuk on 13.01.2022.
6+
//
7+
8+
import Foundation
9+
10+
11+
// MARK: Spiral Matrix
12+
13+
/*
14+
Given an m x n matrix, return all elements of the matrix in spiral order.
15+
16+
Example 1
17+
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
18+
Output: [1,2,3,6,9,8,7,4,5]
19+
20+
Example 2
21+
Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
22+
Output: [1,2,3,4,8,12,11,10,9,5,6,7]
23+
*/
24+
25+
func spiralOrder(_ matrix: [[Int]]) -> [Int] {
26+
var result = [Int]()
27+
28+
if matrix.count == 0 || matrix[0].count == 0 {
29+
return result
30+
}
31+
32+
var left = 0
33+
var bottom = matrix.count - 1
34+
var top = 0
35+
var right = matrix[0].count - 1
36+
37+
while (left <= bottom && top <= right) {
38+
// Go left to right
39+
for col in stride(from: top, through: right, by: 1) {
40+
result.append(matrix[left][col])
41+
}
42+
// Go top to down
43+
left += 1
44+
for row in stride(from: left, through: bottom, by: 1) {
45+
result.append(matrix[row][right])
46+
}
47+
// Go right to left
48+
right -= 1
49+
if left <= bottom {
50+
for col in stride(from: right, through: top, by: -1) {
51+
result.append(matrix[bottom][col])
52+
}
53+
bottom -= 1
54+
}
55+
// Go up
56+
if top <= right {
57+
for row in stride(from: bottom, through: left, by: -1) {
58+
result.append(matrix[row][top])
59+
}
60+
top += 1
61+
}
62+
}
63+
return result
64+
}

README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ In this project, I have collected various best practices and iOS development tip
6363

6464
- [**Leet Code**](#leet-code)
6565
- [Pascal Triangle](#pascal-triangle)
66+
- [Spiral Matrix](#spiral-matrix)
6667

6768

6869
## **Clean Code**
@@ -1495,6 +1496,69 @@ func generate(_ numRows: Int) -> [[Int]] {
14951496
```
14961497

14971498

1499+
1500+
### [Spiral Matrix](https://github.com/lgreydev/Help/blob/master/Help/LeetCode/SpiralMatrix.swift)
1501+
1502+
Given an m x n matrix, return all elements of the matrix in spiral order.<br>
1503+
1504+
<img src="https://github.com/lgreydev/Help/blob/master/Screenshots/spiral.jpg" width="350">
1505+
<img src="https://github.com/lgreydev/Help/blob/master/Screenshots/spiral1.jpg" width="350">
1506+
1507+
**Example 1:**<br>
1508+
*Input:* `matrix = [[1,2,3],[4,5,6],[7,8,9]]`<br>
1509+
*Output:* `[1,2,3,6,9,8,7,4,5]`<br>
1510+
1511+
**Example 2:**<br>
1512+
*Input:* `matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]`<br>
1513+
*Output:* `[1,2,3,4,8,12,11,10,9,5,6,7]`
1514+
1515+
``` swift
1516+
1517+
func spiralOrder(_ matrix: [[Int]]) -> [Int] {
1518+
var result = [Int]()
1519+
1520+
if matrix.count == 0 || matrix[0].count == 0 {
1521+
return result
1522+
}
1523+
1524+
var left = 0
1525+
var bottom = matrix.count - 1
1526+
var top = 0
1527+
var right = matrix[0].count - 1
1528+
1529+
while (left <= bottom && top <= right) {
1530+
// Go left to right
1531+
for col in stride(from: top, through: right, by: 1) {
1532+
result.append(matrix[left][col])
1533+
}
1534+
// Go top to down
1535+
left += 1
1536+
for row in stride(from: left, through: bottom, by: 1) {
1537+
result.append(matrix[row][right])
1538+
}
1539+
// Go right to left
1540+
right -= 1
1541+
if left <= bottom {
1542+
for col in stride(from: right, through: top, by: -1) {
1543+
result.append(matrix[bottom][col])
1544+
}
1545+
bottom -= 1
1546+
}
1547+
// Go up
1548+
if top <= right {
1549+
for row in stride(from: bottom, through: left, by: -1) {
1550+
result.append(matrix[row][top])
1551+
}
1552+
top += 1
1553+
}
1554+
}
1555+
return result
1556+
}
1557+
1558+
```
1559+
1560+
1561+
14981562
### 🛡️ License
14991563

15001564
This project is licensed under the MIT License - see the [`LICENSE`](https://github.com/lgreydev/Help/blob/master/License) file for details.

Screenshots/spiral.jpg

13.8 KB
Loading

Screenshots/spiral1.jpg

9.92 KB
Loading

0 commit comments

Comments
 (0)