LeetCode—Python练习高级编程

leetcode中国官方网站-题库
https://leetcode-cn.com/problemset/all/
leetcode官网-题库
https://leetcode.com/problemset/all/
参考:
https://blog.csdn.net/qq_32424059/article/details/88855423

2.两数相加
给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。
如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
您可以假设除了数字 0 之外,这两个数都不会以 0 开头。

示例:自己实现:

输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807
class people():
    def df(self,num1,num2):
        s = []
        b=0
        print('*'*10)
        for i in range(0,len(num1)):
            print(i)
            a = num1[i] + num2[i]+b
            b=0
            if a//10 !=0:
                b=a//10
                s.append(a%10)
            else:
                s.append(a)
        return s
        
v = people()
print(v.df([2,4,3],[5,6,4]))
print(v.df([9,5,3],[5,6,4]))

在这里插入图片描述
参考别人的方法:

class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None
 
class Solution:
    # @return a ListNode
    def addTwoNumbers(self, l1, l2):
        if l1 is None:
            return l2
        elif l2 is None:
            return l1
        else:
            carry = 0
            ret = ListNode(0)
            ret_Last = ret
            while(l1 or l2):
                sum = 0
                if l1:
                    sum += l1.val
                    l1 = l1.next
                if l2:
                    sum += l2.val
                    l2 = l2.next
                sum += carry
                ret_Last.next = ListNode(sum % 10)
                ret_Last = ret_Last.next
                carry = (sum >= 10)
            ret = ret.next
            if carry:
                ret_Last.next = ListNode(1)
            del ret_Last
            return ret
 
def myPrintList(l):
    while(True):
        print(l.val)
        if l.next is not None:
            l = l.next
        else:
            print()
            break
 
if __name__ == '__main__':
    # 342 + 465 = 807
    l1_1 = ListNode(3)
    l1_2 = ListNode(4)
    l1_3 = ListNode(2)
    l1_1.next = l1_2
    l1_2.next = l1_3
 
    l2_1 = ListNode(4)
    l2_2 = ListNode(6)
    l2_3 = ListNode(5)
    l2_1.next = l2_2
    l2_2.next = l2_3
 
    l3 = Solution().addTwoNumbers(l1_1, l2_1)
    myPrintList(l3)
"""
1.val()函数的功能为:将一组字符型数据的数字部分转换成相应的数值型数据

val()函数用法:

例  x = "12 5fdsa DA456";

那么 val(x)应该返回125 后面的456不能返回来。

val()函数当识别到非数字,停止读入字符串。即如果字符串内有字母或其他非数字字符,val()函数只转换第一个 非数字字符之前的数字。当字符串的首字符为非数字时,返回值为0。

2.  .next() 返回迭代器的下一个项目。
next() 函数要和生成迭代器的iter() 函数一起使用。


"""

方法3:


"""
示例:
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807
"""

num_one = input('num_one:')
num_two = input('num_two:')
sum = []
b = 0
for i,j in zip(num_one,num_two):
    a,b = int(i) + int(j) + b,0
    b,c = a//10,a%10
    sum.append(c)
sum[-1] = sum[-1] + b*10
print(sum)

1.两数之和:

class Solution(object):
    def sum_two(self,list_,targets):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        d={}
        for i in range(len(list_)):
            if not list_[i] in d:
                d[list_[i]] = i
                print(d)
            if targets - list_[i] in d:
                print("-"*10)
                print(targets - list_[i])
                print(d[targets - list_[i]])
                ans = [d[targets - list_[i]],i]
                return ans
a = Solution()
e = a.sum_two([1,5,7,9,0],8)
print(e)

在这里插入图片描述
方法2:

class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        hashmap = {}
        for index, item in enumerate(nums):
#             if (target - item) in hashmap:
            #             if hashmap.has_key(target - item):
            if hashmap.get(target - item) is not None:
                return hashmap[target-item],index
            hashmap[item] = index
            print(hashmap)

a = Solution()
d = a.twoSum([1,2,3,4,5,6],9)
print(d)

在这里插入图片描述
方法3:

"""leetcode
计算两数之和
题目描述
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。

示例:

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
"""

if __name__ == '__main__':
    nums = [2, 7, 11, 15]
    target = 9
    dict = {j:i for i,j in enumerate(nums)}
    for num_one,i in dict.items():
        num_two = target-num_one
        index = dict.get(num_two,None)
        if index:
            break
    print(list((i,index)))

#结果:
[0, 1]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值