From 9b3468e3b3e7ff844bc1c8d1a5bbb141072d5689 Mon Sep 17 00:00:00 2001 From: Ezralin <10881430+ezralin@user.noreply.gitee.com> Date: Fri, 8 Jul 2022 14:10:18 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A00034=20Kotlin=E7=89=88,02?= =?UTF-8?q?03=20Kotlin=E7=89=88,0209=20Koltin=E7=89=88,=200977=20Kotlin?= =?UTF-8?q?=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...元素的第一个和最后一个位置.md | 43 +++++++++++++++++++ problems/0203.移除链表元素.md | 34 +++++++++++++++ problems/0209.长度最小的子数组.md | 32 ++++++++++++++ problems/0977.有序数组的平方.md | 28 ++++++++++++ 4 files changed, 137 insertions(+) diff --git a/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md b/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md index a81b3641..6922c399 100644 --- a/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md +++ b/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md @@ -584,5 +584,48 @@ object Solution { ``` +### Kotlin +```kotlin +class Solution { + fun searchRange(nums: IntArray, target: Int): IntArray { + var index = binarySearch(nums, target) + // 没找到,返回[-1, -1] + if (index == -1) return intArrayOf(-1, -1) + var left = index + var right = index + // 寻找左边界 + while (left - 1 >=0 && nums[left - 1] == target){ + left-- + } + // 寻找右边界 + while (right + 1 target) { + right = middle - 1 + } + else { + if (nums[middle] < target) { + left = middle + 1 + } + else { + return middle + } + } + } + // 没找到,返回-1 + return -1 + } +} +``` + -----------------------
diff --git a/problems/0203.移除链表元素.md b/problems/0203.移除链表元素.md index b79d29a5..ea936093 100644 --- a/problems/0203.移除链表元素.md +++ b/problems/0203.移除链表元素.md @@ -532,5 +532,39 @@ object Solution { } } ``` +Kotlin: +```kotlin +/** + * Example: + * var li = ListNode(5) + * var v = li.`val` + * Definition for singly-linked list. + * class ListNode(var `val`: Int) { + * var next: ListNode? = null + * } + */ +class Solution { + fun removeElements(head: ListNode?, `val`: Int): ListNode? { + // 使用虚拟节点,令该节点指向head + var dummyNode = ListNode(-1) + dummyNode.next = head + // 使用cur遍历链表各个节点 + var cur = dummyNode + // 判断下个节点是否为空 + while (cur.next != null) { + // 符合条件,移除节点 + if (cur.next.`val` == `val`) { + cur.next = cur.next.next + } + // 不符合条件,遍历下一节点 + else { + cur = cur.next + } + } + // 注意:返回的不是虚拟节点 + return dummyNode.next + } +} +``` -----------------------
diff --git a/problems/0209.长度最小的子数组.md b/problems/0209.长度最小的子数组.md index 090e73f0..c1633def 100644 --- a/problems/0209.长度最小的子数组.md +++ b/problems/0209.长度最小的子数组.md @@ -417,6 +417,38 @@ class Solution { } } ``` +滑动窗口 +```kotlin +class Solution { + fun minSubArrayLen(target: Int, nums: IntArray): Int { + // 左边界 和 右边界 + var left: Int = 0 + var right: Int = 0 + // sum 用来记录和 + var sum: Int = 0 + // result记录一个固定值,便于判断是否存在的这样的数组 + var result: Int = Int.MAX_VALUE + // subLenth记录长度 + var subLength = Int.MAX_VALUE + + + while (right < nums.size) { + // 从数组首元素开始逐次求和 + sum += nums[right++] + // 判断 + while (sum >= target) { + var temp = right - left + // 每次和上一次比较求出最小数组长度 + subLength = if (subLength > temp) temp else subLength + // sum减少,左边界右移 + sum -= nums[left++] + } + } + // 如果subLength为初始值,则说明长度为0,否则返回subLength + return if(subLength == result) 0 else subLength + } +} +``` Scala: 滑动窗口: diff --git a/problems/0977.有序数组的平方.md b/problems/0977.有序数组的平方.md index 458107dd..d46b5259 100644 --- a/problems/0977.有序数组的平方.md +++ b/problems/0977.有序数组的平方.md @@ -362,6 +362,8 @@ class Solution { ``` Kotlin: + +双指针法 ```kotlin class Solution { // 双指针法 @@ -383,6 +385,32 @@ class Solution { } } ``` +骚操作(暴力思路) +```kotlin +class Solution { + fun sortedSquares(nums: IntArray): IntArray { + // left 与 right 用来控制循环,类似于滑动窗口 + var left: Int = 0; + var right: Int = nums.size - 1; + // 将每个数字的平方经过排序后加入result数值 + var result: IntArray = IntArray(nums.size); + var k: Int = nums.size - 1; + while (left <= right) { + // 从大到小,从后向前填满数组 + // [left, right] 控制循环 + if (nums[left] * nums[left] > nums[right] * nums[right]) { + result[k--] = nums[left] * nums[left] + left++ + } + else { + result[k--] = nums[right] * nums[right] + right-- + } + } + return result + } +} +``` Scala: From e0a422859904ddae865cc828bc30d6620ccb8137 Mon Sep 17 00:00:00 2001 From: Ezralin <10881430+ezralin@user.noreply.gitee.com> Date: Mon, 11 Jul 2022 15:09:41 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A00206.=E7=BF=BB=E8=BD=AC?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=20Kotlin=E8=A7=A3=E6=9E=90=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0206.翻转链表.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/problems/0206.翻转链表.md b/problems/0206.翻转链表.md index 24ec7b94..bb9b62d5 100644 --- a/problems/0206.翻转链表.md +++ b/problems/0206.翻转链表.md @@ -420,6 +420,41 @@ fun reverseList(head: ListNode?): ListNode? { return pre } ``` +```kotlin +/** + * Example: + * var li = ListNode(5) + * var v = li.`val` + * Definition for singly-linked list. + * class ListNode(var `val`: Int) { + * var next: ListNode? = null + * } + */ +class Solution { + fun reverseList(head: ListNode?): ListNode? { + // temp用来存储临时的节点 + var temp: ListNode? + // cur用来遍历链表 + var cur: ListNode? = head + // pre用来作为链表反转的工具 + // pre是比pre前一位的节点 + var pre: ListNode? = null + while (cur != null) { + // 临时存储原本cur的下一个节点 + temp = cur.next + // 使cur下一节点地址为它之前的 + cur.next = pre + // 之后随着cur的遍历移动pre + pre = cur; + // 移动cur遍历链表各个节点 + cur = temp; + } + // 由于开头使用pre为null,所以cur等于链表本身长度+1, + // 此时pre在cur前一位,所以此时pre为头节点 + return pre; + } +} +``` Swift: ```swift