-
Notifications
You must be signed in to change notification settings - Fork 0
Closed
Description
๐ฌย ๋ฌธ์
https://app.codility.com/programmers/lessons/1-iterations/binary_gap/
๐ฌย Idea
- N์ ์ด์ง์๋ก ๋ณํํ ๋ค 1์ด ๋์ค๋ฉด temp์ ์ ์ฅ๋ 0์ ์นด์ดํธ๋ฅผ maxConut์ ๋น๊ตํ์ฌ ์ต๋๊ฐ์ ๊ณ์ ์ฐพ์๋๊ฐ๋ค
๐ฌย ํ์ด
public func solution(N : Int) -> Int {
let binaryN = String(N, radix: 2)
var maxCnt = 0
var temp = 0
if !binaryN.contains("1") { return 0 }
for i in binaryN {
if i == "1" {
maxCnt = max(maxCnt, temp)
temp = 0
} else {
temp += 1
}
}
return maxCnt
}
- ์ฝ๋๋ฆฌํฐ๋ ์ด๋ฉด์ ํ ์คํธ์ผ์ด์ค๋ฅผ ์ฐพ๋๊ฒ ํต์ฌ์ด๋ค.!!!! ํ ์คํธ์ผ์ด์ค ์ฐพ๋ ์ฐ์ตํ ๊ฒ!