Leetcode 题目整理-3 Palindrome Number & Roman to Integer
9. Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.
Some hints:
Could negative integers be palindromes? (ie, -1)
If you are thinking of converting the integer to string, note the restriction of using extra space.
You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?
There is a more generic way of solving this problem.
判断一个数是否为回文数。
百度了一下回文数是指诸如1234321 或者3223 这样的数,0,1,2,…,9也算;
这里的第一个注解中提到的负数不知道算不算,先不考虑,如果算的话会在提交后给出错误;
题目中还有一个要求就是不能使用额外的空间,据注释讲不能用转换成字符串的方法,字符串的存储会使用额外的空间;
注解中还提醒了如果用reverse integer的方法,要注意基本变量类型的范围是否溢出。
解:实践证明这里定义的回文数并不包括负数
//计算有几位
bool flag{ };
if (x < )
{
flag = ;
return flag;
}
int n = ;
int t_x = x;
while (t_x)
{
t_x = t_x / ;
n++;
}
//判断首位和末位是否相等,不等令flag=0跳出
while (n>)
{
int t = pow(, n - );//最高位
if ((x % ) == (x / t))
{
x = x % t;
x = x / ;
}
else
{
flag = ;
break;
}
n = n - ;
}
return flag;
13. Roman to Integer
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
注:把罗马数字转换成整数,输入限定在1~3999上。那么问题来了,罗马数字啥样?是怎么组数的?
涨知识,一下是罗马数字的一些解释也不知道对不对(百度百科):

