[프로그래머스] 이진 변환 반복하기(Kotlin)
풀이
class Solution {
fun removeZero(str:String):Pair<String, Int>{
var newStr = ""
var zeroCnt = 0
for(i in str){
if(i == '0') zeroCnt++
else newStr += i
}
return Pair(newStr, zeroCnt)
}
fun binaryString(num:Int):String{
var binary = ""
var n = num
while(n != 1){
binary += (n%2).toString()
n /= 2
}
binary += "1"
return binary.reversed()
}
fun solution(s: String): IntArray {
var str = s
var convertCnt = 0
var totalZeroCnt = 0
while(str != "1"){
convertCnt++
var(newStr, zeroCnt) = removeZero(str)
str = newStr
totalZeroCnt += zeroCnt
str = binaryString(str.length)
}
var answer: IntArray = intArrayOf(convertCnt, totalZeroCnt)
return answer
}
}