[Swift]LeetCode523. 连续的子数组和 | Continuous Subarray Sum
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10397532.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n is also an integer.
Example 1:
Input: [23, 2, 4, 6, 7], k=6 Output: True Explanation: Because [2, 4] is a continuous subarray of size 2 and sums up to 6.
Example 2:
Input: [23, 2, 6, 4, 7], k=6 Output: True Explanation: Because [23, 2, 6, 4, 7] is an continuous subarray of size 5 and sums up to 42.
Note:
- The length of the array won't exceed 10,000.
- You may assume the sum of all the numbers is in the range of a signed 32-bit integer.
给定一个包含非负数的数组和一个目标整数 k,编写一个函数来判断该数组是否含有连续的子数组,其大小至少为 2,总和为 k 的倍数,即总和为 n*k,其中 n 也是一个整数。
示例 1:
输入: [23,2,4,6,7], k = 6 输出: True 解释: [2,4] 是一个大小为 2 的子数组,并且和为 6。
示例 2:
输入: [23,2,6,4,7], k = 6 输出: True 解释: [23,2,6,4,7]是大小为 5 的子数组,并且和为 42。
说明:
- 数组的长度不会超过10,000。
- 你可以认为所有数字总和在 32 位有符号整数范围内。
192ms
1 class Solution { 2 func checkSubarraySum(_ nums: [Int], _ k: Int) -> Bool { 3 var map = [Int: Int]() 4 map[0] = -1 5 var runningSum = 0 6 for i in 0..<nums.count { 7 runningSum += nums[i] 8 if k != 0 { 9 runningSum %= k 10 } 11 let pre = map[runningSum] 12 if pre != nil { 13 if (i - pre!) > 1 { return true} 14 } else { 15 map[runningSum] = i 16 } 17 } 18 return false 19 } 20 }
232ms
1 class Solution { 2 func checkSubarraySum(_ nums: [Int], _ k: Int) -> Bool { 3 if nums.count < 2 { return false } 4 if (k == 0) { return nums[0] == 0 && nums[1] == 0 } 5 var dict = [Int:Int]() 6 7 var sum = 0 8 for i in 0...nums.count-1 { 9 sum += nums[i] 10 11 let r = sum % k 12 if r == 0 { 13 14 if(i>=1) { 15 16 return true 17 } 18 } 19 if let value = dict[r] { 20 if (i - value >= 2) { return true } 21 } 22 else { 23 dict[r] = i 24 } 25 26 27 } 28 return false 29 } 30 }
316ms
1 class Solution { 2 func isMultiple(_ num: Int, _ k: Int) -> Bool { 3 if k == 0 { 4 return num == 0 5 } else { 6 return num % k == 0 7 } 8 } 9 func checkSubarraySum(_ nums: [Int], _ k: Int) -> Bool { 10 var preSum: [Int] = [] 11 for (i, num) in nums.enumerated() { 12 let sum = (preSum.last ?? 0) + num 13 preSum.append(sum) 14 if i >= 1 && isMultiple(sum, k) { 15 return true 16 } 17 } 18 if 0 > nums.count - 2 { 19 return false 20 } 21 for i in 0..<(nums.count - 2) { 22 for j in (i + 2)..<nums.count { 23 let sum = preSum[j] - preSum[i] 24 if isMultiple(sum, k) { 25 return true 26 } 27 } 28 } 29 return false 30 } 31 }
324ms
1 class Solution { 2 func checkSubarraySum(_ nums: [Int], _ k: Int) -> Bool { 3 var nums = nums 4 let count = nums.count 5 guard count > 1 else { 6 return false 7 } 8 for i in 1..<count { 9 nums[i] += nums[i - 1] 10 } 11 12 for i in 1..<count { 13 if (k != 0 && nums[i] % k == 0) || (k == 0 && nums[i] == 0) { 14 return true 15 } 16 for j in i + 1..<count { 17 if (k != 0 && (nums[j] - nums[j - i - 1]) % k == 0) 18 || (k == 0 && nums[j] - nums[j - i - 1] == 0) { 19 return true 20 } 21 } 22 } 23 return false 24 } 25 }
340ms
1 class Solution { 2 func checkSubarraySum(_ nums: [Int], _ k: Int) -> Bool { 3 var sum: [Int] = [] 4 var total = 0 5 for i in 0..<nums.count { 6 total += nums[i] 7 sum.append(total) 8 } 9 10 for start in 0..<nums.count-1 { 11 for end in start+1..<nums.count { 12 let summ = sum[end] - sum[start] + nums[start] 13 if summ == k || (k != 0 && summ%k == 0) { 14 return true 15 } 16 } 17 } 18 19 return false 20 } 21 }
380ms
1 class Solution { 2 func checkSubarraySum(_ nums: [Int], _ k: Int) -> Bool { 3 return checkSubarraySumRecursion(nums, k) 4 } 5 6 func checkSubarraySumRecursion(_ nums : [Int], _ k : Int)->Bool{ 7 guard nums.count > 1 else{ 8 return false 9 } 10 11 var sum : Int = nums[0] 12 for index in 1..<nums.count{ 13 sum += nums[index] 14 if k == 0 && sum == 0{ 15 return true 16 } 17 18 if k != 0 && sum % k == 0{ 19 return true 20 } 21 } 22 23 return checkSubarraySumRecursion(Array(nums[1...]), k) 24 } 25 }