23. 数组中超过一半的数字

RT

来个Scala版:

  // Time: O(n), Space: O(1)
  def getMajority(nums: Iterable[Int]): Int = {
    require(nums.nonEmpty)
    val (result, _) = nums.tail.foldLeft((nums.head, 1)) {
      case ((_, 0), cur)                     => (cur, 1)
      case ((num, count), cur) if num == cur => (num, count + 1)
      case ((num, count), _)                 => (num, count - 1)
    }
    result
  }