This commit is contained in:
Kagura 2024-05-18 15:11:16 +08:00
parent 112b3b756f
commit c765d131ec
11 changed files with 552 additions and 3 deletions

View file

@ -1,4 +1,4 @@
fun main() {
val t = Test283()
val t = Test226()
t.test()
}

24
src/Test101.kt Normal file
View file

@ -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
}
}
}

60
src/Test104.kt Normal file
View file

@ -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<Int?>, 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
}
}

51
src/Test141.kt Normal file
View file

@ -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<ListNode>(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
}
}

73
src/Test142.kt Normal file
View file

@ -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
}
}

64
src/Test206.kt Normal file
View file

@ -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)
}
}

80
src/Test21.kt Normal file
View file

@ -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)
}
}

77
src/Test226.kt Normal file
View file

@ -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<Int>, 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<Int?> {
val result = mutableListOf<Int?>()
val queue = ArrayDeque<TreeNode?>()
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
}

64
src/Test234.kt Normal file
View file

@ -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<Int>()
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
}
}

View file

@ -4,8 +4,7 @@ class Test49 {
fun groupAnagrams(strs: Array<String>): List<List<String>> {
var map = HashMap<Int,Int>() // Index Identifier
return listOf(listOf(""))
}

57
src/Test94.kt Normal file
View file

@ -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<Int> {
if (root == null) {
return listOf()
}
val result = mutableListOf<Int>()
inordertraversalRecusive(root, result)
return result
}
fun inordertraversalRecusive(root: TreeNode,list: MutableList<Int>){
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<Int?>, 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]
}
}