๋ฐ์ํ
import Foundation
func solution(_ n:Int) -> Int {
var answer : Int = n + 1
while true {
if n.nonzeroBitCount == answer.nonzeroBitCount {
break;
}
answer += 1
}
return answer
}
n.nonzeroBitcount : ๋นํธ์์ 1์๊ฐ์ ์ธ์ค๋ค.
import Foundation
func solution(_ n:Int) -> Int
{
let countOne = countBinaryOne(n);
for num in n+1...Int.max {
if countBinaryOne(num) == countOne {
return num;
}
}
return n
}
func countBinaryOne(_ n:Int) -> Int {
return String(n, radix:2).map({Int(String($0))!}).reduce(0,+);
}
String(n, radix: 2) : 2์ง๋ฒ์ผ๋ก ๋ฐ๊ฟ์ค๋ค. ex) 10111
๋ฐ์ํ
'๐ ์ฝํ > ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Swift] ํ๋ก๊ทธ๋๋จธ์ค60061 ๊ธฐ๋ฅ๊ณผ ๋ณด ์ค์น (1) | 2023.05.13 |
---|---|
[Swift] ํ๋ก๊ทธ๋๋จธ์ค42862 ์ฒด์ก๋ณต (0) | 2023.05.13 |
[Swift] ํ๋ก๊ทธ๋๋จธ์ค154539 ๋ค์ ์๋ ํฐ ์ ์ฐพ๊ธฐ (0) | 2023.05.13 |
[Swift] ํ๋ก๊ทธ๋๋จธ์ค181188 ์๊ฒฉ์์คํ (0) | 2023.05.12 |
[Swift] ํ๋ก๊ทธ๋๋จธ์ค92342 ์๊ถ๋ํ (0) | 2023.05.11 |