这部分是main函数里面的内容,静态方法调用非静态的记得实例化
public class main {
public static void main(String[] args){
int[] arr = new int[] {
1,2,3,4,5};
test1 test1 = new test1();
int result = test1.sumOddLengthSubarrays(arr);
System.out.println(result);
}
};
这部分是test1里面的内容
class test1 {
public int sumOddLengthSubarrays(int[] arr) {
int sum = 0;
int n = arr.length;
for(int length = 1; length <= n; length += 2){
for(int start = 0; start + length <= n; start += length){
for(int i = 0; i < length; i++){
sum += arr[start];
}
}
}
return sum;
}
}
第一次写csdn,以后会慢慢坚持的,如果有错误请指正。