A recursive algorithm to find two integers in an array that sums to a given integer

后端 未结 8 1188
Happy的楠姐
Happy的楠姐 2021-01-16 19:36

I need an algorithm to determine if an array contains two elements that sum to a given integer.

The array is sorted.

The algorithm should be recursiv

8条回答
  •  没有蜡笔的小新
    2021-01-16 19:51

    Here is the recursion method to perform the groupSum

    public boolean groupSum(int start, int[] nums, int target) 
    {
        if (start >= nums.length) {
        return (target == 0);
    }
    return groupSum(start + 1, nums, target - nums[start]) || groupSum(start + 
    1,nums,target) 
    }
    

提交回复
热议问题