算法数组内数相加等于某一个数

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

var = dict((nums[i], i) for i in range(len(nums)))
for i in range(len(nums)):
num2 = target-nums[i]
if num2 in var and not i == var[num2]:
return [i,var[num2]]
####################
class Solution:

def twoSum(self, nums, target):
    """
    :type nums: List[int]
    :type target: int
    :rtype: List[int]
    """

    recs = dict((num, i) for i, num in enumerate(nums))

    for i, num in enumerate(nums):
        j = recs.get(target - num)
        if j and not j == i:
            return [i, j]
  • 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值