Leetcode 题目整理-3

9. Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space.

click to show spoilers.

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的方法,要注意基本变量类型的范围是否溢出。

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上。那么问题来了,罗马数字啥样?是怎么组数的?

涨知识,一下是罗马数字的一些解释也不知道对不对(百度百科):

  1. 相同的数字连写、所表示的数等于这些数字相加得到的数、如:Ⅲ=3;
  2. 小的数字在大的数字的右边、所表示的数等于这些数字相加得到的数、
    如:Ⅷ=8、Ⅻ=12;
  3. 小的数字、(限于
    Ⅰ、X 和 C)在大的数字的左边、所表示的数等于大数减小数得到的数、如:Ⅳ=4、Ⅸ=9;
  4. 正常使用时、连写的数字重复不得超过三次;
  5. 在一个数的上面画一条横线、表示这个数扩大 1000 倍。

有几条须注意掌握:

  1. 基本数字
    Ⅰ、X 、C 中的任何一个、自身连用构成数目、或者放在大数的右边连用构成数目、都不能超过三个;放在大数的左边只能用一个;
  2. 不能把基本数字 V 、L 、D 中的任何一个作为小数放在大数的左边采用相减的方法构成数目;放在大数的右边采用相加的方式构成数目、只能使用一个;
  3. I只能用在V和X左边;
  4. X只能用在L和C左边;
  5. 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呢。

窗体顶端

窗体底端

时间: 2024-08-03 14:24:37

Leetcode 题目整理-3的相关文章

Leetcode 题目整理-3 Palindrome Number & Roman to Integer

9. Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the res

Leetcode 题目整理-4

14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings. 注:这题竟然连个示例都没有,说明特殊情况并不多,就是要找出所有字符串的最长公共前缀.他应该不会超过所有字符串中最短的那个,可以试着找出最短长度n,然后每个都读取和比较n个,并根据情况不断减小那个n. 19. Remove Nth Node From End of List

Leetcode 题目整理-2 Reverse Integer && String to Integer

今天的两道题关于基本数据类型的探讨,估计也是要考虑各种情况,要细致学习 7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if

Leetcode 题目整理-2

今天的两道题关于基本数据类型的探讨,估计也是要考虑各种情况,要细致学习 7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if

Leetcode 题目整理 Sqrt && Search Insert Position

Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 注:这里的输入输出都是整数说明不会出现 sqrt(7)这种情况,思路一就是应用二分法进行查找.每次给出中间值,然后比对cur的平方与目标值的大小.需要先设定两个变量用来存放左右游标. 这里要考虑一下整型溢出的问题,另外,即使不能开出整数的也要近似给出整数部分,不能忽略. 代码如下: int Solution::mySqrt(int x) { //

数据库题目整理及详解(四)

前言 有多少次挥汗如雨,伤痛曾添满记忆,只因为始终相信,去拼搏才能胜利.总在鼓舞自己,要成功就得努力.热血在赛场沸腾,巨人在赛场升起. 相信自己,你将赢得胜利,创造奇迹:相信自己,梦想在你手中,这是你的天地.当一切过去,你们将是第一. 相信自己,你们将超越极限,超越自己! 相信自己,加油吧,健儿们,相信你自己. 坐在中体对面, 听着这振奋激昂的加油欢呼声, 照样可以感受到校运会的气势磅礴, 虽然我还在敲代码-- 来个这个吧, 特殊纪念, 沃夫慈悲: 说明 老生常谈! 接着之前的SQL语句继续整理

【Android进阶】Android面试题目整理与讲解

这一篇文章专门整理一下研究过的Android面试题,内容会随着学习不断的增加,如果答案有错误,希望大家可以指正 1.简述Activity的生命周期 当Activity开始启动的时候,首先调用onCreate(),onStart(),onResume()方法,此时Activity对用户来说,是可见的状态 当Activity从可见状态变为被Dialog遮挡的状态的时候,会调用onPause()方法,此时的Activity对用户可见,但是不能相 应用户的点击事件 当Activity从可见状态变为被其他

leetcode题目:Clone Graph

题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label and each n

ACM 字符串 题目整理

AC自动机 UVa 11468  Substring AC自动机+概率DP. 注意要补全不存在的边. 为什么要补全不存在的边呢?补全以后可以直接找到状态的转移,即从所有子节点就可以实现所有状态转移. #include<iostream> #include<vector> #include<cmath> #include<map> #include<algorithm> #include<cstring> #include<cst