- 相同的数字连写、所表示的数等于这些数字相加得到的数、如:Ⅲ=3;
- 小的数字在大的数字的右边、所表示的数等于这些数字相加得到的数、如:Ⅷ=8、Ⅻ=12;
- 小的数字、(限于Ⅰ、X 和 C)在大的数字的左边、所表示的数等于大数减小数得到的数、如:Ⅳ=4、Ⅸ=9;
- 正常使用时、连写的数字重复不得超过三次;
- 在一个数的上面画一条横线、表示这个数扩大 1000 倍。
有几条须注意掌握:
- 基本数字Ⅰ、X 、C 中的任何一个、自身连用构成数目、或者放在大数的右边连用构成数目、都不能超过三个;放在大数的左边只能用一个;
- 不能把基本数字 V 、L 、D 中的任何一个作为小数放在大数的左边采用相减的方法构成数目;放在大数的右边采用相加的方式构成数目、只能使用一个;
- I只能用在V和X左边;
- X只能用在L和C左边;
- C只能用在D和M左边
个位数举例
Ⅰ-1、Ⅱ-2、Ⅲ-3、Ⅳ-4、Ⅴ-5、Ⅵ-6、Ⅶ-7、Ⅷ-8、Ⅸ-9
·十位数举例
Ⅹ-10、Ⅺ-11、Ⅻ-12、XIII-13、XIV-14、XV-15、XVI-16、XVII-17、XVIII-18、XIX-19、XX-20、XXI-21、XXII-22、XXIX-29、XXX-30、XXXIV-34、XXXV-35、XXXIX-39、XL-40、L-50、LI-51、LV-55、LX-60、LXV-65、LXXX-80、XC-90、XCIII-93、XCV-95、XCVIII-98、XCIX-99
·百位数举例
C-100、CC-200、CCC-300、CD-400、D-500、DC-600、DCC-700、DCCC-800、CM-900、CMXCIX-999
·千位数举例
M-1000、MC-1100、MCD-1400、MD-1500、MDC-1600、MDCLXVI-1666、MDCCCLXXXVIII-1888、MDCCCXCIX-1899、MCM-1900、MCMLXXVI-1976、MCMLXXXIV-1984、MCMXC-1990、MM-2000、MMMCMXCIX-3999
·千位数以上举例
-65,259、
-134,945,584、
-183,650
注:看来罗马数字的转换要注意一下对应的数所在的位置,小数值的在前是减法,小数值在后的是加法,从字符串读取的同时要记录排序以便计算;
另外,超过3999就要加横杠处理了,怪不得题目中给的是1-3999呢。
解:用map组织数据,然后把第一个字母单列出来,剩下的通过后者与前者的大小关系判断是加上去还是减掉。
代码如下:
//构造个查找表
map<char, int> M;
int trans_ans{ };
M['I'] = ; M['V'] = ; M['X'] = ; M['L'] = ; M['C'] = ; M['D'] = ; M['M'] = ;
//依次读取每个字符
if (s.size() == )
return trans_ans;
trans_ans = M[s.front()];
for (string::iterator s_i = s.begin()+; s_i != s.end(); s_i++)
{
if (M[*s_i] > M[*(s_i - )])
{
trans_ans = trans_ans - * M[*(s_i - )] + M[*s_i];
}
else
{
trans_ans = trans_ans + M[*s_i];
}
}
return trans_ans;
Leetcode 题目整理-3 Palindrome Number & Roman to Integer的更多相关文章
- leetCode练题——9. Palindrome Number
1.题目 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome ...
- leetcode之旅(10)-Roman to Integer
题目描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...
- LeetCode记录之9——Palindrome Number
LeetCode真是个好东西,本来闲了一下午不想看书,感觉太荒废时间了就来刷一道题.能力有限,先把easy的题目给刷完. Determine whether an integer is a palin ...
- 【LeetCode】9、Palindrome Number(回文数)
题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...
- 【LeetCode算法-9】Palindrome Number
LeetCode第9题 Determine whether an integer is a palindrome. An integer is a palindrome when it reads t ...
- LeetCode(9)Palindrome Number
题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...
- Leetcode 题目整理 climbing stairs
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- Leetcode 题目整理-1
1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...
- Leetcode 题目整理-2 Reverse Integer && String to Integer
今天的两道题关于基本数据类型的探讨,估计也是要考虑各种情况,要细致学习 7. Reverse Integer Reverse digits of an integer. Example1: x = 1 ...
随机推荐
- The second day of Crawler learning
用BeatuifulSoup和Requests爬取猫途鹰网 服务器与本地的交换机制 我们每次浏览网页都是再向网页所在的服务器发送一个Request,然后服务器接受到Request后返回Response ...
- 泛型的运用(用于查询数据后DataTable转实体类)
2019.8.14 更新 补全了DataTable转泛型集合的方法: /// <summary> /// DataTable转实体类集合 /// </summary> /// ...
- 对sql server查询速度的优化
处理百万级以上的数据提高查询速度的方法: 1.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索引而进行全表扫描. 2.对查询进行优化,应尽量避免全表扫描,首先应考 ...
- jenkins安装自动部署
1.1简介: 开源项目,一个可扩展的持续集成引擎.提供了一种易于使用的持续集成系统,能实施监控集成中存在的错误,提供详细的日志文件和提醒功能,还能用图表的形式形象地展示项目构建的趋势和稳定性.还做到持 ...
- python I/O编程
1.文件读写 使用open打开文件,f=open('/user/test.txt','r'),r表示可读 如果文件不存在,则抛出IOError 文件打开,则用read()方法进行读取 最后关闭用clo ...
- 关于opengl中的矩阵平移,矩阵旋转,推导过程理解 OpenGL计算机图形学的一些必要矩阵运算知识
原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/12166896.html 为什么引入齐次坐标的变换矩阵可以表示平移呢? - Yu Mao的回答 ...
- Redis 持久化的两种方案
reids是一个key-value存储系统,为了保证效率,缓存在内存中,但是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,以保证数据的持久化. 所以:redis是一个支持持 ...
- 我们为什么会删除不了集群的 Namespace?
作者 | 声东 阿里云售后技术专家 导读:阿里云售后技术团队的同学,每天都在处理各式各样千奇百怪的线上问题.常见的有网络连接失败.服务器宕机.性能不达标及请求响应慢等.但如果要评选的话,什么问题看起 ...
- iOS开发常见问题
1. 在 ViewController 中添加子视图时,导航栏遮挡添加的子视图 let bpView = BpView.init(frame: CGRect.init(x: , y: , width: ...
- 利用Python进行数据分析学习记录(一)
1.Python的科学计算邮件列表 pydata:这是一个Google Group邮件列表,其中的问题都是Python数据分析和pandas方面的. pystatsmodels:针对Numpy相关的问 ...