10ms and short java solution


  • 0
    W
    public class Solution {
        public String intToRoman(int num) {
            int[] w = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
            String[] s = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
            StringBuilder sb =new StringBuilder();
            for (int i = 0; i < w.length; i++) {
                while (num >= w[i]) {
                    sb.append(s[i]);
                    num -= w[i];
                }
                if (num == 0) {
                    break;
                }
            }
            return sb.toString();
        }
    }
    

Log in to reply
 

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