classSolution { publicintsumOddLengthSubarrays(int[] arr) { intsum=0; intn= arr.length; for (intstart=0; start < n; start++) { for (intlength=1; start + length <= n; length += 2) { intend= start + length - 1; for (inti= start; i <= end; i++) { sum += arr[i]; } } } return sum; } }
[sol1-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
publicclassSolution { publicintSumOddLengthSubarrays(int[] arr) { int sum = 0; int n = arr.Length; for (int start = 0; start < n; start++) { for (int length = 1; start + length <= n; length += 2) { int end = start + length - 1; for (int i = start; i <= end; i++) { sum += arr[i]; } } } return sum; } }
[sol1-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { public: intsumOddLengthSubarrays(vector<int>& arr){ int sum = 0; int n = arr.size(); for (int start = 0; start < n; start++) { for (int length = 1; start + length <= n; length += 2) { int end = start + length - 1; for (int i = start; i <= end; i++) { sum += arr[i]; } } } return sum; } };
[sol1-C]
1 2 3 4 5 6 7 8 9 10 11 12
intsumOddLengthSubarrays(int* arr, int arrSize) { int sum = 0; for (int start = 0; start < arrSize; start++) { for (int length = 1; start + length <= arrSize; length += 2) { int end = start + length - 1; for (int i = start; i <= end; i++) { sum += arr[i]; } } } return sum; }
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11
funcsumOddLengthSubarrays(arr []int) (sum int) { n := len(arr) for start := range arr { for length := 1; start+length <= n; length += 2 { for _, v := range arr[start : start+length] { sum += v } } } return sum }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13
var sumOddLengthSubarrays = function(arr) { let sum = 0; const n = arr.length; for (let start = 0; start < n; start++) { for (let length = 1; start + length <= n; length += 2) { const end = start + length - 1; for (let i = start; i <= end; i++) { sum += arr[i]; } } } return sum; };
[sol1-Python3]
1 2 3 4 5 6 7 8 9 10 11
classSolution: defsumOddLengthSubarrays(self, arr: List[int]) -> int: sum = 0 n = len(arr) for start inrange(n): length = 1 while start + length <= n: for v in arr[start:start + length]: sum += v length += 2 returnsum
复杂度分析
时间复杂度:O(n^3),其中 n 是数组 arr 的长度。长度为奇数的子数组的数量是 O(n^2),对于每个子数组需要 O(n) 的时间计算子数组的和,因此总时间复杂度是 O(n^3)。
publicclassSolution { publicintSumOddLengthSubarrays(int[] arr) { int n = arr.Length; int[] prefixSums = newint[n + 1]; for (int i = 0; i < n; i++) { prefixSums[i + 1] = prefixSums[i] + arr[i]; } int sum = 0; for (int start = 0; start < n; start++) { for (int length = 1; start + length <= n; length += 2) { int end = start + length - 1; sum += prefixSums[end + 1] - prefixSums[start]; } } return sum; } }
[sol2-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution { public: intsumOddLengthSubarrays(vector<int>& arr){ int n = arr.size(); vector<int> prefixSums(n + 1); for (int i = 0; i < n; i++) { prefixSums[i + 1] = prefixSums[i] + arr[i]; } int sum = 0; for (int start = 0; start < n; start++) { for (int length = 1; start + length <= n; length += 2) { int end = start + length - 1; sum += prefixSums[end + 1] - prefixSums[start]; } } return sum; } };
[sol2-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
intsumOddLengthSubarrays(int* arr, int arrSize) { int prefixSums[arrSize + 1]; prefixSums[0] = 0; for (int i = 0; i < arrSize; i++) { prefixSums[i + 1] = prefixSums[i] + arr[i]; } int sum = 0; for (int start = 0; start < arrSize; start++) { for (int length = 1; start + length <= arrSize; length += 2) { int end = start + length - 1; sum += prefixSums[end + 1] - prefixSums[start]; } } return sum; }
[sol2-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
funcsumOddLengthSubarrays(arr []int) (sum int) { n := len(arr) prefixSums := make([]int, n+1) for i, v := range arr { prefixSums[i+1] = prefixSums[i] + v } for start := range arr { for length := 1; start+length <= n; length += 2 { end := start + length - 1 sum += prefixSums[end+1] - prefixSums[start] } } return sum }
[sol2-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
var sumOddLengthSubarrays = function(arr) { const n = arr.length; const prefixSums = newArray(n + 1).fill(0); for (let i = 0; i < n; i++) { prefixSums[i + 1] = prefixSums[i] + arr[i]; } let sum = 0; for (let start = 0; start < n; start++) { for (let length = 1; start + length <= n; length += 2) { const end = start + length - 1; sum += prefixSums[end + 1] - prefixSums[start]; } } return sum; };
[sol2-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution: defsumOddLengthSubarrays(self, arr: List[int]) -> int: sum = 0 n = len(arr) prefixSums = [0] * (n + 1) for i, v inenumerate(arr): prefixSums[i + 1] = prefixSums[i] + v for start inrange(n): length = 1 while start + length <= n: end = start + length - 1 sum += prefixSums[end + 1] - prefixSums[start] length += 2 returnsum