@@ -80,9 +80,11 @@ M 1000</pre>
80
80
81
81
## 解法
82
82
83
- ** 方法一:模拟**
83
+ ** 方法一:哈希表 + 模拟**
84
84
85
- 因为字符串的长度 $1 \leq s.length \leq 15$,故时间复杂度为 $O(1)$,空间复杂度为 $O(1)$。
85
+ 我们先用哈希表 $d$ 记录每个字符对应的数值,然后从左到右遍历字符串 $s$,如果当前字符对应的数值小于右边字符对应的数值,则减去当前字符对应的数值,否则加上当前字符对应的数值。
86
+
87
+ 时间复杂度 $(n)$,空间复杂度 $O(m)$。其中 $n$ 和 $m$ 分别为字符串 $s$ 的长度和字符集的大小。
86
88
87
89
<!-- 这里可写通用的实现逻辑 -->
88
90
@@ -95,15 +97,15 @@ M 1000</pre>
95
97
``` python
96
98
class Solution :
97
99
def romanToInt (self , s : str ) -> int :
98
- romans = {' I' : 1 , ' V' : 5 , ' X' : 10 ,
99
- ' L' : 50 , ' C' : 100 , ' D' : 500 , ' M' : 1000 }
100
100
ans = 0
101
- for i in range (len (s) - 1 ):
102
- if romans[s[i]] < romans[s[i + 1 ]]:
103
- ans -= romans[s[i]]
101
+ d = {' I' : 1 , ' V' : 5 , ' X' : 10 , ' L' : 50 , ' C' : 100 , ' D' : 500 , ' M' : 1000 }
102
+ for a, b in pairwise(s):
103
+ if d[a] < d[b]:
104
+ ans -= d[a]
104
105
else :
105
- ans += romans[s[i]]
106
- return ans + romans[s[- 1 ]]
106
+ ans += d[a]
107
+ ans += d[s[- 1 ]]
108
+ return ans
107
109
```
108
110
109
111
### ** Java**
@@ -113,31 +115,23 @@ class Solution:
113
115
``` java
114
116
class Solution {
115
117
public int romanToInt (String s ) {
116
- Map<String , Integer > nums = new HashMap<> ();
117
- nums. put(" M" , 1000 );
118
- nums. put(" CM" , 900 );
119
- nums. put(" D" , 500 );
120
- nums. put(" CD" , 400 );
121
- nums. put(" C" , 100 );
122
- nums. put(" XC" , 90 );
123
- nums. put(" L" , 50 );
124
- nums. put(" XL" , 40 );
125
- nums. put(" X" , 10 );
126
- nums. put(" IX" , 9 );
127
- nums. put(" V" , 5 );
128
- nums. put(" IV" , 4 );
129
- nums. put(" I" , 1 );
130
- int res = 0 ;
131
- for (int i = 0 ; i < s. length();) {
132
- if (i + 1 < s. length() && nums. get(s. substring(i, i + 2 )) != null ) {
133
- res += nums. get(s. substring(i, i + 2 ));
134
- i += 2 ;
118
+ String cs = " IVXLCDM" ;
119
+ int [] vs = {1 , 5 , 10 , 50 , 100 , 500 , 1000 };
120
+ Map<Character , Integer > d = new HashMap<> ();
121
+ for (int i = 0 ; i < vs. length; ++ i) {
122
+ d. put(cs. charAt(i), vs[i]);
123
+ }
124
+ int ans = 0 ;
125
+ int n = s. length();
126
+ for (int i = 0 ; i < n - 1 ; ++ i) {
127
+ if (d. get(s. charAt(i)) < d. get(s. charAt(i + 1 ))) {
128
+ ans -= d. get(s. charAt(i));
135
129
} else {
136
- res += nums. get(s. substring(i, i + 1 ));
137
- i += 1 ;
130
+ ans += d. get(s. charAt(i));
138
131
}
139
132
}
140
- return res;
133
+ ans += d. get(s. charAt(n - 1 ));
134
+ return ans;
141
135
}
142
136
}
143
137
```
@@ -148,7 +142,7 @@ class Solution {
148
142
class Solution {
149
143
public:
150
144
int romanToInt(string s) {
151
- unordered_map<char, int> nums {
145
+ unordered_map<char, int> nums{
152
146
{'I', 1},
153
147
{'V', 5},
154
148
{'X', 10},
@@ -159,10 +153,11 @@ public:
159
153
};
160
154
int ans = 0;
161
155
for (int i = 0; i < s.size() - 1; ++i) {
162
- if (nums[ s[ i]] < nums[ s[ i + 1]] )
156
+ if (nums[ s[ i]] < nums[ s[ i + 1]] ) {
163
157
ans -= nums[ s[ i]] ;
164
- else
158
+ } else {
165
159
ans += nums[ s[ i]] ;
160
+ }
166
161
}
167
162
return ans + nums[ s.back()] ;
168
163
}
@@ -172,20 +167,45 @@ public:
172
167
### **Go**
173
168
174
169
```go
175
- func romanToInt(s string) int {
176
- romans := map[byte]int{'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
177
- ans := 0
170
+ func romanToInt(s string) (ans int) {
171
+ d := map[byte]int{'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
178
172
for i := 0; i < len(s)-1; i++ {
179
- if romans [s[i]] < romans [s[i+1]] {
180
- ans -= romans [s[i]]
173
+ if d [s[i]] < d [s[i+1]] {
174
+ ans -= d [s[i]]
181
175
} else {
182
- ans += romans [s[i]]
176
+ ans += d [s[i]]
183
177
}
184
178
}
185
- return ans + romans[s[len(s)-1]]
179
+ ans += d[s[len(s)-1]]
180
+ return
186
181
}
187
182
```
188
183
184
+ ### ** JavaScript**
185
+
186
+ ``` js
187
+ const romanToInt = function (s ) {
188
+ const d = {
189
+ I : 1 ,
190
+ V : 5 ,
191
+ X : 10 ,
192
+ L : 50 ,
193
+ C : 100 ,
194
+ D : 500 ,
195
+ M : 1000 ,
196
+ };
197
+ let ans = 0 ;
198
+ for (let i = 0 ; i < s .length ; ++ i) {
199
+ if (d[s[i]] < d[s[i + 1 ]]) {
200
+ ans -= d[s[i]];
201
+ } else {
202
+ ans += d[s[i]];
203
+ }
204
+ }
205
+ return ans;
206
+ };
207
+ ```
208
+
189
209
### ** PHP**
190
210
191
211
``` php
0 commit comments