Skip to content

Commit 2c3676d

Browse files
author
tumanob
committed
Add problem 69 and minor cleaning of other tasks
1 parent e56e94d commit 2c3676d

File tree

3 files changed

+69
-30
lines changed

3 files changed

+69
-30
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package Sqrt_p69
2+
3+
/**
4+
* https://leetcode.com/problems/sqrtx/
5+
* Given a non-negative integer x, compute and return the square root of x.
6+
* Since the return type is an integer, the decimal digits are truncated,
7+
* and only the integer part of the result is returned.
8+
*/
9+
object Solution {
10+
/**
11+
* I am gonna Use Newton's method to calculate it as it is fun
12+
* and used in many functional programing courses as a reference
13+
*
14+
* Some external reading about the method
15+
* https://blogs.sas.com/content/iml/2016/05/18/newtons-method-babylonian-square-root.html
16+
*/
17+
val tolerance = 0.001 // when it is good enough as well a start guess
18+
19+
def mySqrt(x: Int): Int = {
20+
def isGoodEnough(guess: Double): Boolean =
21+
Math.abs(guess * guess - x) < tolerance
22+
23+
def improve(guess: Double): Double =
24+
(guess + x / guess) * 0.5
25+
26+
def iterator(guess: Double): Double =
27+
if (isGoodEnough(guess)) guess
28+
else iterator(improve(guess))
29+
30+
iterator(tolerance).toInt
31+
}
32+
33+
def main(args: Array[String]): Unit = {
34+
println(mySqrt(3)) // 1
35+
println(mySqrt(4)) // 2
36+
println(mySqrt(8)) // 2
37+
}
38+
}

ScalaSolutions/src/main/scala/TwoSum_p1/Solution.scala

+6-8
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@ package TwoSum_p1
1111
*/
1212
object Solution {
1313

14-
def main(args: Array[String]): Unit = {
15-
println(twoSum(Array(2,7,11,15), 18).mkString("[", ",", "]")) // [1,2]
16-
println(twoSum(Array(2,7,11,15), 9).mkString("[", ",", "]")) // [0,1]
17-
println(twoSum(Array(2,7,11,15), 26).mkString("[", ",", "]")) // [2,3]
18-
}
19-
20-
//leetcode submit region begin(Prohibit modification and deletion)
2114
def twoSum(nums: Array[Int], target: Int): Array[Int] = {
2215
def twoSum(index: Int, previous: Map[Int, Int]): Array[Int] = {
2316
previous.get(target - nums(index)) match {
@@ -27,5 +20,10 @@ object Solution {
2720
}
2821
twoSum(0, Map.empty)
2922
}
30-
//leetcode submit region end(Prohibit modification and deletion)
23+
24+
def main(args: Array[String]): Unit = {
25+
println(twoSum(Array(2,7,11,15), 18).mkString("[", ",", "]")) // [1,2]
26+
println(twoSum(Array(2,7,11,15), 9).mkString("[", ",", "]")) // [0,1]
27+
println(twoSum(Array(2,7,11,15), 26).mkString("[", ",", "]")) // [2,3]
28+
}
3129
}

ScalaSolutions/src/main/scala/ValidParentheses_p20/Solution.scala

+25-22
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,32 @@ import scala.collection.mutable.Stack
1414
*/
1515
object Solution {
1616

17+
import scala.collection.mutable.Stack
18+
val stack = new Stack[Char]()
19+
val OpenToClose: Map[Char, Char] = Map('{' -> '}', '[' -> ']', '(' -> ')')
20+
21+
def checkPar(char: Char): Boolean =
22+
if (OpenToClose.contains(char)) {
23+
stack.push(char)
24+
true
25+
} else if (stack.size > 0 && OpenToClose.get(stack.top) == Some(char)) {
26+
stack.pop()
27+
true
28+
} else {
29+
false
30+
}
31+
32+
def isValid(s: String): Boolean = {
33+
stack.clear()
34+
35+
s.size > 0 && // check edge case for empty string
36+
s.forall(checkPar) && // run validation agains string elements
37+
stack.size == 0 // if after check stack is not empty this is fail case
38+
}
39+
1740
def main(args: Array[String]): Unit = {
18-
println(!isValid("["))
19-
println(isValid("()[]{}")) // all should be true
41+
println(!isValid("[")) // all should be true
42+
println(isValid("()[]{}"))
2043
println(isValid("()"))
2144
println(!isValid("(]"))
2245
println(isValid("[()]"))
@@ -26,24 +49,4 @@ object Solution {
2649
println(!isValid("{{[](A}}}}"))
2750
println(!isValid("{[(])}"))
2851
}
29-
30-
def isValid(s: String): Boolean = {
31-
import scala.collection.mutable.Stack
32-
val stack = new Stack[Char]()
33-
val OpenToClose: Map[Char, Char] = Map('{' -> '}', '[' -> ']', '(' -> ')')
34-
35-
def checkPar(char: Char): Boolean = {
36-
if (OpenToClose.contains(char)) {
37-
stack.push(char)
38-
true
39-
} else if (stack.size > 0 && OpenToClose.get(stack.top) == Some(char)) {
40-
stack.pop()
41-
true
42-
} else {
43-
false
44-
}
45-
}
46-
47-
s.size > 0 && s.forall(checkPar) && stack.size == 0
48-
}
4952
}

0 commit comments

Comments
 (0)