Java O(N) based simple solution without use of hashmaps


  • 0
    P
                                    public class Solution {
                                    public int[] twoSum(int[] nums, int target) {
                                         int[]k = new int[2];
                                      for(int i=0;i<nums.length;i++)
                                     {
    	                            for(int j=i+1;j<=nums.length-1;j++)
    	                         {
    		                   if(nums[i]+nums[j]==target)
    		                {
    			          for(int y=0;y<k.length;y++)
    			         {
    				     k[0]=i;
    				     k[1]=j;
    			          }
    		              }
    	                    }
                              }
                            return k;
                                    }
                                   }

Log in to reply
 

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