728x90
문자 개수 세기
class Solution {
fun solution(my_string: String): IntArray {
var answer: IntArray = intArrayOf(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
my_string.forEach {
if (it.isLowerCase())
answer[it - 'A' - 6]++
else
answer[it - 'A']++
}
return answer
}
}
배열 만들기 1
class Solution {
fun solution(n: Int, k: Int): IntArray = (k .. n step k).toList().toIntArray()
}
글자 지우기
class Solution {
fun solution(my_string: String, indices: IntArray): String {
var answer: String = my_string
indices.sorted().reversed().forEach {
answer = answer.slice(0 until it) + answer.slice(it + 1 until answer.length)
}
return answer.filter { it != '1' }
}
}
카운트 다운
class Solution {
fun solution(start: Int, end: Int): IntArray = (end .. start).toList().reversed().toIntArray()
}
가까운 1 찾기
class Solution {
fun solution(arr: IntArray, idx: Int): Int {
val result = arr.slice(idx until arr.size).indexOf(1)
return if (result != -1) result + idx else result
}
}
728x90