-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path13.cpp
237 lines (227 loc) · 6.24 KB
/
13.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
__________________________________________________________________________________________________
12ms
static int x=[](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}();
class Solution {
public:
int romanToInt(string &s) {
int num[26] = {0,0,100,500,0,0,0,0,1,0,0,50,1000,0,0,0,0,0,0,0,0,5,0,10,0,0};
int res = 0;
int len = s.size();
for(int i=0;i<s.size();i++){
res += num[s[len-i-1]-'A'];
if(s[len-i-1]=='V' || s[len-i-1]=='X') num['I'-'A'] = -1;
if(s[len-i-1]=='L' || s[len-i-1]=='C') num['X'-'A'] = -10;
if(s[len-i-1]=='D' || s[len-i-1]=='M') num['C'-'A'] = -100;
}
return res;
}
};
__________________________________________________________________________________________________
16ms
class Solution {
public:
int charValue(char c)
{
switch(c){
case 'I' : return 1;
case 'V' : return 5 ;
case 'X' : return 10;
case 'L' : return 50;
case 'C' : return 100 ;
case 'D' : return 500;
case 'M' : return 1000;
default :
return 0;
}
return 0;
}
int romanToInt(string s)
{
int result=0;
if(s.empty()) return 0;
if(s.size()==1) return charValue(s[0]); // only 1
int temp=charValue(s[0]); // store first char
for(int i =1;i<s.size();++i)
{
int v = charValue(s[i]); // from 1 calcuate the char value
if(v>temp) // if its greater then IV we have to minus for the value 5-1
{
result=result-temp;
temp=v;
}
else
{
result=result+temp; // VI we have to add 5+1
temp=v;
}
}
result+=temp;
return result;
}
};
__________________________________________________________________________________________________
20ms
namespace {
int getTokenValue(const char c) {
switch (c) {
case 'I':
return 1;
case 'V':
return 5;
case 'X':
return 10;
case 'L':
return 50;
case 'C':
return 100;
case 'D':
return 500;
case 'M':
return 1000;
default:
return 0;
}
}
}
class Solution {
public:
int romanToInt(string s) {
vector<int> values;
values.resize(s.size());
std::transform(s.begin(), s.end(), values.begin(), &getTokenValue);
for (int i = 1; i < values.size(); ++i) {
if ((values[i-1] == values[i] / 10) ||
(values[i-1] == values[i] / 5)) {
values[i-1] *= -1;
}
}
return std::accumulate(values.begin(), values.end(), 0);
}
};
__________________________________________________________________________________________________
8204 kb
static bool _foo = ios::sync_with_stdio(false);
static ostream* _bar = cin.tie(NULL);
class Solution
{
public:
int romanToInt(string s)
{
int va[]{1,5,10,50,100,500,1000};
string sy="IVXLCDM";
int t[s.size()];
for(int i = 0; i<s.size();i++)
{
for(int j=0;j<7;j++)
{
if(s[i]==sy[j])
{
t[i]=j;
break;
}
}
}
int re = 0;
for(int i=0;i<s.size();i++)
{
re+=va[t[i]];
if(i!=s.size()-1&&t[i]<t[i+1])
{
re += va[t[i+1]]-2*va[t[i]];
i++;
}
}
return re;
}
};
__________________________________________________________________________________________________
8344 kb
static int x = [](){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
return NULL;
}();
class Solution {
public:
int romanToInt(string s) {
enum prior { I = 1, IV = 4, V=5, IX = 9, X=10, XL = 40, L=50, XC = 90, C=100,CD = 400, D=500,CM= 900, M=1000};
int ans = 0;
int i = 0, l = s.length();
while(i<l){
char x = s[i];
switch(x){
case 'M':
ans+=M;
i++;
continue;
case 'D':
ans+=D;
i++;
continue;
case 'C':
if(i+1<l && s[i+1]=='D'){
ans+=CD;
i+=2;
continue;
}
else if(i+1<l && s[i+1] == 'M'){
ans+=CM;
i+=2;
continue;
}
else{
ans+=C;
i++;
continue;
}
case 'L':
ans+=L;
i++;
continue;
case 'X':
if(i+1<l && s[i+1]=='L'){
ans+=XL;
i+=2;
continue;
}
else if(i+1<l && s[i+1] == 'C'){
ans+=XC;
i+=2;
continue;
}
else{
ans+=X;
i++;
continue;
}
case 'V':
ans+=V;
i++;
continue;
case 'I':
if(i+1<l && s[i+1]=='V'){
ans+=IV;
i+=2;
continue;
}
else if(i+1<l && s[i+1] == 'X'){
ans+=IX;
i+=2;
continue;
}
else{
ans+=I;
i++;
continue;
}
}
}
return ans;
}
};
__________________________________________________________________________________________________