4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

codilityのテストを受けてみた

Posted at

Codilityというプログラミングテストをやってみた

Codilityとは

俗に言う競技プログラミングだったり、エンジニアのスキルをチェックするためのサービス。
訳あってCodilityのテストを受けることになったため、その練習のためにTraining問題を解いてみた。

問題 Binary Gap

  • 使用言語:Swift3

コード

public func solution1(_ N : Int) -> Int {
        if N <= 1 {
            return 0
        }
        
        var binaryArray = [Int]()
        var quo = N
        while quo >= 1 {
            binaryArray.append(quo % 2)
            quo = quo / 2
        }
        if quo == 1 {
            binaryArray.append(quo)
        }
        binaryArray = binaryArray.reversed()
        var gapArray = [0]
        var gap = 0
        for i in 1..<binaryArray.count {
            if binaryArray[i] == 0 {
                gap = gap + 1
            } else {
                gapArray.append(gap)
                gap = 0
            }
        }
        let descArray = gapArray.sorted { $0 > $1 }
        return descArray[0]
    }
4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?