Simple Ruby Solution


  • 2
    # @param {String} s
    # @return {Integer}
    def title_to_number(s)
        s = s.split('').map{|x| x.ord-64}
        ans = 0
        for x in 1..s.length
            ans += s[-x]*26**(x-1)
        end
        ans
    end

  • 0
    L

    even simpler:

    def title_to_number s
      s.unpack('C*').inject 0 do |sum, c|
        sum * 26 + c.ord - 64
      end
    end

Log in to reply
 

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