示例#1
0
  public static int minimumTotal(ArrayList<ArrayList<Integer>> triangle) {
    if (triangle == null) return 0;

    int[] sum = new int[triangle.size() + 1];

    for (int i = triangle.size() - 1; i >= 0; i--) {
      for (int j = 0; j < triangle.get(i).size(); j++) {
        List currentList = triangle.get(i);
        sum[j] = (Integer) (currentList.get(j)) + Math.min(sum[j], sum[j + 1]);
      }

      for (int x = 0; x < sum.length; x++) System.out.print(sum[x] + " ");
      System.out.println();
    }

    return sum[0];
  }
示例#2
0
  private static List<List<Integer>> threeSum(int[] num) {
    List resultList = new ArrayList<ArrayList>();
    Arrays.sort(num);
    int length = num.length;
    for (int i = 0; i < length - 2; ) {
      int j = i + 1, k = length - 1;
      while (j < k) {
        int sum = num[i] + num[j] + num[k];
        if (sum == 0) {
          List<Integer> list = new ArrayList<Integer>();
          list.add(num[i]);
          list.add(num[j]);
          list.add(num[k]);
          resultList.add(list);
          do {
            j++;
          } while (num[j] == num[j - 1]);
          do {
            k--;
          } while (num[k] == num[k + 1]);
        } else if (sum > 0) {
          do {
            k--;
          } while (num[k] == num[k + 1]);
        } else {
          do {
            j++;
          } while (num[j] == num[j - 1]);
        }
      }

      do {
        i++;
      } while (num[i] == num[i - 1]);
    }

    return resultList;
  }