LoginSignup
0
0

More than 5 years have passed since last update.

CodilityのTrainingを解いてみた その3

Posted at

問題 CyclicRotation

  • 使用言語:Swift3

感想

最初は下記に示したソースコードより長くて、非効率なプログラムを書いてしまった。
しかしそれだと計算時間がかかり100点が取れなかった。
排他的論理和を使うことで、効率的、かつ、速度の早い実装ができるのだなとしみじみ思った。

ソースコード

public func solution2_3(_ A : inout [Int]) -> Int {

        if A.count == 1 {
            return A[0]
        }

        var val = 0
        for a in A {
            //http://hajihaji-lemon.com/smartphone/swift/%E6%BC%94%E7%AE%97%E5%AD%90%E3%81%AE%E4%BD%BF%E3%81%84%E6%96%B9/
            val ^= a  // 左辺と右辺の排他的論理和(XOR)の結果を左辺の変数に代入する。
        }
        return val

    }
0
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
0
0