Clean C# Two Index solution


  • 0
    C

    public class Solution {
    public int[] TwoSum(int[] numbers, int target) {

        int first = 0, last = numbers.Length - 1;
        
        while (first < last)
        {
            if (numbers[last] + numbers[first] == target)
            {
                return new int[] { first + 1, last + 1 };
            }
            
            if ((numbers[last] + numbers[first]) > target)
            {
                last--;
            } 
            else
            {
                first++;
            }
        }
        
        return new int[] { -1, -1 };
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.