★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-pypinhfm-md.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead.git
Example: github
Input: Output: 2 Explanation: the subarray has the minimal length under the problem constraint.s = 7, nums = [2,3,1,2,4,3][4,3]
給定一個含有 n 個正整數的數組和一個正整數 s ,找出該數組中知足其和 ≥ s 的長度最小的連續子數組。若是不存在符合條件的連續子數組,返回 0。數組
示例: 微信
輸入: 輸出: 2 解釋: 子數組 是該條件下的長度最小的連續子數組。 s = 7, nums = [2,3,1,2,4,3][4,3]
進階:app
若是你已經完成了O(n) 時間複雜度的解法, 請嘗試 O(n log n) 時間複雜度的解法。spa
20mscode
1 class Solution { 2 func minSubArrayLen(_ s: Int, _ nums: [Int]) -> Int { 3 guard nums.count > 0 else { 4 return 0 5 } 6 var i = 0, j = 0,minX = nums.count, sum = nums[0], match = false 7 while j < nums.count { 8 if i == j { 9 if sum < s { 10 j = j + 1 11 if j == nums.count { 12 break; 13 } 14 sum = nums[j] + sum 15 } else { 16 return 1 17 } 18 } else { 19 if sum < s { 20 j = j + 1 21 if j == nums.count { 22 break 23 } 24 sum = nums[j] + sum 25 } else { 26 match = true 27 minX = min(j - i + 1, minX) 28 sum = sum - nums[i] 29 i = i + 1 30 } 31 } 32 } 33 return match ? minX : 0 34 } 35 }
24mshtm
1 class Solution { 2 func minSubArrayLen(_ s: Int, _ nums: [Int]) -> Int { 3 4 var sum = 0, left = 0, res = Int.max 5 for i in nums.indices { 6 sum += nums[i] 7 while sum >= s { 8 res = min(res, i-left+1) 9 sum -= nums[left] 10 left += 1 11 } 12 } 13 return (res == Int.max) ? 0 : res 14 } 15 }
24msblog
1 class Solution { 2 func minSubArrayLen(_ s: Int, _ nums: [Int]) -> Int { 3 var miniSize = Int.max ,start = 0, currentSum = 0 4 5 for (i, num) in nums.enumerated() { 6 currentSum += num 7 8 while currentSum >= s && start <= i { 9 miniSize = min(miniSize, i - start + 1) 10 11 currentSum -= nums[start] 12 start += 1 13 } 14 } 15 16 return miniSize == Int.max ? 0 : miniSize 17 } 18 }
32ms
1 class Solution { 2 func minSubArrayLen(_ s: Int, _ nums: [Int]) -> Int { 3 let n = nums.count 4 if (n < 1 || nums.reduce(0, +) < s) { return 0 } 5 6 // 維護一個滑動窗口 nums[i,j], nums[i...j] < s 7 var i = 0 8 var j = -1 9 var total = 0 10 var res = n + 1 11 while i <= n-1 { 12 if (j + 1 < n) && total < s { // 小於目標值 13 j += 1 14 total += nums[j] 15 } else { // 大於目標值 16 total -= nums[i] 17 i += 1 18 } 19 if total >= s { 20 res = min(res, j-i+1) // 求得當前最小長度 21 } 22 } 23 if res == n+1 { // 沒有改變過 24 return 0 25 } 26 return res 27 } 28 }
84ms
1 class Solution { 2 func minSubArrayLen(_ s: Int, _ nums: [Int]) -> Int { 3 guard nums.count > 0 else { 4 return 0 5 } 6 7 var sums : [Int] = [0] 8 for num in nums{ 9 sums.append(sums.last! + num) 10 } 11 12 var minCount : Int = Int.max 13 14 for (index, sum) in sums.enumerated(){ 15 if sum >= s{ 16 let key = sum - s 17 var foundIndex = binarySearch(low: 0, high: sums.count - 1, key: key, sums: sums) 18 if sums[foundIndex] > key{ 19 foundIndex -= 1 20 } 21 minCount = min(minCount, index - foundIndex) 22 } 23 } 24 25 if sums.last! >= s{ 26 return min(nums.count, minCount) 27 }else{ 28 return 0 29 } 30 31 } 32 33 func binarySearch(low : Int, high : Int, key : Int, sums : [Int])->Int{ 34 guard low < high else{ 35 return low 36 } 37 38 let mid = (low + high) / 2 39 let value = sums[mid] 40 if value == key{ 41 return mid 42 }else if value > key{ 43 return binarySearch(low : low, high : mid-1, key : key, sums : sums) 44 }else{ 45 return binarySearch(low : mid + 1, high : high, key : key, sums : sums) 46 } 47 } 48 }