力扣算法-简单

98 阅读1分钟

leecode 1 两数之和

class Solution:
	def twoSum(slef,nums,target):
		for i in range(len(nums)):
			for j in range(i+1,len(nums)):
				if nums[i] + nums[j] == target:
					return [i,j]
		return [0,0]
				
if __name__ == '__main__':
	so = Solution()
	ret = so.twoSum([3,2,4],6)
	print(ret)

leecode 9 回文数

class Solution(object):
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        str_x = str(x)
        str_len = int(len(str(x))/2)
        for i in range(str_len):
            if str_x[i]!=str_x[len(str_x)-i-1]:
                return False
        return True

leecode 13 罗马数字转整数

class Solution:
	SYMBOL_DICT = {
		'I':1,
		'V':5,
		'X':10,
		'L':50,
		'C':100,
		'D':500,
		'M':1000,
	}
	def roman_to_int(self,s):
		ans = 0
		n = len(s)
		for k,v in enumerate(s):
			value = Solution.SYMBOL_DICT[v]
			if k+1<n and value < Solution.SYMBOL_DICT[s[k+1]] :
				ans -= value
			else:
				ans += value
		print(ans)
		
if __name__ == '__main__':
	so = Solution()
	b = so.roman_to_int('III')