File tree Expand file tree Collapse file tree 5 files changed +116
-0
lines changed
product-of-array-except-self
validate-binary-search-tree Expand file tree Collapse file tree 5 files changed +116
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ fun threeSum (nums : IntArray ): List <List <Int >> {
3
+ if (nums.size < 3 ) return emptyList()
4
+
5
+ val result = mutableSetOf<List <Int >>()
6
+
7
+ nums.sort()
8
+
9
+ for (i in 0 until nums.size - 2 ) {
10
+
11
+ if (i > 0 && nums[i] == nums[i - 1 ]) continue
12
+
13
+ var left = i + 1
14
+ var right = nums.size - 1
15
+
16
+ while (left < right) {
17
+ val sum = nums[i] + nums[left] + nums[right]
18
+
19
+ when {
20
+ sum == 0 -> {
21
+ result.add(listOf (nums[i], nums[left], nums[right]))
22
+
23
+ left++
24
+ right--
25
+ }
26
+
27
+ sum < 0 -> left++
28
+ else -> right--
29
+ }
30
+ }
31
+ }
32
+
33
+ return result.toList()
34
+ }
35
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ fun climbStairs (n : Int ): Int {
3
+ if (n <= 2 ) return n
4
+
5
+ var prev2 = 1
6
+ var prev1 = 2
7
+
8
+ for (i in 3 .. n) {
9
+ val current = prev1 + prev2
10
+ prev2 = prev1
11
+ prev1 = current
12
+ }
13
+
14
+ return prev1
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ fun productExceptSelf (nums : IntArray ): IntArray =
3
+ IntArray (nums.size) { 1 }.apply {
4
+ var leftProduct = 1
5
+ for (i in 1 until nums.size) {
6
+ leftProduct * = nums[i - 1 ]
7
+ this [i] = leftProduct
8
+ }
9
+
10
+ var rightProduct = 1
11
+ for (i in nums.size - 2 downTo 0 ) {
12
+ rightProduct * = nums[i + 1 ]
13
+ this [i] * = rightProduct
14
+ }
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ fun isAnagram (s : String , t : String ): Boolean {
3
+ if (s.length != t.length) return false
4
+
5
+ val charCount = mutableMapOf<Char , Int >()
6
+
7
+ s.forEach { ch ->
8
+ charCount.put(ch, charCount.getOrDefault(ch, 0 ) + 1 )
9
+ }
10
+
11
+ t.forEach { ch ->
12
+ val count = charCount.getOrDefault(ch, 0 )
13
+ if (count == 0 ) return false
14
+ charCount[ch] = count - 1
15
+ }
16
+
17
+ return true
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ /* *
2
+ * Example:
3
+ * var ti = TreeNode(5)
4
+ * var v = ti.`val`
5
+ * Definition for a binary tree node.
6
+ * class TreeNode(var `val`: Int) {
7
+ * var left: TreeNode? = null
8
+ * var right: TreeNode? = null
9
+ * }
10
+ */
11
+ class Solution {
12
+ fun isValidBST (root : TreeNode ? ): Boolean {
13
+ val values = mutableListOf<Int >()
14
+ inorderTraversal(root, values)
15
+
16
+ for (i in 1 until values.size) {
17
+ if (values[i] <= values[i - 1 ]) return false
18
+ }
19
+
20
+ return true
21
+ }
22
+
23
+ private fun inorderTraversal (root : TreeNode ? , values : MutableList <Int >) {
24
+ if (root == null ) return
25
+
26
+ inorderTraversal(root.left, values)
27
+ values.add(root.`val `)
28
+ inorderTraversal(root.right, values)
29
+ }
30
+ }
You can’t perform that action at this time.
0 commit comments