Skip to content

Commit fa4996a

Browse files
committed
15 temporary solution TIMILIMIT EXCEEDED
1 parent b07051c commit fa4996a

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

solution.py

+34-6
Original file line numberDiff line numberDiff line change
@@ -200,27 +200,55 @@ def isPalindrome(self, x):
200200

201201
if int(res) == x:
202202
return True
203+
203204
# leecode13
204205
def romanToInt(self, s):
205206
"""
206207
:type s: str
207208
:rtype: int
208209
"""
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}
210211
num = []
211212
res = 0
212213
for i in range(len(s)):
213214
num.append(romanInt[s[i]])
214215

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]
218219
else:
219220
res += num[i]
220221

221-
return res+num[len(s)-1]
222+
return res + num[len(s) - 1]
222223

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
223251

224252

225253
if __name__ == '__main__':
226-
print Solution().romanToInt('MCMXCIV')
254+
print Solution().threeSum([-2, 0, 1, 1, 2])

0 commit comments

Comments
 (0)