This commit is contained in:
kagura 2024-09-09 12:17:41 +08:00
commit bd07ab1e88
10 changed files with 385 additions and 1 deletions

View file

@ -1,4 +1,4 @@
fun main() {
val t = Test14()
val t = Test977()
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() }
}
}

47
src/Test27.kt Normal file
View file

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

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

41
src/Test88.kt Normal file
View file

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

38
src/Test977.kt Normal file
View file

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