当前位置:网站首页>[LeetCode]1. Sum of two numbers thirty-four

[LeetCode]1. Sum of two numbers thirty-four

2022-06-13 00:12:00 PowerDon

Given an array of integers nums  And a target value target, Please find and as the target value in the array   Two   Integers , And return their array subscripts .

You can assume that each input corresponds to only one answer . however , The same element in an array cannot be used twice .

Example :

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

because nums[0] + nums[1] = 2 + 7 = 9
So back [0, 1]

source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/two-sum
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .

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

                int temp = target - nums[i];
                if (nums[j] == temp)
                {
    
                    return new int[2] {
     i, j };
                }
            }
        }

        return null;
    }
}
原网站

版权声明
本文为[PowerDon]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280602300686.html