LeetCode笔记(一)

更新…

2 . Add Two Numbers

#python2
# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        l3 = ListNode(0) #新建一个节点
        tem = l3  #表头
        l3sum = 0
        if l1 is None:
            return l2
        if l2 is None:
            return l1 
        while True:
            if l1 != None:
                l3sum = l3sum + l1.val
                l1 = l1.next
            if l2 != None:
                l3sum = l3sum + l2.val
                l2 = l2.next
                
            tem.val = l3sum % 10
            l3sum = int(l3sum/10)
            if l1 == None and l2 == None and l3sum == 0:
                break
            tem.next = ListNode(0)
            tem = tem.next
        return l3

12 . Integer to Roman. Integer to Roman
分析: 将数字每一位分割,分别对每一位数字进行转化

class Solution(object): #python
    def intToRoman(self, num):
        """
        :type num: int
        :rtype: str
        """
        dict = {0:("","I","II","III","IV","V","VI","VII","VIII","IX"), 
                1:("","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"), 
                2:("","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"), 
                3:("","M","MM","MMM")}
        sr = []
        sr.append(dict[3][num/1000%10])
        sr.append(dict[2][num/100%10])
        sr.append(dict[1][num/10%10])
        sr.append(dict[0][num%10])
        s=''
        for i in sr:
	        s=s+i
        return s

python3 运行报错
13. Roman to Integer
题目出处:LeetCode

分析: 从题目中可以发现罗马数字有其一一对应的数值,所以想到使用Python中的字典。

class Solution:   #python3
    def romanToInt(self, s):
        """
        :type s: str
        :rtype: int
        """
        sum = 0
        dict = {'M':1000,'D':500,'C':100,'L':50,'X':10,'V':5,'I':1}
        for i in range(len(s)-1):
            if dict[s[i]] < dict[s[i+1]]:
                sum-=dict[s[i]]
            else:
                sum+=dict[s[i]]
        sum+=dict[s[-1]] #加上最后一位
        return sum

20.Valid Parentheses
此题判断括号是否匹配,采用栈结构

class Solution(object):
    def isValid(self, s):
        """
        :type s: str
        :rtype: bool
        """
        stack = []
        for ch in s:
            if ch == '(' or ch == '[' or ch == '{' :
                stack.append(ch)
            else:
                if not stack:
                    return False
                if ch == ')' and stack[-1]!= '(' or ch == ']' and stack[-1]!='[' or ch == '}' and stack[-1]!='{':
                    return False
                stack.pop()
        return not stack
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

烤粽子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值