Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6cf7003

Browse files
committed
easy ones
1 parent fc897a1 commit 6cf7003

12 files changed

+149
-0
lines changed

‎101_symmetric_tree.scala‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
object Solution {
2+
def isSymmetric(root: TreeNode): Boolean = {
3+
if (root == null) true
4+
else isEqual(root.left, root.right)
5+
}
6+
7+
def isEqual(left: TreeNode, right: TreeNode): Boolean = {
8+
if (left == null) right == null
9+
else if (right == null) left == null
10+
else (left.value == right.value) && isEqual(left.left, right.right) && isEqual(left.right, right.left)
11+
}
12+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
object Solution {
2+
def maxDepth(root: TreeNode): Int = {
3+
if (root == null) return 0
4+
else Math.max(1 + maxDepth(root.left), 1 + maxDepth(root.right))
5+
}
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
object Solution {
2+
def maxProfit(prices: Array[Int]): Int = {
3+
if (prices.isEmpty) 0
4+
else prices.scanLeft(prices.head)(Math.min(_, _)).tail zip prices map (x => x._2 - x._1) reduce(Math.max(_, _))
5+
}
6+
}

‎136_single_number.scala‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
object Solution {
2+
def singleNumber(nums: Array[Int]): Int = {
3+
nums.reduce(_^_)
4+
}
5+
}

‎13_roman_to_integer.scala‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
object Solution {
2+
def romanToInt(s: String): Int = {
3+
if (s == "") 0
4+
else s.head match {
5+
case 'V' => 5 + romanToInt(s.tail)
6+
case 'L' => 50 + romanToInt(s.tail)
7+
case 'D' => 500 + romanToInt(s.tail)
8+
case 'M' => 1000 + romanToInt(s.tail)
9+
case 'I' => if (s.tail == "") 1 else s.tail.head match {
10+
case 'V' => 4 + romanToInt(s.tail.tail)
11+
case 'X' => 9 + romanToInt(s.tail.tail)
12+
case _ => 1 + romanToInt(s.tail)
13+
}
14+
case 'X' => if (s.tail == "") 10 else s.tail.head match {
15+
case 'L' => 40 + romanToInt(s.tail.tail)
16+
case 'C' => 90 + romanToInt(s.tail.tail)
17+
case _ => 10 + romanToInt(s.tail)
18+
}
19+
case 'C' => if (s.tail == "") 100 else s.tail.head match {
20+
case 'D' => 400 + romanToInt(s.tail.tail)
21+
case 'M' => 900 + romanToInt(s.tail.tail)
22+
case _ => 100 + romanToInt(s.tail)
23+
}
24+
}
25+
}
26+
}

‎14_longest_common_prefix.scala‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
object Solution {
2+
def longestCommonPrefix(strs: Array[String]): String = {
3+
if (strs.isEmpty) ""
4+
else{
5+
if (strs.map(_.isEmpty).reduce(_ || _)) ""
6+
else{
7+
val prefix = strs(0).head
8+
if (strs.map(_.head == prefix).reduce(_ && _)) prefix + longestCommonPrefix(strs.map(_.tail))
9+
else ""
10+
}
11+
}
12+
}
13+
}

‎20_valid_parentheses.scala‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
object Solution {
2+
def isValid(s: String): Boolean = {
3+
return resolve(s, "s") == "s"
4+
}
5+
6+
def resolve(s: String, stack: String): String = {
7+
if (s.isEmpty) stack
8+
else s.head match {
9+
case ')' => if (stack.last == '(') resolve(s.tail, stack.init) else "f"
10+
case ']' => if (stack.last == '[') resolve(s.tail, stack.init) else "f"
11+
case '}' => if (stack.last == '{') resolve(s.tail, stack.init) else "f"
12+
case c => resolve(s.tail, stack + c)
13+
}
14+
}
15+
}

‎21_merge_two_sorted_lists.scala‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
object Solution {
2+
def mergeTwoLists(l1: ListNode, l2: ListNode): ListNode = {
3+
if (l1 == null) return l2
4+
else if (l2 == null) return l1
5+
else if(l1.x < l2.x) {
6+
val ret = new ListNode(l1.x)
7+
ret.next = mergeTwoLists(l1.next, l2)
8+
ret
9+
}else{
10+
val ret = new ListNode(l2.x)
11+
ret.next = mergeTwoLists(l1, l2.next)
12+
ret
13+
}
14+
}
15+
}

‎53_maximum_subarray.scala‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
object Solution {
2+
def maxSubArray(nums: Array[Int]): Int = {
3+
val accSum = nums.toList.scanLeft(0)(_ + _).tail
4+
(accSum.scanLeft(0)(Math.min(_, _)).init zip accSum).map(x => x._2 - x._1).max
5+
}
6+
}

‎70_climbing_stairs.scala‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
object Solution {
2+
3+
val mat_mul = (x: (Int, Int, Int, Int), y: (Int, Int, Int, Int)) => (x._1 * y._1 + x._2 * y._3, x._1 * y._2 + x._2 * y._4, x._3 * y._1 + x._4 * y._3, x._3 * y._2 + x._4 * y._4)
4+
5+
def fib_mat(n: Int): (Int, Int, Int, Int) = {
6+
if (n == 1) (1, 1, 1, 0)
7+
else if (n % 2 == 0) {
8+
val fib_half = fib_mat(n / 2)
9+
mat_mul(fib_half, fib_half)
10+
}else{
11+
val fib_half = fib_mat(n / 2)
12+
mat_mul(mat_mul(fib_half, fib_half), (1, 1, 1, 0))
13+
}
14+
}
15+
16+
def climbStairs(n: Int): Int = {
17+
if (n == 1) 1
18+
else{
19+
val mat = fib_mat(n - 1)
20+
mat._1 + mat._2
21+
}
22+
}
23+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /