package org.alohaworld.mactest;import java.util.Arrays;public class BuyBlockTest { /** * 面试题选出股票的买入和卖出时机,使盈利最大 * * @param args * 股票数据数组 * @return 买入时间点和卖出时间点,买入点时的点数,卖出点时的点数 */ public static int[] getInAndOutPoint(int[] args) { if (null == args || args.length <= 1) { return new int[] { 0, 0, 0, 0 }; } int in = 0, out = 0, next = 0; for (int i = 0; i < args.length; i++) { if (args[i] < args[next]) { next = i; } if ((args[i] - args[next]) > (args[out] - args[in])) { in = next; out = i; } else if (args[i] > args[out]) { out = i; } } return new int[] { in + 1, out + 1, args[in], args[out] }; } public static void main(String[] args) { int[] a = new int[] { 9, 6, 7, 6, 7, 8, 99, -1, 99 }; int[] result = getInAndOutPoint(a); System.out.println(Arrays.toString(result)); }}