golang coding


  • 0
    func intToRoman(num int) string {
        
        th :=[]string{"","M","MM","MMM"}
        hd := []string{"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"}
        ten := []string{"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"}
        ones := []string{"","I","II","III","IV","V","VI","VII","VIII","IX"}
    
        n1 := num/1000
        n2 := (num - n1*1000)/100
        n3 := (num - n1*1000-n2*100)/10
        n4 := num - n1*1000-n2*100-n3*10
        
        var res string
        
        if n1 >0 {
            res = res + th[n1]
        }
      
          if n2 >0 {
            res = res + hd[n2]
        }
        
            if n3 >0 {
            res = res + ten[n3]
        }
            if n4 >0 {
            res = res + ones[n4]
        }
        return res
    }
    

Log in to reply
 

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