Posts

13. Roman to Integer

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. Python 3 Code (By Wake Liu): class Solution:     def romanToInt(self, s):         """         :type s: str         :rtype: int         """         roman = {'M': 1000,'D': 500 ,'C': 100,'L': 50,'X': 10,'V': 5,'I': 1}         ans = roman[s[-1]]         for i in range(len(s)-1):             ans += roman[s[i]]*((roman[s[i]]>=roman[s[i+1]])*2-1)         return ans

7. Reverse Integer

Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321 Example 3: Input: 120 Output: 21 Note: Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. Python3 Code: class Solution:     def reverse(self, x):         """         :type x: int         :rtype: int         """         if x < 0:             x *= -1             flag = False         else:             flag = True         y = int(0)         while x > 0:       ...