# 6ms use map O(n) and 5ms by Arrays.sort() O(n*logn)

• ``````public int[] twoSum(int[] nums, int target) {
Map<Integer,Integer> m=new HashMap<Integer,Integer>();
int[] ans=new int[2];
for(int i=0;i<nums.length;i++){
if(m.containsKey(target-nums[i])){
ans[0]=i+1;
ans[1]=m.get(target-nums[i])+1;
break;
}
m.put(nums[i], i);

}
if(ans[0]>ans[1]){
ans[0]=ans[0]^ans[1];
ans[1]=ans[0]^ans[1];
ans[0]=ans[0]^ans[1];
}
return ans;
}

public int[] twoSum(int[] nums, int target) {
int[] a=nums.clone();
Arrays.sort(nums);
int a1=0,a2=0;
out:	for(int i=0;i<nums.length;i++){
int r=target-nums[i];
a1=i;
int start=i,end=nums.length-1;
while(start<=end){
int m=(start+end)/2;
if(nums[m]==r){
a2=m;
break out;
}
else if(nums[m]>r)
end=m-1;
else if(nums[m]<r)
start=m+1;

}
}
//System.out.println(nums[a1]+nums[a2]);

for(int i=0;i<a.length;i++){
if(a[i]==nums[a1]){a1=i;break;}

}
for(int i=0;i<a.length;i++){
if(a[i]==nums[a2]&&i!=a1){a2=i;break;}
}
if(a1>a2){
a1=a1^a2;
a2=a1^a2;
a1=a1^a2;
}
return new int[]{a1+1,a2+1};
}``````

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