Accepted Java O(n) Solution and defeats 99.81%


  • -2
    C
    public int[] twoSum(int[] nums, int target) {
        int[] result = new int[2];
        int[] cache = new int[99999];
        int temp = 0;
        for (int i = 0;; ++i) {
           temp = target - nums[i];
           if(cache[temp+5000] > 0){
               result[0] = cache[temp+5000];
               result[1] = i+1;
               return result;
           }else{
               cache[nums[i]+5000] = i+1;
           }
        }
    }

  • 2

    the range of nums[i] in your solution can only be -5000~94999. Although it's fast it's not the best solution because the problem doesn't talk about the data range.


Log in to reply
 

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