-
Notifications
You must be signed in to change notification settings - Fork 419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed of Two Sum problem #8
Comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] copyArray = Arrays.copyOf(nums, nums.length);
Arrays.sort(copyArray);
int i=0, j=copyArray.length-1;
int[] arr= new int[2];
int num1=0, num2=0;
while(i<j){
int sum= copyArray[i] + copyArray[j];
if(sum < target ) i++;
else if(sum > target) j--;
else {
num1=copyArray[i];
num2=copyArray[j];
break;
}
}
for(int k=0;k<nums.length;k++){
if(num1==nums[k] && arr[0]==0) arr[0]=k;
else if(num2==nums[k]) arr[1]=k;
}
return arr;
}
}
The text was updated successfully, but these errors were encountered: