From dea7cf2f925827400404f9fb1646bf36fe21bf60 Mon Sep 17 00:00:00 2001 From: Kagura Date: Mon, 26 Aug 2024 14:35:43 +0800 Subject: [PATCH 1/3] 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 From 5c777cc6fb802c053dfe83b238920d7f816f0a9c Mon Sep 17 00:00:00 2001 From: Kagura Date: Tue, 3 Sep 2024 20:52:57 +0800 Subject: [PATCH 2/3] 09start --- src/Test27.kt | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/Test27.kt diff --git a/src/Test27.kt b/src/Test27.kt new file mode 100644 index 0000000..0de8f45 --- /dev/null +++ b/src/Test27.kt @@ -0,0 +1,47 @@ +class Test27 { + class Solution { + fun removeElement(nums: IntArray, `val`: Int): Int { + if (nums.isEmpty()){ + return 0 + } + if (nums.size == 1){ + return if (nums[0] == `val`) 0 else 1 + } + + var l = 0 + var r = nums.size - 1 + while (r >= 0 && nums[r] == `val`){ + r-- + } + + while (l <= r){ + if (nums[l] == `val`){ + nums[l] = nums[r] + do { + r-- + } while (nums[r] == `val`) + } + l++ + } + return l + } + } + + fun test(): Unit { + val s = Solution() + val a1 = arrayOf(3,2,2,3).toIntArray() + println(s.removeElement(a1,3)) + a1.forEach { print(it) } + println() + + val a2 = intArrayOf(0,1,2,2,3,0,4,2) + println(s.removeElement(a2,2)) + a2.forEach { print(it) } + println() + + val a3 = intArrayOf(3,3) + println(s.removeElement(a3,3)) + a3.forEach { print(it) } + println() + } +} \ No newline at end of file From 070b481895fb68e899cae78cb2b67e0bd59af9f9 Mon Sep 17 00:00:00 2001 From: Kagura Date: Mon, 9 Sep 2024 12:13:49 +0800 Subject: [PATCH 3/3] 0908 --- src/Main.kt | 2 +- src/Test88.kt | 41 +++++++++++++++++++++++++++++++++++++++++ src/Test977.kt | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 src/Test88.kt create mode 100644 src/Test977.kt diff --git a/src/Main.kt b/src/Main.kt index c2585ce..61c027a 100644 --- a/src/Main.kt +++ b/src/Main.kt @@ -1,4 +1,4 @@ fun main() { - val t = Test125() + val t = Test977() t.test() } \ No newline at end of file diff --git a/src/Test88.kt b/src/Test88.kt new file mode 100644 index 0000000..32431ef --- /dev/null +++ b/src/Test88.kt @@ -0,0 +1,41 @@ +class Test88 { + class Solution { + fun merge(nums1: IntArray, m: Int, nums2: IntArray, n: Int): Unit { + if (m == 0){ + nums2.fold(0){ acc, i -> + nums1[acc] = i + acc +1 + } + return + } + + var mp = m-1 + var np = n-1 + + while (mp != -1 && np != -1){ + if (nums1[mp] >= nums2[np]){ + nums1[mp + np +1] = nums1[mp] + mp -- + }else{ + nums1[mp + np +1] = nums2[np] + np -- + } + } + while (np >= 0){ + nums1[np] = nums2[np] + np-- + } + + } + } + fun test(){ + val s = Solution() + val a11 = intArrayOf(1,2,3,0,0,0) + s.merge(a11,3, intArrayOf(4,5,6),3) + a11.forEach { print(it) }.also { println() } + + val a21 = intArrayOf(2,0) + s.merge(a21,1, intArrayOf(1),1) + a21.forEach { print(it) }.also { println() } + } +} \ No newline at end of file diff --git a/src/Test977.kt b/src/Test977.kt new file mode 100644 index 0000000..1e0266b --- /dev/null +++ b/src/Test977.kt @@ -0,0 +1,38 @@ +class Test977 { + class Solution { + fun sortedSquares(nums: IntArray): IntArray { + // -inf .. 0 <-l r-> 0.. +inf + var l = 0 + while (l < nums.size && nums[l] < 0) { + l++ + } + l -- + var r = l + 1 + var result = intArrayOf() + + while (l >= 0 && r < nums.size) { + if (-nums[l] <= nums[r]) { + result += nums[l] * nums[l] + l-- + } else { + result += nums[r] * nums[r] + r++ + } + } + while (r < nums.size) { + result += nums[r] * nums[r] + r++ + } + while (l >= 0) { + result += nums[l] * nums[l] + l-- + } + return result + } + } + fun test(){ + Solution().sortedSquares(intArrayOf(-4,-1,0,3,10)).forEach { print("$it,") }.also { println() } + Solution().sortedSquares(intArrayOf(-7,-3,2,3,11)).forEach { print("$it,") }.also { println() } + + } +} \ No newline at end of file