From af11a3f63f00cabbdc6129f64b70164aaf1edcd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BF=9E=E6=80=9D=E7=9C=9F?= Date: Mon, 13 Nov 2023 14:19:06 +0800 Subject: [PATCH] feat: support kotlin --- .../com/ysz/dm/lib/leecode/LeeCode1233.kt | 49 ------------- .../com/ysz/dm/lib/leecode/LeeCode155.kt | 52 -------------- .../com/ysz/dm/lib/leecode/LeeCode1663.kt | 40 ----------- .../com/ysz/dm/lib/leecode/LeeCode1664.kt | 69 ------------------- .../com/ysz/dm/lib/leecode/LeeCode1798.kt | 30 -------- .../com/ysz/dm/lib/leecode/LeeCode206.kt | 26 ------- .../com/ysz/dm/lib/leecode/LeeCode2335.kt | 43 ------------ .../dm/soc/user/vshop/OrderCreateFacade.kt | 61 ++++++++++++++++ .../dm/soc/user/vshop/inventory/Inventory.kt | 21 ++++++ 9 files changed, 82 insertions(+), 309 deletions(-) delete mode 100644 dm-kt/dm-lib/src/test/kotlin/com/ysz/dm/lib/leecode/LeeCode1233.kt delete mode 100644 dm-kt/dm-lib/src/test/kotlin/com/ysz/dm/lib/leecode/LeeCode155.kt delete mode 100644 dm-kt/dm-lib/src/test/kotlin/com/ysz/dm/lib/leecode/LeeCode1663.kt delete mode 100644 dm-kt/dm-lib/src/test/kotlin/com/ysz/dm/lib/leecode/LeeCode1664.kt delete mode 100644 dm-kt/dm-lib/src/test/kotlin/com/ysz/dm/lib/leecode/LeeCode1798.kt delete mode 100644 dm-kt/dm-lib/src/test/kotlin/com/ysz/dm/lib/leecode/LeeCode206.kt delete mode 100644 dm-kt/dm-lib/src/test/kotlin/com/ysz/dm/lib/leecode/LeeCode2335.kt create mode 100644 dm-kt/dm-soc/user/src/main/kotlin/com/ysz/dm/soc/user/vshop/OrderCreateFacade.kt create mode 100644 dm-kt/dm-soc/user/src/main/kotlin/com/ysz/dm/soc/user/vshop/inventory/Inventory.kt diff --git a/dm-kt/dm-lib/src/test/kotlin/com/ysz/dm/lib/leecode/LeeCode1233.kt b/dm-kt/dm-lib/src/test/kotlin/com/ysz/dm/lib/leecode/LeeCode1233.kt deleted file mode 100644 index 7852813..0000000 --- a/dm-kt/dm-lib/src/test/kotlin/com/ysz/dm/lib/leecode/LeeCode1233.kt +++ /dev/null @@ -1,49 +0,0 @@ -package com.ysz.dm.lib.leecode - -import java.util.LinkedList - - -/** - *
- * https://leetcode.cn/problems/remove-sub-folders-from-the-filesystem/
- * class desc here
- *
- * 这个代码不合适. 可以优化为 和 前 1 个比较, 属于脑筋急转弯. 看排序后的结果
- *
- *@author carl.yu - *@since 2023/2/8 - **/ -class LeeCode1233 {} - -internal object Solution { - - fun removeSubfolders(folder: Array): List { - - val sorted = folder.sorted() - val result = ArrayList() - result.add(sorted[0]) - - var current: String - var prev: String = sorted[0] - for (i in 1 until sorted.size) { - current = sorted[i] - if (!current.startsWith(prev + "/")) { - result.add(current) - prev = current - } - } - - - return result - } -} - -fun main(args: Array) { - println( - Solution.removeSubfolders( - arrayOf( - "/a/b/c", "/a/b/ca", "/a/b/d" - ) - ) - ) -} \ No newline at end of file diff --git a/dm-kt/dm-lib/src/test/kotlin/com/ysz/dm/lib/leecode/LeeCode155.kt b/dm-kt/dm-lib/src/test/kotlin/com/ysz/dm/lib/leecode/LeeCode155.kt deleted file mode 100644 index 9025485..0000000 --- a/dm-kt/dm-lib/src/test/kotlin/com/ysz/dm/lib/leecode/LeeCode155.kt +++ /dev/null @@ -1,52 +0,0 @@ -package com.ysz.dm.lib.leecode - - -/** - *
- * class desc here
- *
- *@author carl.yu - *@since 2023/2/7 - **/ -class LeeCode155 - -class MinStack( -) { - - private val stack: java.util.LinkedList = java.util.LinkedList() - private var min: Int? = null - - fun push(`val`: Int) { - stack.add(`val`) - min = if (min == null) { - `val` - } else { - kotlin.math.min(`val`, min!!) - } - } - - fun pop() { - val removed = stack.removeLast() - if (removed == min!!) { - this.min = this.stack.min() - } - } - - fun top(): Int { - return stack.last() - } - - fun getMin(): Int { - return min!! - } -} - -fun main(args: Array) { - - val stack = MinStack() - - stack.push(-2) - stack.push(-3) - stack.pop() - println(stack.getMin()) -} diff --git a/dm-kt/dm-lib/src/test/kotlin/com/ysz/dm/lib/leecode/LeeCode1663.kt b/dm-kt/dm-lib/src/test/kotlin/com/ysz/dm/lib/leecode/LeeCode1663.kt deleted file mode 100644 index 8a6c20d..0000000 --- a/dm-kt/dm-lib/src/test/kotlin/com/ysz/dm/lib/leecode/LeeCode1663.kt +++ /dev/null @@ -1,40 +0,0 @@ -package com.ysz.dm.lib.leecode - -import com.ysz.dm.lib.leecode.LeeCode1663.Companion.codeAsChar -import com.ysz.dm.lib.leecode.LeeCode1663.Companion.getSmallestString - -/** - * https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/ - * @author carl - * @since 2023-01-26 11:18 PM - **/ -class LeeCode1663 { - companion object { - fun codeAsChar(code: Int) = ('a' + code - 1) - - fun getSmallestString(n: Int, k: Int): String { - val res = StringBuilder() - var total = k - - for (i in 1..n) { - var diff = total - (n - i) * 26 - if (diff <= 1) { - diff = 1 - } - - total -= diff - res.append( - codeAsChar(diff) - ) - } - return res.toString() - } - - } -} - -fun main(args: Array) { - println(codeAsChar(19)) - println(getSmallestString(5, 73)) - -} \ No newline at end of file diff --git a/dm-kt/dm-lib/src/test/kotlin/com/ysz/dm/lib/leecode/LeeCode1664.kt b/dm-kt/dm-lib/src/test/kotlin/com/ysz/dm/lib/leecode/LeeCode1664.kt deleted file mode 100644 index 7dd5cab..0000000 --- a/dm-kt/dm-lib/src/test/kotlin/com/ysz/dm/lib/leecode/LeeCode1664.kt +++ /dev/null @@ -1,69 +0,0 @@ -package com.ysz.dm.lib.leecode - -/** - * @author carl - * @since 2023-01-28 12:47 AM - **/ -class LeeCode1664 { - companion object { - fun waysToMakeFair(nums: IntArray): Int { - var evenSum = 0 - var oddSum = 0 - nums.forEachIndexed { index, i -> - if (index % 2 == 0) { - evenSum += i - } else { - oddSum += i - } - } - println("origin: ${nums.contentToString()}") - println("evenSum:$evenSum, oddSum:$oddSum") - - - var res = 0 - - var oddNew: Int - var evenNew: Int - - var oddBefore = 0 - var evenBefore = 0 - - var oddAfter: Int - var evenAfter: Int - - - nums.forEachIndexed { index, i -> - oddAfter = oddSum - oddBefore - evenAfter = evenSum - evenBefore - if (index % 2 == 0) { - // 偶数 - evenNew = evenBefore + oddAfter - oddNew = oddBefore + evenAfter - i - - println("index:${index}, i:${i}, evenBefore: $evenBefore, evenAfter: $evenAfter , oddBefore:${oddBefore} , oddAfter: $oddAfter") - - evenBefore += i - } else { - // 奇数 - evenNew = evenBefore + oddAfter - i - oddNew = oddBefore + evenAfter - - println("index:${index}, i:${i}, evenBefore: $evenBefore, evenAfter: $evenAfter , oddBefore:${oddBefore} , oddAfter: $oddAfter") - oddBefore += i - } - - - - if (oddNew == evenNew) res++ - - } - - - return res - } - } -} - -fun main() { - LeeCode1664.waysToMakeFair(intArrayOf(2, 1, 6, 4)) -} \ No newline at end of file diff --git a/dm-kt/dm-lib/src/test/kotlin/com/ysz/dm/lib/leecode/LeeCode1798.kt b/dm-kt/dm-lib/src/test/kotlin/com/ysz/dm/lib/leecode/LeeCode1798.kt deleted file mode 100644 index f580c85..0000000 --- a/dm-kt/dm-lib/src/test/kotlin/com/ysz/dm/lib/leecode/LeeCode1798.kt +++ /dev/null @@ -1,30 +0,0 @@ -package com.ysz.dm.lib.leecode - -/** - * 对应题目为 https://leetcode.cn/problems/maximum-number-of-consecutive-values-you-can-make/ - * @author carl - * @since 2023-02-04 10:59 PM - **/ -object LeeCode1798 { - fun getMaximumConsecutive(coins: IntArray): Int { - val sorted = coins.sorted() - var count = 0; - - for (coin in sorted) { - val max = count + coin - - if (count + 1 in coin..max) { - count = max - } else { - break - } - } - return count + 1 - } - -} - - -fun main(args: Array) { - println(LeeCode1798.getMaximumConsecutive(intArrayOf(1, 1, 1, 4))) -} \ No newline at end of file diff --git a/dm-kt/dm-lib/src/test/kotlin/com/ysz/dm/lib/leecode/LeeCode206.kt b/dm-kt/dm-lib/src/test/kotlin/com/ysz/dm/lib/leecode/LeeCode206.kt deleted file mode 100644 index 97951cf..0000000 --- a/dm-kt/dm-lib/src/test/kotlin/com/ysz/dm/lib/leecode/LeeCode206.kt +++ /dev/null @@ -1,26 +0,0 @@ -package com.ysz.dm.lib.leecode - -/** - * @author carl - * @since 2023-02-09 6:28 PM - **/ -class LeeCode206 { - class ListNode(var `val`: Int) { - var next: ListNode? = null - } - - fun reverseList(head: ListNode?): ListNode? { - if (head == null) return null - else if (head.next == null) return head - else { - var slow = head - var fast = head.next!! - return fast - } - } -} - - -fun main(args: Array) { - -} \ No newline at end of file diff --git a/dm-kt/dm-lib/src/test/kotlin/com/ysz/dm/lib/leecode/LeeCode2335.kt b/dm-kt/dm-lib/src/test/kotlin/com/ysz/dm/lib/leecode/LeeCode2335.kt deleted file mode 100644 index a2d8365..0000000 --- a/dm-kt/dm-lib/src/test/kotlin/com/ysz/dm/lib/leecode/LeeCode2335.kt +++ /dev/null @@ -1,43 +0,0 @@ -package com.ysz.dm.lib.leecode - -/** - * keypoint: 让留下的最后1个值最小 . - * @author carl - * @since 2023-02-11 11:24 PM - **/ -class LeeCode2335 { - private fun maxIndex(amount: IntArray, excludeIndex: Int = -1): Int { - var max = -1 - var maxPos = -1 - amount.forEachIndexed { index, i -> - if (excludeIndex != index && i > max) { - max = i - maxPos = index - } - } - - return maxPos - } - - - fun fillCups(amount: IntArray): Int { - var steps = 0; - while (true) { - var maxIdx = maxIndex(amount) - if (amount[maxIdx] == 0) break - - amount[maxIdx] = amount[maxIdx] - 1 - steps++ - - maxIdx = maxIndex(amount, maxIdx) - if (amount[maxIdx] == 0) { - amount[maxIdx] = amount[maxIdx] - 1 - } - } - return steps - } -} - -fun main(args: Array) { - print(LeeCode2335().fillCups(intArrayOf(5, 4, 4))) -} \ No newline at end of file diff --git a/dm-kt/dm-soc/user/src/main/kotlin/com/ysz/dm/soc/user/vshop/OrderCreateFacade.kt b/dm-kt/dm-soc/user/src/main/kotlin/com/ysz/dm/soc/user/vshop/OrderCreateFacade.kt new file mode 100644 index 0000000..a098a2e --- /dev/null +++ b/dm-kt/dm-soc/user/src/main/kotlin/com/ysz/dm/soc/user/vshop/OrderCreateFacade.kt @@ -0,0 +1,61 @@ +package com.ysz.dm.soc.user.vshop + +data class OrderItemDto( + val inventoryId: Long, + val quantity: Int, + val price: String, + /*property version num, 乐观锁, 防止价格改变造成观感 不一致*/ + val version: Long +) + + +data class OrderCreateDto( + val userId: Long, + val orderItems: List, + /*可能支付的领域在其他的地方,但是也可以用*/ + val pspType: PaymentServiceProvider +) + +enum class PaymentServiceProvider +data class PrepareOrderResultDto( + val orderId: String, + val paymentOrderId: String, +) + +data class InventorySnapshot( + val inventoryId: Long, + val version: Long, + val createAt: Long, + val updateAt: Long, + val inventoryName: String, + val inventoryDesc: String?, + val sellPrices: String, + val normalPrices: String, +) + +interface InventoryService { + fun checkUserCanBuy(userId: Long, orderItems: List): Boolean +} + +interface PaymentService { +} + +class OrderCreateFacade( + private val inventoryService: InventoryService +) { + + fun orderCreate(cmd: OrderCreateDto) { + /*1. maybe*/ + inventoryService.checkUserCanBuy(cmd.userId, cmd.orderItems) + /*2. check user can user this payment type*/ + /*3. */ + /*4. Get Inventory Snapshot*/ + /*5. Prepare for OrderId, PaymentId */ + /*6. Sign For Payment and ...*/ + /*7. Create Order*/ + /*8. Create Payment*/ + /*9. Sync Finish */ + } + + +} \ No newline at end of file diff --git a/dm-kt/dm-soc/user/src/main/kotlin/com/ysz/dm/soc/user/vshop/inventory/Inventory.kt b/dm-kt/dm-soc/user/src/main/kotlin/com/ysz/dm/soc/user/vshop/inventory/Inventory.kt new file mode 100644 index 0000000..932d9c5 --- /dev/null +++ b/dm-kt/dm-soc/user/src/main/kotlin/com/ysz/dm/soc/user/vshop/inventory/Inventory.kt @@ -0,0 +1,21 @@ +package com.ysz.dm.soc.user.vshop.inventory + +data class VipProps( + val vipLevel: Int +) + +data class InventoryProps( + val vipProps: VipProps? +) + +data class Inventory( + val inventoryId: Long, + val version: Long, + val createAt: Long, + val updateAt: Long, + val inventoryName: String, + val inventoryDesc: String?, + val sellPrices: String, + val normalPrices: String, + val props: InventoryProps, +)