题意:比如有一数组[2, 4, 3, 1, 5],求5拆分成两个数,这两个数在数组下标集合为0,2和1,3
代码
public static Map<Integer, Integer> getIndex(List<Integer> list, int a) { // 数拆分成两个数在数组中两个下标的map Map<Integer, Integer> indexMap = new HashMap<>(); if (null == list || list.size() <= 0 || a < 2) { return indexMap; } // 数组中的数与下标的map Map<Integer, Integer> numberIndexMap = new HashMap<>(); for (int index = 0, size = list.size(); index < size; index++) { numberIndexMap.put(list.get(index), index); } Integer integerX; Integer integerY; for (int i = 1, x = 1, y = a - 1; x < y; i++, x = i, y = a - i) { integerX = numberIndexMap.get(x); integerY = numberIndexMap.get(y); if (null != integerX && null != integerY) { indexMap.put(integerX, integerY); } } return indexMap; }