From dea7cf2f925827400404f9fb1646bf36fe21bf60 Mon Sep 17 00:00:00 2001 From: Kagura Date: Mon, 26 Aug 2024 14:35:43 +0800 Subject: [PATCH] 0826 --- src/Main.kt | 2 +- src/Test125.kt | 28 +++++++++++++++++ src/Test19.kt | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/Test26.kt | 24 +++++++++++++++ src/Test46.kt | 8 ----- src/Test48.kt | 47 +++++++++++++++++++++++++++++ src/Test54.kt | 54 +++++++++++++++++++++++++++++++++ src/Test658.kt | 23 ++++++++++++++ 8 files changed, 259 insertions(+), 9 deletions(-) create mode 100644 src/Test125.kt create mode 100644 src/Test19.kt create mode 100644 src/Test26.kt delete mode 100644 src/Test46.kt create mode 100644 src/Test48.kt create mode 100644 src/Test54.kt create mode 100644 src/Test658.kt diff --git a/src/Main.kt b/src/Main.kt index d199a1d..c2585ce 100644 --- a/src/Main.kt +++ b/src/Main.kt @@ -1,4 +1,4 @@ fun main() { - val t = Test763() + val t = Test125() t.test() } \ No newline at end of file diff --git a/src/Test125.kt b/src/Test125.kt new file mode 100644 index 0000000..f7b68a8 --- /dev/null +++ b/src/Test125.kt @@ -0,0 +1,28 @@ +class Test125 { + class Solution { + fun isPalindrome(s: String): Boolean { + val sb = StringBuffer() + s.lowercase().forEach { + if (it in 'a'..'z' || it in '0'..'9'){ + sb.append(it) + } + } + var l = 0 + var r = sb.lastIndex + while (l null + else -> slower.next!!.next + } + + return head + } + } + + fun test() { + val solution = Solution() + + // 测试用例 1 + val head1 = ListNode(1).apply { + next = ListNode(2).apply { + next = ListNode(3).apply { + next = ListNode(4).apply { + next = ListNode(5) + } + } + } + } + val result1 = solution.removeNthFromEnd(head1, 2) + printList(result1) // 预期输出: [1, 2, 3, 5] + + // 测试用例 2 + val head2 = ListNode(1) + val result2 = solution.removeNthFromEnd(head2, 1) + printList(result2) // 预期输出: [] + + // 测试用例 3 + val head3 = ListNode(1).apply { + next = ListNode(2) + } + val result3 = solution.removeNthFromEnd(head3, 1) + printList(result3) // 预期输出: [1] + + val head4 = ListNode(1).apply { + next = ListNode(2) + } + val result4 = solution.removeNthFromEnd(head4, 2) + printList(result4) // 预期输出: [2] + } + + // 辅助函数:打印链表 + fun printList(head: ListNode?) { + var current = head + val result = mutableListOf() + while (current != null) { + result.add(current.`val`) + current = current.next + } + println(result) + } +} \ No newline at end of file diff --git a/src/Test26.kt b/src/Test26.kt new file mode 100644 index 0000000..4e2bc59 --- /dev/null +++ b/src/Test26.kt @@ -0,0 +1,24 @@ +class Test26 { + class Solution { + fun removeDuplicates(nums: IntArray): Int { + var unique = 0 + for (i in 1..> { - val result = arrayOf(intArrayOf()) - for (i in) - } - } -} \ No newline at end of file diff --git a/src/Test48.kt b/src/Test48.kt new file mode 100644 index 0000000..c696721 --- /dev/null +++ b/src/Test48.kt @@ -0,0 +1,47 @@ +class Test48 { + class Solution { + fun rotate(matrix: Array): Unit { + for (i in 0..(matrix.size-1)/2){ + matrix[i] = matrix[matrix.size-1-i] + .also { matrix[matrix.size-1-i] = matrix[i] } + } + + for (i in 1..) { + matrix.forEach { row -> + println(row.joinToString(prefix = "[", postfix = "]", separator = ",")) + } + } +} \ No newline at end of file diff --git a/src/Test54.kt b/src/Test54.kt new file mode 100644 index 0000000..40ae20a --- /dev/null +++ b/src/Test54.kt @@ -0,0 +1,54 @@ +class Test54 { + class Solution { + fun spiralOrder(matrix: Array): List { + val column = matrix[0].size + val row = matrix.size + val result = MutableList(column * row) { 0 } + + var read = 0 + var i = 0 + var j = 0 + var direction = 0 // →0 ↓1 ←2 ↑3 + var round = 0 + + while (read != column * row){ + result[read] = matrix[i][j] + read++ + + // check boundary + if ((direction == 0 || direction == 1 ) && j + 1 + round == column){ // at rightmost + if (i - round == 0){ //right up + direction = 1 + } + if (i + 1 + round == row){ + direction = 2 + } + } else if ((direction == 2 || direction == 3 ) &&j - round == 0){ // left + if (i + 1 + round == row){ + direction = 3 + } + if (i - round -1 == 0){ + direction = 0 + round ++ + } + } + + // move pointer + when (direction){ + 0 -> j++ + 1 -> i++ + 2 -> j-- + 3 -> i-- + } + } + + return result + } + } + fun test(){ + val solution = Solution() + println(solution.spiralOrder(arrayOf(intArrayOf(1,2,3), intArrayOf(4,5,6), intArrayOf(7,8,9)))) + println(solution.spiralOrder(arrayOf(intArrayOf(1,2,3,4), intArrayOf(5,6,7,8), intArrayOf(9,10,11,12)))) + + } +} \ No newline at end of file diff --git a/src/Test658.kt b/src/Test658.kt new file mode 100644 index 0000000..2b1d8d1 --- /dev/null +++ b/src/Test658.kt @@ -0,0 +1,23 @@ +import kotlin.math.abs + +class Test658 { + class Solution { + fun findClosestElements(arr: IntArray, k: Int, x: Int): List { + var left = 0 + var right = arr.size - 1 + while (right - left + 1 != k){ + if (abs(arr[left] - x) <= abs(arr[right] - x)){ + right-- + }else{ + left ++ + } + } + return arr.slice(left..right) + } + } + + fun test(){ + println(Solution().findClosestElements(intArrayOf(1,2,3,4,5),4,3)) + println(Solution().findClosestElements(intArrayOf(1,2,3,4,5),4,-1)) + } +} \ No newline at end of file