Share my Ruby solution


  • 0
    R
    def count_and_say0(s)
      return '1' if s == ''
      front = nil; cnt = 0; res = ''
      len = s.length
      for i in (0...len)
        if s[i] == front
          cnt += 1
        else
          res << "#{cnt}#{s[i-1]}" if not front.nil?
          cnt = 1
          front = s[i]
        end
      end
      res << "#{cnt}#{s[len-1]}"
    end
    
    
    # @param {Integer} n
    # @return {String}
    def count_and_say(n)
      s = ''
      1.upto(n) do
        s = count_and_say0(s)
      end
      return s
    end

Log in to reply
 

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