This says time exceed, but I assume this should work?


  • 0
    T

    can someone please give a efficient solution and explain please, I am little rusty with hash tables

    public class Solution {
            public int[] twoSum(int[] numbers, int target) {
             int len = numbers.length;
             int x = 0;
             int y = 0;
             while(x<len){
                 y = x+1;
             
             while(y<len){
                 if((numbers[x] + numbers[y])== target){
                     int [] val = {x+1,y+1};
                     return val;
                 }
                 y++;
               
             }
             x++;
             }
             return null;
           
        }
        }

  • 1
    L

    Your answer is time complexity O(n^2),is TLE although problem is not involved with this. You should use hash so that time complexity will be O(n).


Log in to reply
 

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