This commit is contained in:
Kagura 2024-08-26 14:35:43 +08:00
parent beebc54755
commit dea7cf2f92
8 changed files with 259 additions and 9 deletions

View file

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

28
src/Test125.kt Normal file
View file

@ -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<r){
if (sb[l]!=sb[r]){
return false
}
l++
r--
}
return true
}
}
fun test(): Unit {
println(Solution().isPalindrome("A man, a plan, a canal: Panama"))
println(Solution().isPalindrome("race a car"))
}
}

82
src/Test19.kt Normal file
View file

@ -0,0 +1,82 @@
class Test19 {
class ListNode(var `val`: Int) {
var next: ListNode? = null
}
class Solution {
fun removeNthFromEnd(head: ListNode?, n: Int): ListNode? {
var slower = head
var faster = head
// faster move n steps
for (ignored in 0..<n){
faster = faster!!.next
}
// case of remove head
if (faster == null){
return head!!.next
}
// then move
while (faster?.next != null){
faster = faster.next
slower = slower!!.next
}
// delete
slower!!.next = when(slower.next){
null -> 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<Int>()
while (current != null) {
result.add(current.`val`)
current = current.next
}
println(result)
}
}

24
src/Test26.kt Normal file
View file

@ -0,0 +1,24 @@
class Test26 {
class Solution {
fun removeDuplicates(nums: IntArray): Int {
var unique = 0
for (i in 1..<nums.size){
if (nums[i]!=nums[unique]){
nums[++unique] = nums[i]
}
}
return unique+1
}
}
fun test(){
val arr1 = intArrayOf(1,1,2)
println(Solution().removeDuplicates(arr1))
arr1.forEach { print("${it},") }.run { println() }
val arr2 = intArrayOf(0,0,1,1,1,2,2,3,3,4)
println(Solution().removeDuplicates(arr2))
arr2.forEach { print("${it},") }.run { println() }
}
}

View file

@ -1,8 +0,0 @@
class Test46 {
class Solution {
fun permute(nums: IntArray): List<List<Int>> {
val result = arrayOf(intArrayOf())
for (i in)
}
}
}

47
src/Test48.kt Normal file
View file

@ -0,0 +1,47 @@
class Test48 {
class Solution {
fun rotate(matrix: Array<IntArray>): 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.size){
for (j in 0..i){
matrix[i][j] = matrix[j][i]
.also { matrix[j][i] = matrix[i][j] }
}
}
}
}
fun test() {
val solution = Solution()
// 测试用例 1
val matrix1 = arrayOf(
intArrayOf(1, 2, 3),
intArrayOf(4, 5, 6),
intArrayOf(7, 8, 9)
)
solution.rotate(matrix1)
printMatrix(matrix1) // 预期输出: [[7,4,1],[8,5,2],[9,6,3]]
// 测试用例 2
val matrix2 = arrayOf(
intArrayOf(5, 1, 9, 11),
intArrayOf(2, 4, 8, 10),
intArrayOf(13, 3, 6, 7),
intArrayOf(15, 14, 12, 16)
)
solution.rotate(matrix2)
printMatrix(matrix2) // 预期输出: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
}
// 辅助函数:打印二维矩阵
fun printMatrix(matrix: Array<IntArray>) {
matrix.forEach { row ->
println(row.joinToString(prefix = "[", postfix = "]", separator = ","))
}
}
}

54
src/Test54.kt Normal file
View file

@ -0,0 +1,54 @@
class Test54 {
class Solution {
fun spiralOrder(matrix: Array<IntArray>): List<Int> {
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))))
}
}

23
src/Test658.kt Normal file
View file

@ -0,0 +1,23 @@
import kotlin.math.abs
class Test658 {
class Solution {
fun findClosestElements(arr: IntArray, k: Int, x: Int): List<Int> {
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))
}
}