记录一下自己笔试遇到的东西~
题目:给定一个数组arr及数组长度len,求满足0<=a<=b<len的arr[b]-arr[a]的最大值.
示例:arr[]={10,5} len=2 返回:0
public class Solution(){
public static int max_find(int[] list, int len) {
if (list == null) {
return -1;
}
int max = list[0];
int sum = max - list[0];
for (int i = 1; i < len; i++) {
if (list[i] > max) {
max = list[i];
for (int j = i - 1; j >= 0; j--) {
int cur = max - list[j];
if (cur > sum) // 判断是否是最大数对之差
{
sum = cur;
}
}
}
}
return sum;
}
public static void main(String[] args) {
int[] list = { 10,5 };
System.out.print("arr[b] - arr[a]:" + max_find(list, list.length)
+ "\n");
}
}