Skip to content

[Algorithm] BinaryGapย #128

@hwangJi-dev

Description

@hwangJi-dev

๐Ÿ’ฌย ๋ฌธ์ œ

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
}
  • ์ฝ”๋”œ๋ฆฌํ‹ฐ๋Š” ์ด๋ฉด์˜ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋ฅผ ์ฐพ๋Š”๊ฒŒ ํ•ต์‹ฌ์ด๋‹ค.!!!! ํ…Œ์ŠคํŠธ์ผ€์ด์Šค ์ฐพ๋Š” ์—ฐ์Šตํ• ๊ฒƒ!

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions