leetcode 12

image

算法介绍:

  • 哈希

  • 贪心

  • 实现代码如下

class Solution {
public:
    string intToRoman(int num) {
        // 哈希贪心
		string s[] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
		int v[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
		int i = 13;
		string res = "";
		for(int i = 0;i<13;i++){
			int temp = num / v[i];
			num -= temp * v[i];
			
			for(int j = 0;j < temp;j++){
				res += s[i];
			}
		}
		return res;
    }
};
posted @ 2023-08-23 22:06  MaoShine  阅读(9)  评论(0编辑  收藏  举报