- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map.Entry;
- public class Find {
- public static HashMap<String, Integer> cache = new HashMap<String, Integer>();
- public static void main(String args[]) {
- int[] array = new int[100];
- for (int i = 0; i < array.length; i++) {
- array[i] = ((Math.random() > 0.5 ? -1 : 1) * (int) (Math.random() * 100));
- if (i % 10 == 0) {
- System.out.println();
- }
- System.out.print(array[i] + " ");
- }
- for (int i = 0; i < array.length; i++)
- for (int j = i; j < array.length; j++)
- sum(array, i, j);
- Iterator<Entry<String, Integer>> it = cache.entrySet().iterator();
- int max = Integer.MIN_VALUE;
- String finalResult = null;
- while (it.hasNext()) {
- Entry<String,Integer> entry = it.next();
- int temp = entry.getValue();
- if (temp > max) {
- max=temp;
- finalResult = entry.getKey();
- }
- }
- System.out.println();
- System.out.println("##############");
- System.out.println(finalResult);
- }
- public static int sum(int[] array, int i, int j) {
- Integer result;
- if ((result = cache.get(i + "," + j)) != null) {
- return result;
- }
- if (j - i > 0) {
- result = array[i] + sum(array, i + 1, j);
- } else {
- result = array[i];
- }
- cache.put(i + "," + j, result);
- return result;
- }
- }
复制代码
百度了一个,运行结果如下
[img=664,442][/img] |