Strongly recommended support Scala language


  • 17
    M

    and here is the code for solve the single number, and using scala language is just only one line of code. With now, functional programming is very popular in big data area. So, I strongly recommended the leecode support the compiler compatible with scala.

     def singleNumber(nums:Array[Int]): Int = {
        nums.foldLeft(0)(_^_)
      }
    
      val arr = Array(1,2,2,3,3,4,5,6,6,5,4,1,9)
      println(singleNumber(arr))
    

    And another example can directly show more message , and just needs a tuple. I show you with this is wish to tell you that scala can support more message and expressive.

    def hummingWeight(n: Int): (Int, ArrayBuffer[Int]) = {
        val arrBuffer = ArrayBuffer[Int]()
        var alt_n = n
        var count = 0
        while (alt_n > 0) {
          if ((alt_n & 1) == 1){
            count+=1
            arrBuffer += 1
          } else {
            arrBuffer += 0
          }
          alt_n >>= 1
        }
        (count, arrBuffer.reverse)
      }
      println (hummingWeight(2147483647))
    }
    

    And the output like following

    (31,ArrayBuffer(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1))

  • 0
    A
    This post is deleted!

  • 0
    K

    Indeed, support for Scala or Haskell would be nice.


  • 0

    This will be valuable as functional programming languages becoming increasing popular in the job marketing.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.