热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

LeetCode整数与字符的转换问题

整数转罗马数字给定一个整数,将其转为罗马数字。输入确保在1到3999的范围内。示例:输入:3,输出:“III”。字符数值:I1ÿ

整数转罗马数字

给定一个整数,将其转为罗马数字。输入确保在 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);
//编译器默认初始化为0 n位数和m位数相乘最多为m&#43;n位数。
//乘数 num1 位数为 MM&#xff0c;被乘数 num2 位数为 NN&#xff0c; num1 x num2 结果 res 最大总位数为 M&#43;N
//num1[i] x num2[j] 的结果为 tmp(位数为两位&#xff0c;"0x","xy"的形式)&#xff0c;其第一位位于 res[i&#43;j]&#xff0c;第二位位于 res[i&#43;j&#43;1]。
//11*12 &#61;132 1和2相乘的结果为2的下标加1的下标&#xff0c;假设1的下标代表2乘了几个10&#xff08;0下标开始的话&#xff09;for (int i &#61; n1; i >&#61; 0; i--)for (int j &#61; n2; j >&#61; 0; j--) { //11 12int bitmul &#61; (num1[i] - &#39;0&#39;)*(num2[j] - &#39;0&#39;);//两个位上的数的乘积bitmul &#43;&#61; mul[i &#43; j &#43; 1];//先加低位&#xff0c;判断是否有新的进位mul[i &#43; j] &#43;&#61; bitmul / 10;//高位mul[i &#43; j &#43; 1] &#61; bitmul % 10;//低位}//去掉前导0int 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;);//string类型的append函数return multi;}
};



把字符串转换成整数

思路

1.功能测试

正数/复数/0

2.边界值测试

最大的正整数/最小的负整数&#xff08;数据上下溢出&#xff09;

class Solution {
public:/*全局变量*/enum {kValid,kInvalid}; // 枚举元素&#xff08;kValid&#61;0&#xff0c;kInvalid&#61;1&#xff09;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;){// string转int类型g_nStatus &#61; kValid; // 标记为合法输入num &#61; num*10 &#43; (*cstr -&#39;0&#39;); // string转换为int类型cstr&#43;&#43;;// 数据上下溢出if( ((minus>0) && (num > 0x7FFFFFFF)) ||((minus<0) && (num > 0x80000000)) ){g_nStatus &#61; kInvalid; // 如果溢出&#xff0c;则标记为非法输入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;}
};


推荐阅读
author-avatar
aarongwang56_972
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有