https://leetcode.com/problems/roman-to-integer/submissions/
class Solution(object):
SymbolValue = {
"I": 1,
"II": 2,
"III": 3,
"IV": 4,
"V": 5,
"VI": 6,
"VII": 7,
"VIII": 8,
"IX": 9,
"X": 10,
"XX": 20,
"XXX": 30,
"XL": 40,
"L": 50,
"LX": 60,
"LXX": 70,
"LXXX": 80,
"XC": 90,
"C": 100,
"CC": 200,
"CCC": 300,
"CD": 400,
"D": 500,
"DC": 600,
"DCC": 700,
"DCCC": 800,
"CM": 900,
"M": 1000,
"MM": 2000,
"MMM": 3000
}
SymbolList = [ "MMM", "MM", "M", "CM", "DCCC", "DCC", "DC", "D", "CD", "CCC", "CC", "C", "XC", "LXXX", "LXX", "LX", "L", "XL", "XXX", "XX", "X", "IX", "VIII", "VII", "VI", "V", "IV", "III", "II", "I"]
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
res = 0
idx = 0
terminal = len(s)
while idx < terminal:
offset = 0
for symbol in self.SymbolList:
_len_symbol = len(symbol)
if symbol == s[idx:idx + _len_symbol]:
offset = _len_symbol
res = res + self.SymbolValue[symbol]
break
idx += offset
return res
https://leetcode.com/problems/longest-common-prefix/submissions/
class Solution(object):
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
if len(strs) == 1:
return strs[0]
prefix = strs[0]
_len_prefix = len(prefix)
Res = ""
for i in range(_len_prefix, 0 , -1):
Temp = prefix[0:i]
flag = True
for j in range(1, len(strs), 1):
TempStrs = strs[j][0:i]
if Temp != TempStrs:
flag = False
break
if flag is True:
Res = Temp
break
return Res
https://leetcode.com/problems/valid-parentheses/submissions/
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
stack = []
for c in s:
if len(stack) == 0:
stack.append(c)
continue
top = stack.pop()
if c == ")" or c == "]" or c == "}":
temp = "%c%c"%(top, c)
if temp == "()" or temp == "[]" or temp == "{}":
pass
else:
stack.append(top)
stack.append(c)
else:
stack.append(top)
stack.append(c)
if len(stack) > 0:
return False
else:
return True
网友评论