LeetCode 12 New Beginning

The career of OI is over long ago, and ACM is also an anticlimactic. Now he is still in graduate school, and there is no problem with studying and living. Now he is looking for a job.

The trigger to start blogging again was an email from csdn, and I was surprised that there were still people reading what I wrote when I was young, and even asking questions. Here I have to say sorry to everyone who may be beginners or OI gods or successful workers, etc., because I can't understand it myself and can't remember it = =.

For looking for a job, I still decide to prepare early and take time to do the questions slowly. CF can't be beat, but the level of LeetCode is still quite low, you can eat it with confidence, here is a step by step to brush it slowly. Writing something is also a process of learning.

I also know that many big guys on the Internet have already finished brushing LeetCode (after all, bzoj was almost completely brushed back then), but if you are willing to take a look here or have some discussions, I am really grateful.

The nonsense session is over.

Topic:

Interger to Roman.

Literally, the input range is [1,3999]. The simple way is to do it bit by bit and write a lot of special judgments.

In terms of scalability, it can be found that every change from 1 to 9 actually follows the same rule, then consider using an array to record the 1 and 5 of the corresponding number of digits (one hundred thousand...) , that is, the c1[] and c5[] arrays in the code, the code can be reused, and the same subscript and the same function can be used to output each bit. Although it is still a special sentence of 1-9, it is ugly. The most special should be 9.

Summarize:

For the water question, if you want to save the complexity of the code, you can use your brain a little bit, and then the total time will be faster than writing ifelse.

Code:

class Solution {
public:
    char c1[10] = {'I', 'X', 'C', 'M'};
    char c5[10] = {'V', 'L', 'D'};
    string ans = "";
    void put(char c, int times = 1){
        while(times--){
            ans += c;
        }
    }#output
    void check(int number, int pos){#pos means the position of 'number', then we can use array c1[] and c5[]
        if (number == 1)
            put(c1[pos]);
        if (number == 2)
            put(c1[pos], 2);
        if (number == 3)
            put(c1[pos], 3);
        if (number == 4){
            put(c1[pos]);
            put(c5[pos]);
        }
        if (number == 5)
            put(c5[pos]);
        if (number == 6){
            put(c5[pos]);
            put(c1[pos]);
        }
        if (number == 7){
            put(c5[pos]);
            put(c1[pos], 2);
        }
        if (number == 8){
            put(c5[pos]);
            put(c1[pos], 3);
        }
        if (number == 9){
            put(c1[pos]);
            put(c1[pos+1]);
        }
            
    }
    string intToRoman(int num) {
        int s[10];
        int cnt = 0;
        while(num){
            s[cnt++] = num % 10;
            num /= 10;
        }
        while(cnt--){
            check(s[cnt], cnt);
        }
        return ans;
    }
};

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326383531&siteId=291194637