Leetcode----处理字符串
来源:互联网 发布:中美消费水平2017数据 编辑:程序博客网 时间:2024/05/08 11:50
1.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)罗马数字到整数的转换规则
个位数举例
I,1 】II,2】 III,3】 IV,4 】V,5 】VI,6】 VII,7】 VIII,8 】IX,9
·十位数举例
X,10】 XI,11 】XII,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】
2)处理千位数(如果有的话)
sum+=M的个数*1000
3)处理百位,十位,个位方法类似,只是字母不同
以百位为例:百位最多为四个字符
若第一字符为C,则sum+=C个数*100+D个数*300+M个数*800
若第一字符是D,则sum+=D个数*500+C个数*100
代码:
class Solution {public:int extraCac(string s, char a, char b, char c,int &sum, int base){ int i=0; int k=0,t=0,l=0; while(a==s[i]||b==s[i]||c==s[i]) { if(a==s[i]) ++k; if(b==s[i]) ++t; if(c==s[i]) ++l; ++i; } if(a==s[i-(k+l+t)]) sum+=((k+3*t+8*l)*base); else sum+=((5*t+k)*base); return i; }int romanToInt(string s) { int sum=0; int n=s.length(); int j=0; while('M'==s[j]) ++j; sum+=(1000*j); for(int i=j;i<n;) { if('C'==s[i]||'D'==s[i]) i=i+extraCac(s.substr(i),'C','D','M',sum,100); else if('X'==s[i]||'L'==s[i]) i=i+extraCac(s.substr(i),'X','L','C',sum,10); else i=i+extraCac(s.substr(i),'I','V','X',sum,1); } return sum; }};
可以改进的地方:没有查清楚罗马数字的记数方法
记数方法(百度百科)
改进的方法:
思路:从n-1个字符开始向前遍历,str[i] >=str[i+1],sum+str[i], str[i] < str[i+1],sum-str[i]
代码:
<strong> </strong>int romanToInt(string s) { if (s.empty()) { return 0; } unordered_map<char, int> mp { {'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000} }; int sum = mp[s.back()]; for (int i = s.size() - 2; i >= 0; --i) { sum += mp[s[i]] >= mp[s[i + 1]] ? mp[s[i]] : -mp[s[i]]; } return sum; }
- Leetcode----处理字符串
- leetcode字符串处理经典
- 【leetcode 字符串处理】Compare Version Numbers
- leetcode -- Simplify Path -- 字符串处理的题目
- LeetCode 68 Text Justification (字符串格式处理)
- LeetCode-67-Add Binary(数字/字符串处理)-Easy
- 67.LeetCode Add Binary(easy)[字符串 大数相加处理]
- 299.LeetCode Bulls and Cows(easy)[字符串处理 map]
- 205.leetcode Isomorphic Strings (easy)[map结构 字符串处理]
- 389.leetcode Find the Difference(easy)[字符串处理]
- 409.leetcode Longest Palindrome(easy)[字符串处理 最长回文]
- Leetcode 68. Text Justification (Airbnb面试题,字符串处理,边界条件)
- LeetCode 151 Reverse Words in a String (字符串处理 推荐)
- 1.leetcode-字符串处理函数atoi的书写
- LeetCode 415. Add Strings (大数加法,字符串处理)
- leetcode -- 434. Number of Segments in a String 【字符串处理】
- leetcode 字符串
- leetcode:字符串
- [iOS]判断当前时间是否在指定的时间段内
- BloomFilter——大规模数据处理利器
- #笔记#圣思园 JavaSE 第73讲——异常 Exception,自定义异常
- 对itoa函数的分析
- jsonp ajax jersey 跨域访问
- Leetcode----处理字符串
- URAL 1712. Cipher Grille (模拟)
- 【Pytest】pytest断言
- mybatis--使用in的多个参数查询。
- 后台调用js
- android 开发环境的搭建
- Mini Hook 主要代码理解
- ssanf
- 我只是为看一下而已