SW Test/Programmers

[Programmers] Lv2.์ด์ง„ ๋ณ€ํ™˜ ๋ฐ˜๋ณตํ•˜๊ธฐ(kotlin)

An effort will never betray ๐Ÿ˜Ž 2022. 9. 14. 01:56
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

https://school.programmers.co.kr/learn/courses/30/lessons/70129

 

 

 

์˜ˆ์‹œ

https://school.programmers.co.kr/learn/courses/30/lessons/70129

 

 

 

ํ’€์ด

  • ๋ฌธ์ž์—ด์ด "1"์ด ๋˜๊ธฐ์ „๊นŒ์ง€ ๋ฐ˜๋ณตํ•ด์„œ ์ด์ง„ํ™”ํ•˜๊ณ  ์ด์ง„ ๋ณ€ํ™˜ ํšŸ์ˆ˜์™€ ๋ฌธ์ž์—ด์˜ "0"์˜ ๊ฐฏ์ˆ˜๋ฅผ ํ•˜๋‚˜์”ฉ ๋”ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
  • ๋ฌธ์ž์—ด์˜ ์ˆ˜๋ฅผ ์ด์ง„๋ฒ•์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์€ Java์˜ Integer.toBinaryString(int) ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํŽธ๋ฆฌํ•˜๊ฒŒ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
class Solution {
    fun solution(s: String): IntArray {
        var str = s
        var trans = 0
        var cnt = 0
        
        
        while(str != "1"){
            trans++
            cnt += str.filter{ it == '0'}.length
            str = Integer.toBinaryString(str.filter{it == '1'}.length)
        }
        
        return intArrayOf(trans, cnt)
    }
}

 

 

 

์ฐธ๊ณ 

 

filter - Kotlin Programming Language

 

kotlinlang.org

  • https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#toBinaryString(int)
๋ฐ˜์‘ํ˜•