Just for fun, it contains the least static part of the program.


  • 0
    public String intToRoman(int num) {
            char[] map = new char[]{'I','V','X','L','C','D','M'};
            int[] divisor = new int[]{2,5};
            int romanNum = 1000;
            int index = 6;
            StringBuilder sb = new StringBuilder();
            while(index>=0){
                int result = num/romanNum;
                int remain = romanNum - num%romanNum;
                for(int i=0;i<result;i++) {
                    sb.append(map[index]);
                    num -= romanNum;
                }
                if(remain<=romanNum/(10/(index%2+1))) {
                    sb.append(map[index-((index+1)%2+1)]);
                    sb.append(map[index]);
                    num -= romanNum;
                    num += romanNum/(10/(index%2+1));
                }
                romanNum /= divisor[index%2];
                index--;
            }
            return sb.toString();
        }
    

Log in to reply
 

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