classSolution { public: intminSubArrayLen(int s, vector<int>& nums){ int n = nums.size(); if (n == 0) { return0; } int ans = INT_MAX; for (int i = 0; i < n; i++) { int sum = 0; for (int j = i; j < n; j++) { sum += nums[j]; if (sum >= s) { ans = min(ans, j - i + 1); break; } } } return ans == INT_MAX ? 0 : ans; } };
publicclassSolution { publicintMinSubArrayLen(int s, int[] nums) { int n = nums.Length; if (n == 0) { return0; }
int ans = int.MaxValue; for (int i = 0; i < n; ++i) { int sum = 0; for (int j = i; j < n; ++j) { sum += nums[j]; if (sum >= s) { ans = Math.Min(ans, j - i + 1); break; } } }
funcminSubArrayLen(s int, nums []int)int { n := len(nums) if n == 0 { return0 } ans := math.MaxInt32 for i := 0; i < n; i++ { sum := 0 for j := i; j < n; j++ { sum += nums[j] if sum >= s { ans = min(ans, j - i + 1) break } } } if ans == math.MaxInt32 { return0 } return ans }
funcmin(x, y int)int { if x < y { return x } return y }
[sol1-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution: defminSubArrayLen(self, s: int, nums: List[int]) -> int: ifnot nums: return0 n = len(nums) ans = n + 1 for i inrange(n): total = 0 for j inrange(i, n): total += nums[j] if total >= s: ans = min(ans, j - i + 1) break return0if ans == n + 1else ans
[sol1-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
intminSubArrayLen(int s, int* nums, int numsSize) { if (numsSize == 0) { return0; } int ans = INT_MAX; for (int i = 0; i < numsSize; i++) { int sum = 0; for (int j = i; j < numsSize; j++) { sum += nums[j]; if (sum >= s) { ans = fmin(ans, j - i + 1); break; } } } return ans == INT_MAX ? 0 : ans; }
privateintLowerBound(int[] a, int l, int r, int target) { int mid = -1, originL = l, originR = r; while (l < r) { mid = (l + r) >> 1; if (a[mid] < target) l = mid + 1; else r = mid; }
publicclassSolution { privateintLowerBound(int[] a, int l, int r, int target) { int mid = -1, originL = l, originR = r; while (l < r) { mid = (l + r) >> 1; if (a[mid] < target) l = mid + 1; else r = mid; }
return (a[l] >= target) ? l : -1; }
publicintMinSubArrayLen(int s, int[] nums) { int n = nums.Length; if (n == 0) { return0; }
int ans = int.MaxValue; int[] sums = newint[n + 1]; // 为了方便计算,令 size = n + 1 // sums[0] = 0 意味着前 0 个元素的前缀和为 0 // sums[1] = A[0] 前 1 个元素的前缀和为 A[0] // 以此类推 for (int i = 1; i <= n; ++i) { sums[i] = sums[i - 1] + nums[i - 1]; }
for (int i = 1; i <= n; ++i) { int target = s + sums[i - 1]; int bound = LowerBound(sums, i, n, target); if (bound != -1) { ans = Math.Min(ans, bound - i + 1); } }
funcminSubArrayLen(s int, nums []int)int { n := len(nums) if n == 0 { return0 } ans := math.MaxInt32 sums := make([]int, n + 1) // 为了方便计算,令 size = n + 1 // sums[0] = 0 意味着前 0 个元素的前缀和为 0 // sums[1] = A[0] 前 1 个元素的前缀和为 A[0] // 以此类推 for i := 1; i <= n; i++ { sums[i] = sums[i - 1] + nums[i - 1] } for i := 1; i <= n; i++ { target := s + sums[i-1] bound := sort.SearchInts(sums, target) if bound < 0 { bound = -bound - 1 } if bound <= n { ans = min(ans, bound - (i - 1)) } } if ans == math.MaxInt32 { return0 } return ans }
funcmin(x, y int)int { if x < y { return x } return y }
[sol2-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution: defminSubArrayLen(self, s: int, nums: List[int]) -> int: ifnot nums: return0 n = len(nums) ans = n + 1 sums = [0] for i inrange(n): sums.append(sums[-1] + nums[i]) for i inrange(1, n + 1): target = s + sums[i - 1] bound = bisect.bisect_left(sums, target) if bound != len(sums): ans = min(ans, bound - (i - 1)) return0if ans == n + 1else ans
classSolution { public: intminSubArrayLen(int s, vector<int>& nums){ int n = nums.size(); if (n == 0) { return0; } int ans = INT_MAX; int start = 0, end = 0; int sum = 0; while (end < n) { sum += nums[end]; while (sum >= s) { ans = min(ans, end - start + 1); sum -= nums[start]; start++; } end++; } return ans == INT_MAX ? 0 : ans; } };
publicclassSolution { publicintMinSubArrayLen(int s, int[] nums) { int n = nums.Length; if (n == 0) { return0; }
int ans = int.MaxValue; int start = 0, end = 0; int sum = 0; while (end < n) { sum += nums[end]; while (sum >= s) { ans = Math.Min(ans, end - start + 1); sum -= nums[start]; ++start; } ++end; }
funcminSubArrayLen(s int, nums []int)int { n := len(nums) if n == 0 { return0 } ans := math.MaxInt32 start, end := 0, 0 sum := 0 for end < n { sum += nums[end] for sum >= s { ans = min(ans, end - start + 1) sum -= nums[start] start++ } end++ } if ans == math.MaxInt32 { return0 } return ans }
funcmin(x, y int)int { if x < y { return x } return y }
[sol3-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution: defminSubArrayLen(self, s: int, nums: List[int]) -> int: ifnot nums: return0 n = len(nums) ans = n + 1 start, end = 0, 0 total = 0 while end < n: total += nums[end] while total >= s: ans = min(ans, end - start + 1) total -= nums[start] start += 1 end += 1 return0if ans == n + 1else ans
[sol3-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
intminSubArrayLen(int s, int *nums, int numsSize) { if (numsSize == 0) { return0; } int ans = INT_MAX; int start = 0, end = 0; int sum = 0; while (end < numsSize) { sum += nums[end]; while (sum >= s) { ans = fmin(ans, end - start + 1); sum -= nums[start]; start++; } end++; } return ans == INT_MAX ? 0 : ans; }