From bd736dff34f0d2c51018b459388a0eb6b496fa6f Mon Sep 17 00:00:00 2001 From: Kagura Date: Wed, 6 Nov 2024 12:43:21 +0800 Subject: [PATCH] ? --- src/Test135.kt | 39 +++++++++++++++++++++++++++++++++++++++ src/Test205.kt | 23 +++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 src/Test135.kt create mode 100644 src/Test205.kt diff --git a/src/Test135.kt b/src/Test135.kt new file mode 100644 index 0000000..7034410 --- /dev/null +++ b/src/Test135.kt @@ -0,0 +1,39 @@ +class Test135 { + class Solution { + fun candy(ratings: IntArray): Int { + if (ratings.size == 1){ + return 1 + } + + val results = Array(ratings.size){1} + + // 正着算 + if (ratings[0] > ratings[1]){ + results[0] = 2 + } + + for (i in 1..ratings.lastIndex){ + if (ratings[i] > ratings[i-1] && results[i] <= results[i-1]){ + results[i] = results[i-1] + 1 + } + } + + // 反着修 + for (i in 0..= results[j-1] ){ + results[j-1] = results[j] + 1 + } + } + +// results.forEach { print(it) } +// println() + return results.sum() + } + } + fun test(){ + println(Solution().candy(intArrayOf(1,2,87,87,87,2,1))) + println(Solution().candy(intArrayOf(1,0,2))) + println(Solution().candy(intArrayOf(1,2,2))) + } +} \ No newline at end of file diff --git a/src/Test205.kt b/src/Test205.kt new file mode 100644 index 0000000..cc1a5d3 --- /dev/null +++ b/src/Test205.kt @@ -0,0 +1,23 @@ +class Test205 { + class Solution { + fun isIsomorphic(s: String, t: String): Boolean { + val f = mutableMapOf() + val fp = mutableMapOf() + for (i in s.indices){ + if (f.containsKey(s[i]) || fp.containsKey(t[i])){ + if (t[i] != f[s[i]] || s[i] != fp[t[i]]){ + return false + } + }else{ + f[s[i]] = t[i] + fp[t[i]] = s[i] + } + } + return true + } + } + fun test(){ + println(Solution().isIsomorphic("badc","baba")) + + } +} \ No newline at end of file