From 7a18d29e4b8d2770a2d3a62641910e7761f386b9 Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Mon, 27 May 2024 09:07:04 +0100 Subject: [PATCH 1/2] Swift implementation for LCOF 42 --- .../README.md" | 18 ++++++++++++++++++ .../Solution.swift" | 13 +++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 "lcof/\351\235\242\350\257\225\351\242\23042. \350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\222\214/Solution.swift" diff --git "a/lcof/\351\235\242\350\257\225\351\242\23042. \350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\222\214/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23042. \350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\222\214/README.md" index 0e22e5f0bb5c2..629f2c3be84b5 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23042. \350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\222\214/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23042. \350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\222\214/README.md" @@ -189,6 +189,24 @@ public class Solution { } ``` +#### Swift + +```swift +class Solution { + func maxSubArray(_ nums: [Int]) -> Int { + var ans = Int.min + var currentSum = 0 + + for x in nums { + currentSum = max(currentSum, 0) + x + ans = max(ans, currentSum) + } + + return ans + } +} +``` + diff --git "a/lcof/\351\235\242\350\257\225\351\242\23042. \350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\222\214/Solution.swift" "b/lcof/\351\235\242\350\257\225\351\242\23042. \350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\222\214/Solution.swift" new file mode 100644 index 0000000000000..4b584df17b7e0 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23042. \350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\222\214/Solution.swift" @@ -0,0 +1,13 @@ +class Solution { + func maxSubArray(_ nums: [Int]) -> Int { + var ans = Int.min + var currentSum = 0 + + for x in nums { + currentSum = max(currentSum, 0) + x + ans = max(ans, currentSum) + } + + return ans + } +} \ No newline at end of file From c58fb3409a8863e0b6b1dfb9db17e495f0f4e998 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Mon, 27 May 2024 08:40:34 +0000 Subject: [PATCH 2/2] style: format code and docs with prettier --- .../README.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/lcof/\351\235\242\350\257\225\351\242\23042. \350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\222\214/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23042. \350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\222\214/README.md" index 629f2c3be84b5..002c2cc42d3a5 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23042. \350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\222\214/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23042. \350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\222\214/README.md" @@ -196,12 +196,12 @@ class Solution { func maxSubArray(_ nums: [Int]) -> Int { var ans = Int.min var currentSum = 0 - + for x in nums { currentSum = max(currentSum, 0) + x ans = max(ans, currentSum) } - + return ans } }