Skip to content

Commit e2c2e8d

Browse files
committedMay 15, 2021
feat: add cpp solutions to leetcode problem No.12,No.13
1 parent 8cbac15 commit e2c2e8d

File tree

6 files changed

+131
-0
lines changed

6 files changed

+131
-0
lines changed
 

‎solution/0000-0099/0012.Integer to Roman/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,26 @@ class Solution {
119119
}
120120
```
121121

122+
### **C++**
123+
124+
```cpp
125+
class Solution {
126+
public:
127+
string intToRoman(int num) {
128+
vector<int> nums{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
129+
vector<string> romans{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
130+
string ans;
131+
for (int i = 0; i < nums.size(); ++i) {
132+
while (num >= nums[i]) {
133+
num -= nums[i];
134+
ans.append(romans[i]);
135+
}
136+
}
137+
return ans;
138+
}
139+
};
140+
```
141+
122142
### **...**
123143
124144
```

‎solution/0000-0099/0012.Integer to Roman/README_EN.md

+20
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,26 @@ class Solution {
111111
}
112112
```
113113

114+
### **C++**
115+
116+
```cpp
117+
class Solution {
118+
public:
119+
string intToRoman(int num) {
120+
vector<int> nums{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
121+
vector<string> romans{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
122+
string ans;
123+
for (int i = 0; i < nums.size(); ++i) {
124+
while (num >= nums[i]) {
125+
num -= nums[i];
126+
ans.append(romans[i]);
127+
}
128+
}
129+
return ans;
130+
}
131+
};
132+
```
133+
114134
### **...**
115135
116136
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
string intToRoman(int num) {
4+
vector<int> nums{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
5+
vector<string> romans{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
6+
string ans;
7+
for (int i = 0; i < nums.size(); ++i) {
8+
while (num >= nums[i]) {
9+
num -= nums[i];
10+
ans.append(romans[i]);
11+
}
12+
}
13+
return ans;
14+
}
15+
};

‎solution/0000-0099/0013.Roman to Integer/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,33 @@ class Solution {
154154
}
155155
```
156156

157+
### **C++**
158+
159+
```cpp
160+
class Solution {
161+
public:
162+
int romanToInt(string s) {
163+
unordered_map<char, int> nums{
164+
{'I', 1},
165+
{'V', 5},
166+
{'X', 10},
167+
{'L', 50},
168+
{'C', 100},
169+
{'D', 500},
170+
{'M', 1000},
171+
};
172+
int ans = 0;
173+
for (int i = 0; i < s.size() - 1; ++i) {
174+
if (nums[s[i]] < nums[s[i + 1]])
175+
ans -= nums[s[i]];
176+
else
177+
ans += nums[s[i]];
178+
}
179+
return ans + nums[s.back()];
180+
}
181+
};
182+
```
183+
157184
### **...**
158185
159186
```

‎solution/0000-0099/0013.Roman to Integer/README_EN.md

+27
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,33 @@ class Solution {
205205
}
206206
```
207207

208+
### **C++**
209+
210+
```cpp
211+
class Solution {
212+
public:
213+
int romanToInt(string s) {
214+
unordered_map<char, int> nums{
215+
{'I', 1},
216+
{'V', 5},
217+
{'X', 10},
218+
{'L', 50},
219+
{'C', 100},
220+
{'D', 500},
221+
{'M', 1000},
222+
};
223+
int ans = 0;
224+
for (int i = 0; i < s.size() - 1; ++i) {
225+
if (nums[s[i]] < nums[s[i + 1]])
226+
ans -= nums[s[i]];
227+
else
228+
ans += nums[s[i]];
229+
}
230+
return ans + nums[s.back()];
231+
}
232+
};
233+
```
234+
208235
### **...**
209236
210237
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
int romanToInt(string s) {
4+
unordered_map<char, int> nums{
5+
{'I', 1},
6+
{'V', 5},
7+
{'X', 10},
8+
{'L', 50},
9+
{'C', 100},
10+
{'D', 500},
11+
{'M', 1000},
12+
};
13+
int ans = 0;
14+
for (int i = 0; i < s.size() - 1; ++i) {
15+
if (nums[s[i]] < nums[s[i + 1]])
16+
ans -= nums[s[i]];
17+
else
18+
ans += nums[s[i]];
19+
}
20+
return ans + nums[s.back()];
21+
}
22+
};

0 commit comments

Comments
 (0)
Please sign in to comment.