Java数组中找出两个相加等于某个值的数据下标

public class Solution2 {

    public static void main(String[] args) {
        int[] nums = {7,1,3,2,11,34,6,9,32,8};
        int target = 13;
        // 结果数组
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i], i);
        }
        for (int i = 0; i < nums.length; i++) {
            int two = target - nums[i];
            // 如果存在第二个数的数组下标&&结果的两个数不是同一个数的值
            if (map.containsKey(two) && target != 2 * two) {
                // 返回找到的两个数的数组下标
                System.out.println(i+" & "+ map.get(two));
            }
        }
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
这个问题可以使用回溯法(Backtracking)来解决。回溯法是一种暴力搜索的算法,它通过不断尝试所有可能的情况来寻找问题的解。 在这个问题中,我们可以使用一个递归函数来进行回溯搜索。函数需要接收以下参数: - 数组arr:要搜索的数组 - int target:目标和 - int start:当前搜索的起始下标 - List<Integer> path:保存当前搜索路径的列表 - List<List<Integer>> result:保存所有搜索结果的列表 具体实现如下: ```java public static void findSum(int[] arr, int target, int start, List<Integer> path, List<List<Integer>> result) { if (target == 0) { // 如果目标和为0,说明找到了一组解 result.add(new ArrayList<>(path)); return; } if (target < 0 || start == arr.length) { // 如果目标和小于0或者已经搜索到数组末尾,直接返回 return; } for (int i = start; i < arr.length; i++) { path.add(arr[i]); // 将当前数加入路径 findSum(arr, target - arr[i], i + 1, path, result); // 递归搜索 path.remove(path.size() - 1); // 回溯,将当前数从路径中移除 } } ``` 在上面的函数中,当目标和为0时,说明找到了一组解,将当前路径保存到结果列表中。如果目标和小于0或者已经搜索到数组末尾,直接返回。 在每次搜索时,我们从当前位置开始,依次将数组中的数加入路径中。然后递归搜索下一层,将目标和减去当前数。当搜索返回时,我们需要回溯,将当前数从路径中移除,以便尝试其他可能的组合。 下面是完整的代码: ```java import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Main { public static void main(String[] args) { int[] arr = {2, 3, 6, 7}; int target = 7; List<List<Integer>> result = combinationSum(arr, target); System.out.println(result); } public static List<List<Integer>> combinationSum(int[] arr, int target) { Arrays.sort(arr); // 先对数组进行排序,以便剪枝 List<List<Integer>> result = new ArrayList<>(); findSum(arr, target, 0, new ArrayList<>(), result); return result; } public static void findSum(int[] arr, int target, int start, List<Integer> path, List<List<Integer>> result) { if (target == 0) { // 如果目标和为0,说明找到了一组解 result.add(new ArrayList<>(path)); return; } if (target < 0 || start == arr.length) { // 如果目标和小于0或者已经搜索到数组末尾,直接返回 return; } for (int i = start; i < arr.length; i++) { if (i > start && arr[i] == arr[i-1]) continue; // 剪枝,去掉重复的数 path.add(arr[i]); // 将当前数加入路径 findSum(arr, target - arr[i], i + 1, path, result); // 递归搜索 path.remove(path.size() - 1); // 回溯,将当前数从路径中移除 } } } ``` 在上面的代码中,我们先对数组进行排序,以便剪枝,去掉重复的数。在搜索过程中,如果发现当前数和前一个数相同,直接跳过,避免重复计算。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fate急速出击

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值