public int[] twoSum(int[] nums, int target) { // 数据转为list List<Integer> resultList = new ArrayList<>(nums.length); for (int num : nums) { resultList.add(num); } List<Integer> indexList = new ArrayList<>(); this.commonIndex(target, resultList, indexList); Integer[] array2 = indexList.toArray(new Integer[indexList.size()]); int[] resultNum = new int[array2.length]; for (int j = 0; j < array2.length; j++) { resultNum[j] = array2[j].intValue(); } return resultNum; } private void commonIndex(int target, List<Integer> resultList, List<Integer> indexList) { for (int i = 0; i < resultList.size(); i++) { int differ = target - resultList.get(i); if (resultList.indexOf(differ) != -1 && resultList.indexOf(differ) != i) { indexList.add(i); indexList.add(resultList.indexOf(differ)); resultList.remove(i); resultList.remove(resultList.indexOf(differ)); break; } } }
leetcode 两数之和 完美解答 拿去不谢
最新推荐文章于 2023-12-11 13:40:59 发布