一个排好序的数组,找出两数之和为m的所有组合

 public static void main(String[] args) {
  int[] ints = { 1, 3, 5, 11, 13, 15, 17, 55 };
  int m = 16;
  int key = 0;
  Map map = new HashMap();
  for (int i = 0; i < ints.length; i++) {
   key = m - ints[i];
   map.put(ints[i], ints[i]);
   if (map.containsKey(key)) {
    System.out.println(key + "+" + ints[i]);
   }
  }
 }

猜你喜欢

转载自ligure.iteye.com/blog/1886315