Created
October 26, 2010 03:37
-
-
Save anonymous/646264 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public boolean hassum(int target) { | |
if (array.length == 0) | |
{ | |
return false; | |
} | |
int hi = 0; | |
int lo = 0; | |
int sum = array[0]; | |
while ((hi < array.length) && (lo < array.length)) | |
{ | |
if (sum == target) | |
{ | |
return true; | |
} | |
if (sum > target) | |
{ | |
sum -= array[lo]; | |
lo++; | |
} | |
if (sum < target) | |
{ | |
hi++; | |
sum += array[hi]; | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment