From 6c945e40feb8743962ba7d9678d9a4aadc3d143f Mon Sep 17 00:00:00 2001 From: 2giwon <2giwon@gmail.com> Date: Fri, 27 Nov 2020 18:07:04 +0900 Subject: [PATCH 1/4] Range Sum of BST --- 2giwon/RangeSumBST.kt | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 2giwon/RangeSumBST.kt diff --git a/2giwon/RangeSumBST.kt b/2giwon/RangeSumBST.kt new file mode 100644 index 0000000..26e022f --- /dev/null +++ b/2giwon/RangeSumBST.kt @@ -0,0 +1,14 @@ +class RangeSumBST { + fun rangeSumBST(root: TreeNode?, low: Int, high: Int): Int { + var result = 0 + root?.let { + if (root.`val` in low..high) { + result += root.`val` + } + result += rangeSumBST(root.left, low, high) + result += rangeSumBST(root.right, low, high) + } + + return result + } +} \ No newline at end of file From ce1f5a19bd6f69f42029b126c5f6f4a8d63909bc Mon Sep 17 00:00:00 2001 From: 2giwon <2giwon@gmail.com> Date: Fri, 4 Dec 2020 14:57:34 +0900 Subject: [PATCH 2/4] Decode String --- 2giwon/DecodeString.kt | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 2giwon/DecodeString.kt diff --git a/2giwon/DecodeString.kt b/2giwon/DecodeString.kt new file mode 100644 index 0000000..f056cff --- /dev/null +++ b/2giwon/DecodeString.kt @@ -0,0 +1,41 @@ +package leetcode.study + +import java.util.* + +class DecodeString { + fun decodeString(s: String): String { + var result = "" + var count = "" + val stack = Stack() + val indexStack = Stack() + + var i = 0 + while (i < s.length) { + when { + s[i] == '[' -> { + stack.push(count.toInt()) + indexStack.push(i) + count = "" + } + s[i] == ']' -> { + if (stack[stack.lastIndex] <= 1) { + stack.pop() + indexStack.pop() + } else { + stack[stack.lastIndex]-- + i = indexStack.peek() + } + } + Character.isDigit(s[i]) -> { + count += s[i] + } + else -> { + result += s[i] + } + } + i++ + } + + return result + } +} \ No newline at end of file From 2f0a54f33b10fbb943129f32ae6e50db499dc55f Mon Sep 17 00:00:00 2001 From: 2giwon <2giwon@gmail.com> Date: Mon, 7 Dec 2020 17:10:11 +0900 Subject: [PATCH 3/4] Increasing Order Search Tree --- 2giwon/IncreasingBST.kt | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 2giwon/IncreasingBST.kt diff --git a/2giwon/IncreasingBST.kt b/2giwon/IncreasingBST.kt new file mode 100644 index 0000000..e7b3f6d --- /dev/null +++ b/2giwon/IncreasingBST.kt @@ -0,0 +1,18 @@ +class IncreasingBST { + private var current: TreeNode? = null + fun increasingBST(root: TreeNode?): TreeNode? { + val answer = TreeNode(0) + current = answer + inOrder(root) + return answer.right + } + + private fun inOrder(node: TreeNode?) { + if (node == null) return + inOrder(node.left) + node.left = null + current?.right = node + current = node + inOrder(node.right) + } +} \ No newline at end of file From ec00a9aa6e4b4382d4f38f9ac0689b5b95901f0e Mon Sep 17 00:00:00 2001 From: 2giwon <2giwon@gmail.com> Date: Wed, 16 Dec 2020 17:22:00 +0900 Subject: [PATCH 4/4] spiral matrix 2 --- 2giwon/SpiralMatrix2.kt | 43 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 2giwon/SpiralMatrix2.kt diff --git a/2giwon/SpiralMatrix2.kt b/2giwon/SpiralMatrix2.kt new file mode 100644 index 0000000..e0057b2 --- /dev/null +++ b/2giwon/SpiralMatrix2.kt @@ -0,0 +1,43 @@ +class SpiralMatrix2 { + private lateinit var matrix: Array + private var number = 1 + + fun generateMatrix(n: Int): Array { + matrix = Array(n) { IntArray(n) { 0 } } + + val directions: Array> = arrayOf(0 to 1, 1 to 0, 0 to -1, -1 to 0) + var directionIndex = 0 + var col = 0 + var row = 0 + + matrix[col][row] = number++ + + while (true) { + if (number > n * n) break + while (true) { + val direction = directions[directionIndex] + if (row + direction.second !in 0 until n || col + direction.first !in 0 until n) break + + if (move(col + direction.first, row + direction.second)) { + col += direction.first + row += direction.second + } else { + break + } + } + + directionIndex = if (directionIndex + 1 >= directions.size) 0 else directionIndex + 1 + } + + return matrix + } + + private fun move(col: Int, row: Int): Boolean { + return if (matrix[col][row] == 0) { + matrix[col][row] = number++ + true + } else { + false + } + } +} \ No newline at end of file