From c765d131ec4a0c0794da9484717851c9433d2c9d Mon Sep 17 00:00:00 2001 From: Kagura Date: Sat, 18 May 2024 15:11:16 +0800 Subject: [PATCH] Stage --- src/Main.kt | 2 +- src/Test101.kt | 24 +++++++++++++++ src/Test104.kt | 60 +++++++++++++++++++++++++++++++++++++ src/Test141.kt | 51 ++++++++++++++++++++++++++++++++ src/Test142.kt | 73 +++++++++++++++++++++++++++++++++++++++++++++ src/Test206.kt | 64 ++++++++++++++++++++++++++++++++++++++++ src/Test21.kt | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/Test226.kt | 77 ++++++++++++++++++++++++++++++++++++++++++++++++ src/Test234.kt | 64 ++++++++++++++++++++++++++++++++++++++++ src/Test49.kt | 3 +- src/Test94.kt | 57 +++++++++++++++++++++++++++++++++++ 11 files changed, 552 insertions(+), 3 deletions(-) create mode 100644 src/Test101.kt create mode 100644 src/Test104.kt create mode 100644 src/Test141.kt create mode 100644 src/Test142.kt create mode 100644 src/Test206.kt create mode 100644 src/Test21.kt create mode 100644 src/Test226.kt create mode 100644 src/Test234.kt create mode 100644 src/Test94.kt diff --git a/src/Main.kt b/src/Main.kt index e3303b5..4bbd649 100644 --- a/src/Main.kt +++ b/src/Main.kt @@ -1,4 +1,4 @@ fun main() { - val t = Test283() + val t = Test226() t.test() } \ No newline at end of file diff --git a/src/Test101.kt b/src/Test101.kt new file mode 100644 index 0000000..5c1752f --- /dev/null +++ b/src/Test101.kt @@ -0,0 +1,24 @@ +class Test101 { + class TreeNode(var `val`: Int) { + var left: TreeNode? = null + var right: TreeNode? = null + } + class Solution { + fun isSymmetric(root: TreeNode?): Boolean { + if (root == null){ + return true + } + + var pl = root.left + var pr = root.right + + + + + } + } + + + + +} \ No newline at end of file diff --git a/src/Test104.kt b/src/Test104.kt new file mode 100644 index 0000000..0870ebd --- /dev/null +++ b/src/Test104.kt @@ -0,0 +1,60 @@ +import kotlin.math.max + +class Test104 { + class TreeNode(var `val`: Int) { + var left: TreeNode? = null + var right: TreeNode? = null + } + + class Solution { + fun maxDepth(root: TreeNode?): Int { + if (root==null){ + return 0 + } + return getDepth(root) + } + + fun getDepth(root: TreeNode): Int{ + var l = 0 + var r = 0 + if (root.left!=null){ + l = getDepth(root.left!!) + } + if (root.right!=null){ + r = getDepth(root.right!!) + } + return max(l,r)+1 + } + } + + fun test() { + // Helper function to build a binary tree from an array representation + fun buildTree(arr: Array, index: Int = 0): TreeNode? { + if (index >= arr.size || arr[index] == null) { + return null + } + val node = TreeNode(arr[index]!!) + node.left = buildTree(arr, 2 * index + 1) + node.right = buildTree(arr, 2 * index + 2) + return node + } + + // Example test case 1 + val treeArray1 = arrayOf(3, 9, 20, null, null, 15, 7) + val root1 = buildTree(treeArray1) + + // Example test case 2 + val treeArray2 = arrayOf(1, null, 2) + val root2 = buildTree(treeArray2) + + // Create an instance of Solution + val solution = Solution() + + // Call maxDepth and print the results + val result1 = solution.maxDepth(root1) + println(result1) // Expected output: 3 + + val result2 = solution.maxDepth(root2) + println(result2) // Expected output: 2 + } +} \ No newline at end of file diff --git a/src/Test141.kt b/src/Test141.kt new file mode 100644 index 0000000..cc4b21a --- /dev/null +++ b/src/Test141.kt @@ -0,0 +1,51 @@ +class Test141 { + class ListNode(var `val`: Int) { + var next: ListNode? = null + } + + class Solution { + fun hasCycle(head: ListNode?): Boolean { + if (head==null){ + return false + } + var occurList = arrayListOf(head) + + var p = head.next + + while (p!=null){ + if (occurList.contains(p)){ + return true + } + occurList.add(p) + p = p.next + } + return false + } + } + + + fun test() { + val solution = Solution() + + // Create a linked list with a cycle + val headWithCycle = ListNode(1) + headWithCycle.next = ListNode(2) + headWithCycle.next?.next = ListNode(3) + headWithCycle.next?.next?.next = ListNode(4) + headWithCycle.next?.next?.next?.next = ListNode(5) + headWithCycle.next?.next?.next?.next?.next = headWithCycle.next?.next + + println("Testing linked list with a cycle") + println("Has cycle: ${solution.hasCycle(headWithCycle)}") // Should print true + + // Create a linked list without a cycle + val headWithoutCycle = ListNode(1) + headWithoutCycle.next = ListNode(2) + headWithoutCycle.next?.next = ListNode(3) + headWithoutCycle.next?.next?.next = ListNode(4) + headWithoutCycle.next?.next?.next?.next = ListNode(5) + + println("Testing linked list without a cycle") + println("Has cycle: ${solution.hasCycle(headWithoutCycle)}") // Should print false + } +} \ No newline at end of file diff --git a/src/Test142.kt b/src/Test142.kt new file mode 100644 index 0000000..cfb07a5 --- /dev/null +++ b/src/Test142.kt @@ -0,0 +1,73 @@ +class Test142 { + class ListNode(var `val`: Int) { + var next: ListNode? = null + } + + class Solution { + fun detectCycle(head: ListNode?): ListNode? { + if (head == null) { + return null + } + + var slow = head.next + var fast = head.next?.next + + while (fast?.next != null) { + if (fast == slow) { + var h = head + while (h != fast) { + h = h!!.next + fast = fast!!.next + } + return h + } + fast = fast.next?.next + slow = slow!!.next + } + + return null + } + + } + + fun test() { + val solution = Solution() + + // Create a linked list with a cycle + val headWithCycle = ListNode(3) + val second = ListNode(2) + val third = ListNode(0) + val fourth = ListNode(-4) + headWithCycle.next = second + second.next = third + third.next = fourth + fourth.next = second // Creating a cycle at node with value 2 + + println("Testing linked list with a cycle at node with value 2") + val cycleNode = solution.detectCycle(headWithCycle) + println("Cycle starts at node with value: ${cycleNode?.`val`}") // Should print 2 + + // Create a linked list without a cycle + val headWithoutCycle = ListNode(1) + headWithoutCycle.next = ListNode(2) + + println("Testing linked list without a cycle") + val noCycleNode = solution.detectCycle(headWithoutCycle) + println("Cycle starts at node: ${noCycleNode?.`val`}") // Should print null + + // Create another linked list with a cycle + val headWithCycle2 = ListNode(1) + headWithCycle2.next = headWithCycle2 // Creating a cycle at the head node + + println("Testing linked list with a cycle at the head node") + val cycleNode2 = solution.detectCycle(headWithCycle2) + println("Cycle starts at node with value: ${cycleNode2?.`val`}") // Should print 1 + + // Create a single node linked list without a cycle + val singleNode = ListNode(1) + + println("Testing single node linked list without a cycle") + val singleNodeCycle = solution.detectCycle(singleNode) + println("Cycle starts at node: ${singleNodeCycle?.`val`}") // Should print null + } +} \ No newline at end of file diff --git a/src/Test206.kt b/src/Test206.kt new file mode 100644 index 0000000..eec109b --- /dev/null +++ b/src/Test206.kt @@ -0,0 +1,64 @@ +class Test206 { + class ListNode(var `val`: Int) { + var next: ListNode? = null + } + + class Solution { + fun reverseList(head: ListNode?): ListNode? { + if (head == null) { + return null + } + + var node = head + while (node!!.next!=null){ + node = node.next!! + } + + reverse(head,head.next?.next) + + head.next = null + return node + } + + fun reverse(head: ListNode, nextNode:ListNode?) { // head要两个,next要一个 + if (head.next == null) { + return + } + + head.next!!.next = head + + if (nextNode!= null) { + reverse(nextNode,nextNode.next?.next) + nextNode.next = head.next + } + } + } + + fun printList(head: ListNode?) { + var temp = head + while (temp != null) { + print("${temp.`val`} -> ") + temp = temp.next + } + println("null") + } + + fun test(){ + val solution = Solution() + val head = ListNode(1) + head.next = ListNode(2) + head.next?.next = ListNode(3) + head.next?.next?.next = ListNode(4) + head.next?.next?.next?.next = ListNode(5) + + println("Original List:") + printList(head) + + val reversedHead = solution.reverseList(head) + + println("Reversed List:") + printList(reversedHead) + + } + +} \ No newline at end of file diff --git a/src/Test21.kt b/src/Test21.kt new file mode 100644 index 0000000..9984025 --- /dev/null +++ b/src/Test21.kt @@ -0,0 +1,80 @@ +class Test21 { + class ListNode(var `val`: Int) { + var next: ListNode? = null + } + + class Solution { + fun mergeTwoLists(list1: ListNode?, list2: ListNode?): ListNode? { + if (list1 == null && list2 == null){ + return null + } + if (list1 == null){ + return list2 + } + if (list2 == null){ + return list1 + } + + + var p1 = list1 + var p2 = list2 + val head = ListNode(-1) + var result = head + + while (p1!=null && p2!=null){ + if (p1.`val`<=p2.`val`){ + result.next = p1 + p1 = p1.next + result = result.next!! + }else{ + result.next = p2 + p2 = p2.next + result = result.next!! + } + } + if (p1!=null){ + result.next = p1 + }else{ + result.next = p2 + } + + return head.next + } + } + + fun printList(head: ListNode?) { + var temp = head + while (temp != null) { + print("${temp.`val`} -> ") + temp = temp.next + } + println("null") + } + + fun test() { + val solution = Solution() + + // Create first sorted linked list: 1 -> 2 -> 4 + val list1 = ListNode(1) + list1.next = ListNode(2) + list1.next?.next = ListNode(4) + + // Create second sorted linked list: 1 -> 3 -> 4 + val list2 = ListNode(1) + list2.next = ListNode(3) + list2.next?.next = ListNode(4) + + println("List 1:") + printList(list1) + + println("List 2:") + printList(list2) + + // Merge the two lists + val mergedList = solution.mergeTwoLists(list1, list2) + + println("Merged List:") + printList(mergedList) + } + +} \ No newline at end of file diff --git a/src/Test226.kt b/src/Test226.kt new file mode 100644 index 0000000..4835164 --- /dev/null +++ b/src/Test226.kt @@ -0,0 +1,77 @@ +class Test226 { + class TreeNode(var `val`: Int) { + var left: TreeNode? = null + var right: TreeNode? = null + } + class Solution { + fun invertTree(root: TreeNode?): TreeNode? { + if (root == null){ + return null + } + + val right = root.right + root.right = invertTree(root.left) + root.left = invertTree(right) + + return root + } + } + + fun test() { + // Helper function to build a binary tree from an array representation + fun buildTree(arr: Array, index: Int = 0): TreeNode? { + if (index >= arr.size) { + return null + } + val node = TreeNode(arr[index]) + node.left = buildTree(arr, 2 * index + 1) + node.right = buildTree(arr, 2 * index + 2) + return node + } + + // Helper function to convert a binary tree to an array representation (level-order traversal) + fun treeToArray(root: TreeNode?): List { + val result = mutableListOf() + val queue = ArrayDeque() + queue.add(root) + while (queue.isNotEmpty()) { + val node = queue.removeFirst() + if (node != null) { + result.add(node.`val`) + queue.add(node.left) + queue.add(node.right) + } else { + result.add(null) + } + } + // Trim trailing nulls + while (result.isNotEmpty() && result.last() == null) { + result.removeAt(result.size - 1) + } + return result + } + + // Example test case 1 + val treeArray1 = arrayOf(4, 2, 7, 1, 3, 6, 9) + val root1 = buildTree(treeArray1) + + // Create an instance of Solution + val solution = Solution() + + // Call invertTree and print the results + val invertedRoot1 = solution.invertTree(root1) + val invertedArray1 = treeToArray(invertedRoot1) + println(invertedArray1) // Expected output: [4, 7, 2, 9, 6, 3, 1] + + // Example test case 2 + val treeArray2 = arrayOf(2, 1, 3) + val root2 = buildTree(treeArray2) + + val invertedRoot2 = solution.invertTree(root2) + val invertedArray2 = treeToArray(invertedRoot2) + println(invertedArray2) // Expected output: [2, 3, 1] + } + +// Call the test function + +} \ No newline at end of file diff --git a/src/Test234.kt b/src/Test234.kt new file mode 100644 index 0000000..33b3b20 --- /dev/null +++ b/src/Test234.kt @@ -0,0 +1,64 @@ +import java.util.ArrayDeque + +class Test234 { + class ListNode(var `val`: Int) { + var next: ListNode? = null + } + class Solution { + fun isPalindrome(head: ListNode?): Boolean { + if (head==null){ + return true + } + val deque = ArrayDeque() + + var headptr = head + while (headptr!=null){ + deque.add(headptr.`val`) + headptr = headptr.next + } + + while (deque.size>1){ + if (deque.first != deque.last){ + return false + } + deque.removeFirst() + deque.removeLast() + } + return true + } + } + + fun printList(head: ListNode?) { + var temp = head + while (temp != null) { + print("${temp.`val`} -> ") + temp = temp.next + } + println("null") + } + + fun test() { + val solution = Solution() + + // Create a palindrome list: 1 -> 2 -> 3 -> 2 -> 1 + val head1 = ListNode(1) + head1.next = ListNode(2) + head1.next?.next = ListNode(3) + head1.next?.next?.next = ListNode(2) + head1.next?.next?.next?.next = ListNode(1) + + println("Testing palindrome list 1 -> 2 -> 3 -> 2 -> 1") + println("Is palindrome: ${solution.isPalindrome(head1)}") // Should print true + + // Create a non-palindrome list: 1 -> 2 -> 3 -> 4 -> 5 + val head2 = ListNode(1) + head2.next = ListNode(2) + head2.next?.next = ListNode(3) + head2.next?.next?.next = ListNode(4) + head2.next?.next?.next?.next = ListNode(5) + + println("Testing non-palindrome list 1 -> 2 -> 3 -> 4 -> 5") + println("Is palindrome: ${solution.isPalindrome(head2)}") // Should print false + } + +} \ No newline at end of file diff --git a/src/Test49.kt b/src/Test49.kt index ea5ec6a..3332c2a 100644 --- a/src/Test49.kt +++ b/src/Test49.kt @@ -4,8 +4,7 @@ class Test49 { fun groupAnagrams(strs: Array): List> { var map = HashMap() // Index Identifier - - + return listOf(listOf("")) } diff --git a/src/Test94.kt b/src/Test94.kt new file mode 100644 index 0000000..4e73506 --- /dev/null +++ b/src/Test94.kt @@ -0,0 +1,57 @@ +import kotlin.collections.MutableList + +class Test94 { + class TreeNode(var `val`: Int) { + var left: TreeNode? = null + var right: TreeNode? = null + } + + class Solution { + fun inorderTraversal(root: TreeNode?): List { + if (root == null) { + return listOf() + } + val result = mutableListOf() + inordertraversalRecusive(root, result) + return result + } + + fun inordertraversalRecusive(root: TreeNode,list: MutableList){ + if (root.left!=null){ + inordertraversalRecusive(root.left!!,list) + } + list.add(root.`val`) + if (root.right!=null){ + inordertraversalRecusive(root.right!!,list) + } + } + } + + fun test() { + // Helper function to build a binary tree from an array representation + fun buildTree(arr: Array, index: Int = 0): TreeNode? { + if (index >= arr.size || arr[index] == null) { + return null + } + val node = TreeNode(arr[index]!!) + node.left = buildTree(arr, 2 * index + 1) + node.right = buildTree(arr, 2 * index + 2) + return node + } + + // Example test case + val treeArray = arrayOf(1, null, 2, null, null, 3) + val root = buildTree(treeArray) + + // Create an instance of Solution + val solution = Solution() + + // Call inorderTraversal and print the result + val result = solution.inorderTraversal(root) + println(result) // Expected output: [1, 3, 2] + } + + + + +} \ No newline at end of file