当前位置:网站首页>(leisure) leetcode13 Roman to Integer
(leisure) leetcode13 Roman to Integer
2022-07-22 02:21:00 【phac123】
Topic link :: delivery
Learn something :
- C I directly traverse the language , Actually, it's written later , I found that I can actually write a function , It will be simpler to write in this way .
- Python The length of the first traversal side is 2 Of , Then go over it again, and the length is 1 Of ; You can use Python Dictionary in , then ,in and not in useful .
Code :
Python:
class Solution:
def romanToInt(self, s: str) -> int:
my_bo = []; num = 0;
my_dictionary = {
'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000,
'IV': 4, 'IX': 9, 'XL': 40, 'XC': 90, 'CD': 400, 'CM': 900,
}
for i in range(0, len(s) - 1):
ss = s[i] + s[i + 1];
if(ss in my_dictionary):
num += my_dictionary[ss]
my_bo.append(i); my_bo.append(i + 1);
i += 1;
for i in range(0, len(s)):
if(i not in my_bo):
num += my_dictionary[s[i]]
return num
C++:
class Solution {
public:
int romanToInt(string s) {
string str = s;
int sum = 0;
for(int i = 0; i < s.size(); i ++) {
if(str[i] == 'I') {
if(i + 1 < s.size()) {
if(str[i + 1] == 'V') {
//IV
sum += 4;
i ++;
}
else if(str[i + 1] == 'X') {
//IX
sum += 9;
i ++;
} else sum += 1; //I
} else {
sum += 1; //I
}
} else if(str[i] == 'V') {
sum += 5;
} else if(str[i] == 'X') {
if(i + 1 < s.size()) {
if(str[i + 1] == 'L') {
//XL
sum += 40;
i ++;
}
else if(str[i + 1] == 'C') {
//LC
sum += 90;
i ++;
} else sum += 10; //L
} else {
sum += 10; //L
}
} else if(str[i] == 'L') {
sum += 50;
} else if(str[i] == 'C') {
if(i + 1 < s.size()) {
if(str[i + 1] == 'D') {
//CD
sum += 400;
i ++;
}
else if(str[i + 1] == 'M') {
//CM
sum += 900;
i ++;
} else sum += 100; //C
} else {
sum += 100; //C
}
} else if(str[i] == 'D') {
sum += 500;
} else if(str[i] == 'M') {
sum += 1000;
}
}
return sum;
}
};
边栏推荐
猜你喜欢
M carry out DS-CDMA modeling in Simulink, and then call Simulink model through MATLAB to simulate the bit error rate
Seata details of distributed transactions
[论文精读]Attention Is All You Need
HCIP第十一天笔记
A thorough understanding of bio, NiO, AIO
Matlab simulation analysis of spectrum switching mechanism of cognitive radio based on relay assistance
Distributed transaction two-phase commit, at mode, TCC mode
字节跳动确认将自研芯片:仅供内部使用;马斯克:我已将大脑上传云端;Go 语言产品负责人离职|极客头条...
Childhood memories games are coming - greedy snakes, come and write your own games
Static distribution and dynamic distribution in trust
随机推荐
js对象:检测属性是否存在
Qt | Qt的项目文件.pro文件详解
Grasp the drive of morning meeting 【 target management 】
【开发教程6】AI语音人脸识别(会议记录仪/人脸打卡机)-串口
On Newton iteration
07.02 Huffman code
M carry out DS-CDMA modeling in Simulink, and then call Simulink model through MATLAB to simulate the bit error rate
Replace ribbon load balancer with loadbalancer
SVD奇异值分解——矩阵压缩
Distributed transaction two-phase commit, at mode, TCC mode
过时 2 天后,微软放弃“封禁”商业开源!
童年的回忆小游戏来了——贪吃蛇,快来自己动手写一个属于自己的小游戏吧
rose
SkyWalking服务端的搭建、微服务接入SkyWalking
字节跳动确认将自研芯片:仅供内部使用;马斯克:我已将大脑上传云端;Go 语言产品负责人离职|极客头条...
JS object: several ways to implement inheritance and their advantages and disadvantages
Creation of gateway routing service
[deep learning notes] attention mechanism
(闲暇)LeetCode9. Palindrome Number
Gateway integrates sentinel to implement flow restriction rules for routing