Written by a Java beginner


  • 0
    K

    public class Solution {
    public static int[] twoSum(int[] nums, int target) {
    int[] result = new int[2];
    //int head = 0;
    int next=1;
    for (int head=0;head < nums.length - 1;head ++){
    while(nums[head] + nums[next] != target && next < nums.length -1){
    next ++;
    }
    if(nums[head] + nums[next] == target){
    result[0]=head;
    result[1]=next;
    }
    next = head + 2;
    }
    return result;
    }
    }


Log in to reply
 

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