Likou Brush Hundred Days Plan Day7 Sum of Two Numbers II - Input Ordered Array C#

learning target:

I will continue to update my unique algorithm ideas, hoping to bring you different thinking expansion!
If you find it helpful, please like, follow and support!
Your encouragement is what keeps me going!
! ! !

Likou Question Bank Question 167 Official Link

Learning Content:

Sum of Two Numbers II - Input Sorted Array

给定一个已按照 非递减顺序排列  的整数数组 numbers ,请你从数组中找出两个数满足相加之和等于目标数 target 。

函数应该以长度为 2 的整数数组的形式返回这两个数的下标值。numbers 的下标 从 1 开始计数 ,所以答案数组应当满足 1 <= answer[0] < answer[1] <= numbers.length 。

你可以假设每个输入 只对应唯一的答案 ,而且你 不可以 重复使用相同的元素。

Example 1:

Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is equal to the target number 9. So index1 = 1, index2 = 2 .
Example 2:

Input: numbers = [2,3,4], target = 6
Output: [1,3]
Example 3:

Input: numbers = [-1,0], target = -1
Output: [1,2]

提示:

2 <= numbers.length <= 3 * 104
-1000 <= numbers[i] <= 1000
numbers 按 非递减顺序 排列
-1000 <= target <= 1000
仅存在一个有效答案

Source: LeetCode
Link: https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted

study-time:

2022.1.16


Learning output:

idea one
insert image description here

Problem- solving ideas
Base address round robin

1. First determine a base address, then traverse all the indexes behind the base address, add the two values ​​each time to judge, if found, return

public class Solution {
    
    
    public int[] TwoSum(int[] numbers, int target) {
    
    
        for(int i=0;i<numbers.Length;i++){
    
    
            for(int j=i+1;j<numbers.Length;j++){
    
    
                if(numbers[i]+numbers[j]==target){
    
    
                    return new int[]{
    
    i + 1, j + 1};
                }
            }
        }
        return new int[]{
    
    0, 0};
    }
}

idea two
insert image description here

Problem- solving ideas
double pointer method

1. First determine the left pointer and the right pointer, and then determine whether the value of the index pointed to by the two pointers is the target value. If it is greater than the target value, move the right pointer to the left, and if it is less than the target value, move the left pointer to the right

public class Solution {
    
    
    public int[] TwoSum(int[] numbers, int target) {
    
    
        if (numbers == null) return null;
        int i = 0, j = numbers.Length - 1;
        while (i < j) {
    
    
            int sum = numbers[i] + numbers[j];
            if (sum == target) {
    
    
                 return new int[]{
    
    i + 1, j + 1};
            } else if (sum < target) {
    
    
                i++;
            } else {
    
    
                j--;
        }
    }
        return null;
    }
}

Author: guinea pig Xiaohuihui
The copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

Guess you like

Origin blog.csdn.net/m0_48781656/article/details/122530262