Skip to content

Commit cfd10ba

Browse files
committed
updates
1 parent 3859705 commit cfd10ba

3 files changed

+27
-0
lines changed

169_majority_element.scala

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import scala.util.Sorting.quickSort
2+
3+
object Solution {
4+
def majorityElement(nums: Array[Int]): Int = {
5+
val nums_sorted = scala.util.Sorting.quickSort(nums)
6+
nums(nums.length / 2)
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
object Solution {
2+
def lengthOfLongestSubstring(s: String): Int = {
3+
s.scanLeft("")(((x: String, y: Char) => x.substring(1 + x.indexOf(y)) + y)).map(_.length).reduce(Math.max)
4+
}
5+
}

69_sqrt.scala

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
object Solution {
2+
3+
def mySqrt(x: Int, beg: Int = 0, end2: Int = -1): Int = {
4+
val end = if (end2 < 0) x else end2
5+
if (x == 1){
6+
1
7+
} else if (end - beg <= 1) {
8+
beg
9+
} else {
10+
val mid = ((beg + end) / 2).toLong
11+
if (mid * mid > x.toLong) mySqrt(x, beg, mid.toInt) else mySqrt(x, mid.toInt, end)
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)