Skip to content

Commit 587ff62

Browse files
committed
The LeetCode problem [53, 54]'s Swift solution.
1 parent feb8c1b commit 587ff62

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
[38. Count and Say](https://leetcode.com/problems/count-and-say) [[C](./src/38.c)]
2727

28+
[53. Maximum Subarray](https://leetcode.com/problems/maximum-subarray) [[Swift](./swift/53.swift)]
29+
2830
## Medium
2931
[2. Add Two Numbers](https://leetcode.com/problems/add-two-numbers) [[C](./src/2.c)]
3032

@@ -80,6 +82,8 @@
8082

8183
[50. Pow(x, n)](https://leetcode.com/problems/powx-n) [[C](./src/50.c)]
8284

85+
[54. Spiral Matrix](https://leetcode.com/problems/spiral-matrix) [[Swift](./swift/54.swift)]
86+
8387
## Hard
8488
[4. Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays) [[C](./src/4.c)]
8589

swift/53.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
// 53. Maximum Subarray
3+
4+
class Solution {
5+
func maxSubArrayDC(_ nums: [Int], x: Int, y: Int) -> Int {
6+
var v: Int, L: Int, R: Int, maxs: Int
7+
if x + 1 == y {
8+
return nums[x]
9+
}
10+
11+
let m = (x + y) >> 1
12+
maxs = max(maxSubArrayDC(nums, x: x, y: m), maxSubArrayDC(nums, x: m, y: y))
13+
14+
v = 0
15+
L = nums[m - 1]
16+
for i in stride(from: m - 1, to: x - 1, by: -1) {
17+
v += nums[i]
18+
L = max(L, v)
19+
}
20+
21+
v = 0
22+
R = nums[m]
23+
for i in m ..< y {
24+
v += nums[i]
25+
R = max(R, v)
26+
}
27+
28+
return max(maxs, L + R)
29+
}
30+
31+
func maxSubArray(_ nums: [Int]) -> Int {
32+
// var maxNums = nums
33+
// let numsLen = nums.count
34+
//
35+
// var maxValue: Int = maxNums[0]
36+
//
37+
// for i in 1 ..< numsLen {
38+
// maxNums[i] = max(nums[i], (maxNums[i - 1] + nums[i]))
39+
// if maxNums[i] > maxValue {
40+
// maxValue = maxNums[i]
41+
// }
42+
// }
43+
//
44+
// return maxValue
45+
return maxSubArrayDC(nums, x: 0, y: nums.count)
46+
}
47+
}
48+
49+
// print(Solution().maxSubArray([-2,1,-3,4,-1,2,1,-5,4]))

swift/54.swift

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
// 54. Spiral Matrix
3+
4+
class Solution {
5+
func spiralOrder(_ matrix: [[Int]]) -> [Int] {
6+
let m = matrix.count
7+
if m == 0 {
8+
return []
9+
}
10+
let n = matrix[0].count
11+
if n == 0 {
12+
return []
13+
}
14+
var ret: [Int] = Array.init(repeating: 0, count: m * n)
15+
var flag: [Bool] = Array.init(repeating: false, count: m * n)
16+
var count = 0
17+
var x = 0
18+
var y = 0
19+
ret[count] = matrix[x][y]
20+
flag[0] = true
21+
count += 1
22+
23+
while count < m * n {
24+
while count < m * n && y + 1 < n && !flag[x * n + y + 1] {
25+
ret[count] = matrix[x][y + 1]
26+
flag[x * n + y + 1] = true
27+
y += 1
28+
count += 1
29+
}
30+
31+
while count < m * n && x + 1 < m && !flag[(x + 1) * n + y] {
32+
ret[count] = matrix[x + 1][y]
33+
flag[(x + 1) * n + y] = true
34+
x += 1
35+
count += 1
36+
}
37+
38+
while count < m * n && y - 1 >= 0 && !flag[x * n + y - 1] {
39+
ret[count] = matrix[x][y - 1]
40+
flag[x * n + y - 1] = true
41+
y -= 1
42+
count += 1
43+
}
44+
45+
while count < m * n && x - 1 >= 0 && !flag[(x - 1) * n + y] {
46+
ret[count] = matrix[x - 1][y]
47+
flag[(x - 1) * n + y] = true
48+
x -= 1
49+
count += 1
50+
}
51+
}
52+
53+
return ret
54+
}
55+
}
56+
57+
// let matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
58+
// let matrix = [[3], [2]]
59+
// let ret = Solution().spiralOrder(matrix)
60+
//
61+
// print(ret)

0 commit comments

Comments
 (0)