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

解:实践证明这里定义的回文数并不包括负数

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

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

  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呢。

解:用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的更多相关文章

  1. leetCode练题——9. Palindrome Number

    1.题目 9. Palindrome Number   Determine whether an integer is a palindrome. An integer is a palindrome ...

  2. leetcode之旅(10)-Roman to Integer

    题目描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...

  3. LeetCode记录之9——Palindrome Number

    LeetCode真是个好东西,本来闲了一下午不想看书,感觉太荒废时间了就来刷一道题.能力有限,先把easy的题目给刷完. Determine whether an integer is a palin ...

  4. 【LeetCode】9、Palindrome Number(回文数)

    题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...

  5. 【LeetCode算法-9】Palindrome Number

    LeetCode第9题 Determine whether an integer is a palindrome. An integer is a palindrome when it reads t ...

  6. LeetCode(9)Palindrome Number

    题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...

  7. Leetcode 题目整理 climbing stairs

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  8. Leetcode 题目整理-1

    1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...

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

    今天的两道题关于基本数据类型的探讨,估计也是要考虑各种情况,要细致学习 7. Reverse Integer Reverse digits of an integer. Example1: x = 1 ...

随机推荐

  1. 微信小程序map地图的一些使用注意事项

    1.小程序组件map,在微信7.0.4以上(不包括7.0.4)层级问题官方已作更新,可在map上随意添加任何标签使用z-index即可:微信7.0.4版本以下map组件层级默认是最高的,只能使用官方提 ...

  2. DOCKER学习_001:Docker简介

    一 Docker简介 1.1 docker由来 Docker的英文翻译是“码头工人”,即搬运工,它搬运的东西就是我们常说的集装箱Container,Container里面装的是任意类型的App.我们的 ...

  3. JavaScript 构造树形结构的一种高效算法

    引言 我们经常会碰到树形数据结构,比如组织层级.省市县或者动植物分类等等数据.下面是一个树形结构的例子: 在实际应用中,比较常见的做法是将这些信息存储为下面的结构,特别是当存在1对多的父/子节点关系时 ...

  4. 【题解】Killer Names($O(n\log n)$做法)

    [题解]Killer Names(\(O(n\log n)\)做法) HDU - 6143 感觉好久没做过这种直来直去的组合题,过来水一篇题解.还以为要写一个\(MTT\)或者三模数\(NTT\),想 ...

  5. $HDU1846\ Brave\ Game$ 博弈论

    正解:博弈论 解题报告: 传送门! 巴什博奕板子题鸭$QwQ$ 就有个结论,是说当$(m+1)\mid n$时先手必败,否则必胜 这个瞎证明一下就能出来 就考虑当$(m+1)\mid 1$时,若先手取 ...

  6. 使用sqlmap中的tamper脚本绕过waf

    使用sqlmap中tamper脚本绕过waf 脚本名:0x2char.py 作用:用UTF-8全角对应字符替换撇号字符 作用:用等价的CONCAT(CHAR(),...)对应替换每个(MySQL)0x ...

  7. 1068 万绿丛中一点红 (20分)C语言

    对于计算机而言,颜色不过是像素点对应的一个 24 位的数值.现给定一幅分辨率为 M×N 的画,要求你找出万绿丛中的一点红,即有独一无二颜色的那个像素点,并且该点的颜色与其周围 8 个相邻像素的颜色差充 ...

  8. js中的事件冒泡

    事件冒泡和阻止事件冒泡: 事件冒泡的原理:从实际操作的元素(事件)向上级父元素一级一级执行下去,直到达到document/window,冒泡过程结束.例如:假设我有一个 div 盒子,里面嵌套了1个子 ...

  9. 使用K均值算法进行图片压缩

    K均值算法   上一期介绍了机器学习中的监督式学习,并用了离散回归与神经网络模型算法来解决手写数字的识别问题.今天我们介绍一种机器学习中的非监督式学习算法--K均值算法.   所谓非监督式学习,是一种 ...

  10. red note8 pro谷歌套件

    谷歌核心Apps(即Google官方应用“全家桶”),包括YouTube,Google Now,Google Play store,Google Play Games,Google Maps等: 基于 ...