Does anybody know the fastest java solution? 1ms


  • 0
    L

    Any genius?

    Unlike C or C++, Java can't allocate the Map like int[] during compile time.

    int* twoSum(int* nums, int numsSize, int target) {
        static int res[2] = {0};
        int hash[59999] = {0};
    
        for (int i = 0; i < numsSize; i++) {
            if (hash[nums[i]+29999]) {
                res[0] = hash[nums[i]+29999]-1;
                res[1] = i;
                break;
            }
            hash[target-nums[i]+29999] = i+1;
        }
        return res;
    }
    

Log in to reply
 

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