?
This commit is contained in:
parent
b22b0fa5e6
commit
bd736dff34
2 changed files with 62 additions and 0 deletions
39
src/Test135.kt
Normal file
39
src/Test135.kt
Normal file
|
@ -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..<ratings.lastIndex){
|
||||
val j = ratings.lastIndex - i
|
||||
if (ratings[j] < ratings[j-1] && results[j] >= 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)))
|
||||
}
|
||||
}
|
23
src/Test205.kt
Normal file
23
src/Test205.kt
Normal file
|
@ -0,0 +1,23 @@
|
|||
class Test205 {
|
||||
class Solution {
|
||||
fun isIsomorphic(s: String, t: String): Boolean {
|
||||
val f = mutableMapOf<Char,Char>()
|
||||
val fp = mutableMapOf<Char,Char>()
|
||||
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"))
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue