O(n) Simple C# solution


  • 0
    W

    Simple and readable solution

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

Log in to reply
 

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