class Type {
fun function(): Int = 1
}
fun nullable(): Type? = null
fun nonnull(): Type = Type()
val a = nullable() // Gives null
?.function() // Gives nullable Int
val b = nonnull() // Gives nonnull
.function() // Gives nonnull Int
class Type {
fun function(): Int? = null
}
fun nullable(): Type? = null
fun nonnull(): Type = Type()
val a = nullable() // Gives null
?.function() // Gives nullable Int
val b = nonnull() // Gives nonnull
.function() // Gives nullable Int
class Type {
fun function(): Int? = null
}
fun nullable(): Type? = null
fun nonnull(): Type = Type()
val a = nullable() // Gives null
?.function() // Gives nullable Int
?: 1 // Gives nonnull
class Position(val x: Int, val y: Int) {
companion object {
fun fromInts(x: Int, y: Int): Postiion {
return Position(x, y)
}
}
fun toInts(): Pair<Int, Int> {
return x to y
}
}
Leetcode - 100. Same Tree
- 給定兩個二元樹的起始節點,請判斷兩個樹是否完全相等。
- 請在不更動以下的程式碼為前提解題。
- 提示:使用物件類別!!
Wrap up - 挑戰時間!
進階挑戰!
class Solution {
// 提示:在這裡定義資料型別Node
fun isSameTree(p: TreeNode?, q: TreeNode?): Boolean {
val left = Node.fromTreeNode(p)
val right = Node.fromTreeNode(q)
return left == right
}
}