@@ -200,27 +200,55 @@ def isPalindrome(self, x):
200
200
201
201
if int (res ) == x :
202
202
return True
203
+
203
204
# leecode13
204
205
def romanToInt (self , s ):
205
206
"""
206
207
:type s: str
207
208
:rtype: int
208
209
"""
209
- romanInt = {'I' :1 , 'V' :5 , 'X' :10 ,'L' :50 ,'C' :100 ,'D' :500 ,'M' :1000 }
210
+ romanInt = {'I' : 1 , 'V' : 5 , 'X' : 10 , 'L' : 50 , 'C' : 100 , 'D' : 500 , 'M' : 1000 }
210
211
num = []
211
212
res = 0
212
213
for i in range (len (s )):
213
214
num .append (romanInt [s [i ]])
214
215
215
- for i in range (len (s )- 1 ):
216
- if num [i ]< num [i + 1 ]:
217
- res -= num [i ]
216
+ for i in range (len (s ) - 1 ):
217
+ if num [i ] < num [i + 1 ]:
218
+ res -= num [i ]
218
219
else :
219
220
res += num [i ]
220
221
221
- return res + num [len (s )- 1 ]
222
+ return res + num [len (s ) - 1 ]
222
223
224
+ # leetcode15
225
+ def threeSum (self , nums ):
226
+ """
227
+ :type nums: List[int]
228
+ :rtype: List[List[int]]
229
+ """
230
+ res = []
231
+ nums = sorted (nums )
232
+ print nums
233
+ for i in range (len (nums )):
234
+ start = i + 1
235
+ end = len (nums ) - 1
236
+ if start >= end :
237
+ break
238
+ else :
239
+ while start < end :
240
+ if nums [i ] + nums [start ] + nums [end ] > 0 :
241
+ end -= 1
242
+ elif nums [i ] + nums [start ] + nums [end ] < 0 :
243
+ start += 1
244
+ else :
245
+ if [nums [i ], nums [start ], nums [end ]] not in res :
246
+ res .append ([nums [i ], nums [start ], nums [end ]])
247
+ end -= 1
248
+ start += 1
249
+
250
+ return res
223
251
224
252
225
253
if __name__ == '__main__' :
226
- print Solution ().romanToInt ( 'MCMXCIV' )
254
+ print Solution ().threeSum ([ - 2 , 0 , 1 , 1 , 2 ] )
0 commit comments