Last active
August 29, 2015 14:21
-
-
Save nuboat/8fe9099829686d841e87 to your computer and use it in GitHub Desktop.
ปัญหา 2-SUM
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
true | |
Execution n = 1007377 in 1521 ms | |
true | |
Tuning Execution n = 1007377 in 1394 ms |
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
object TwoSum { | |
def sum(n: Int, sorted: ArrayBuffer[Int]): Boolean = sorted.par.find(x => isMatch(x, n, sorted)).isDefined | |
def isMatch(x: Int, n: Int, sorted: ArrayBuffer[Int]): Boolean = sorted.find( y => (x + y) == n).isDefined | |
} |
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
object TwoSumTuning { | |
def sum(n: Int, sorted: ArrayBuffer[Int]): Boolean = sorted.par.find(x => isMatch(x, n, sorted)).isDefined | |
def isMatch(x: Int, n: Int, sorted: ArrayBuffer[Int]): Boolean = { | |
var result = false | |
breakable { | |
var a = 0 | |
for (a <- x until sorted.size) { | |
if ((x + sorted(a)) == n) { | |
result = true | |
break | |
} | |
} | |
} | |
return result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment