整数转罗马数字
给定一个整数,将其转为罗马数字。输入确保在 1 到 3999 的范围内。
示例 :输入: 3,输出: “III”。
字符 数值:I 1,V 5,X 10,L 50,C 100,D 500,M 1000
class Solution {
public:string intToRoman(int num) {int values[]&#61;{1000,900,500,400,100,90,50,40,10,9,5,4,1};string reps[]&#61;{"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};string res;for(int i&#61;0; i<13; i&#43;&#43;){while(num>&#61;values[i]){num -&#61; values[i];res &#43;&#61; reps[i];}}return res;}
};
罗马数字转整数
给定一个罗马数字&#xff0c;将其转换成整数。输入确保在 1 到 3999 的范围内。
示例 : 输入: “III”,输出: 3
class Solution {
public:int romanToInt(string s) {int values[] &#61; { 1000,900,500,400,100,90,50,40,10,9,5,4,1 };string reps[] &#61; { "M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I" };int res&#61;0;int index &#61; 0;int i &#61; 0;while( i <13&&index};
整数转换英文表示
将非负整数转换为其对应的英文表示。可以保证给定输入小于 2^31 - 1 。
示例&#xff1a;输入: 12345, 输出: “Twelve Thousand Three Hundred Forty Five”。
class Solution {
public:vector keys &#61; {1000000000,1000000,1000,100,90,80,70,60,50,40,30,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0};vector values &#61; {"Billion","Million","Thousand","Hundred","Ninety","Eighty","Seventy","Sixty","Fifty","Forty","Thirty","Twenty","Nineteen","Eighteen","Seventeen","Sixteen","Fifteen","Fourteen","Thirteen","Twelve","Eleven","Ten","Nine","Eight","Seven","Six","Five","Four","Three","Two","One","Zero"};string numberToWords(int num) {for (int i &#61; 0; i <32; &#43;&#43;i) {int key &#61; keys[i];if (num >&#61; key) {if (num >&#61; 100) {return numberToWords(num / key) &#43; " " &#43; values[i] &#43; (num % key > 0 ? " " &#43; numberToWords(num % key) : "");} else if (num >&#61; 20) {return values[i] &#43; (num % 10 > 0 ? " " &#43; numberToWords(num % 10) : "");} else {return values[i];}}}return "";}
};
字符串相乘
给定两个以字符串形式表示的非负整数 num1 和 num2&#xff0c;返回 num1 和 num2 的乘积&#xff0c;它们的乘积也表示为字符串形式。
示例 1: 输入: num1 &#61; “2”, num2 &#61; “3”. 输出: “6”
class Solution {
public:string multiply(string num1, string num2) {if (num1 &#61;&#61; "0" || num2 &#61;&#61; "0") return "0";int n1 &#61; num1.length() - 1;int n2 &#61; num2.length() - 1;vector<int> mul(n1 &#43; n2 &#43; 2);
for (int i &#61; n1; i >&#61; 0; i--)for (int j &#61; n2; j >&#61; 0; j--) { int bitmul &#61; (num1[i] - &#39;0&#39;)*(num2[j] - &#39;0&#39;);bitmul &#43;&#61; mul[i &#43; j &#43; 1];mul[i &#43; j] &#43;&#61; bitmul / 10;mul[i &#43; j &#43; 1] &#61; bitmul % 10;}int i &#61; 0;while (i < n1 &#43; n2 &#43; 1 && mul[i] &#61;&#61; 0)i&#43;&#43;;int index &#61; i;string multi&#61;string(n1&#43;n2&#43;2-i,&#39;0&#39;);for (; i < n1 &#43; n2 &#43; 2; i&#43;&#43;)multi[i-index]&#61;(mul[i]&#43;&#39;0&#39;);return multi;}
};
把字符串转换成整数
思路
1.功能测试
正数/复数/0
2.边界值测试
最大的正整数/最小的负整数&#xff08;数据上下溢出&#xff09;
class Solution {
public:enum {kValid,kInvalid}; int g_nStatus &#61; kValid; int StrToInt(string str){g_nStatus &#61; kInvalid; long long num &#61; 0; const char* cstr &#61; str.c_str();if( (cstr !&#61; NULL) && (*cstr !&#61; &#39;\0&#39;) ){int minus &#61; 1;if(*cstr &#61;&#61; &#39;-&#39;){minus &#61; -1;cstr&#43;&#43;;}else if(*cstr &#61;&#61; &#39;&#43;&#39;){minus &#61; 1;cstr&#43;&#43;;}while(*cstr !&#61; &#39;\0&#39;){if(*cstr > &#39;0&#39; && *cstr < &#39;9&#39;){g_nStatus &#61; kValid; num &#61; num*10 &#43; (*cstr -&#39;0&#39;); cstr&#43;&#43;;if( ((minus>0) && (num > 0x7FFFFFFF)) ||((minus<0) && (num > 0x80000000)) ){g_nStatus &#61; kInvalid; num &#61; 0;break;}}else{g_nStatus &#61; kInvalid;num &#61; 0;break;}}if(g_nStatus &#61;&#61; kValid)num &#61; num * minus;}cout<<(int)num<<endl;return (int)num;}
};