##002 Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8
# 链表节点都是一位数字,以上可以视为243+564=807
#先定义节点和链表类
import numpy as np
import time

class Node(object):
    def __init__(self,n,next_node=None):
        self.data=n
        self.next=next_node
    
class linklist(object):
    def __init__(self):
        self.head=None

def init(self,data):
        assert type(data)==list,type(data)
        self.head=Node(data[0],None)
        p=self.head
        for i in data[1:]:
            node=Node(i)
            p.next=node
            p=p.next

def show(self):
        l=[]
        p=self.head
        while p:
            l.append(str(p.data))
            p=p.next
        print('->'.join(l))

l1,l2=[],[]
x=1000000
t=time.time()
for i in range(x):
    l1.append(np.random.randint(0,10))
    l2.append(np.random.randint(0,10))
t=time.time()-t
print('%s 元素用时 %s s'%(x,t))
t=time.time()
ll1,ll2=linklist(),linklist()
ll1.init(l1)
ll2.init(l2)
#ll1.show()
#ll2.show()
p1,p2=ll1.head,ll2.head
ll3=linklist()
flg=0
while p1 and p2:
    num=p1.data+p2.data+flg
    p3=ll3.head
    ll3.head=Node(num%10)
    ll3.head.next=p3
    p1,p2=p1.next,p2.next
    flg=0
    if num>9:
        flg=1
if flg==1:
    p3=ll3.head
    ll3.head=Node(1)
    ll3.head.next=p3
t=time.time()-t
print('%s 元素用时 %s s'%(x,t))
#ll3.show()

leetcode python 002的更多相关文章

  1. Leetcode Python Solution(continue update)

    leetcode python solution 1. two sum (easy) Given an array of integers, return indices of the two num ...

  2. LeetCode python实现题解(持续更新)

    目录 LeetCode Python实现算法简介 0001 两数之和 0002 两数相加 0003 无重复字符的最长子串 0004 寻找两个有序数组的中位数 0005 最长回文子串 0006 Z字型变 ...

  3. [LeetCode][Python]Container With Most Water

    # -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/container-with-most-water/ Given n non-neg ...

  4. LeetCode Python 位操作 1

    Python 位操作: 按位与 &, 按位或 | 体会不到 按位异或 ^ num ^ num = 0 左移 << num << 1 == num * 2**1 右移 & ...

  5. 【leetcode❤python】Sum Of Two Number

    #-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100 ...

  6. [Leetcode][Python]56: Merge Intervals

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...

  7. [Leetcode][Python]55: Jump Game

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 55: Jump Gamehttps://leetcode.com/probl ...

  8. [Leetcode][Python]54: Spiral Matrix

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 54: Spiral Matrixhttps://leetcode.com/p ...

  9. [Leetcode][Python]53: Maximum Subarray

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...

随机推荐

  1. Eclipse无法使用springboot2.x

    <!-- 阿里云提供的镜像地址 --> <mirror> <id>nexus-aliyun</id> <mirrorOf>*</mir ...

  2. (10)进程---Manager数据共享

    Manager  能够实现进程之间的数据共享(dict list),但是必须上锁来确保数据的准确性, 队列则可以实现进程之间数据通信 from multiprocessing import Proce ...

  3. 酷开 5.5 版本安装第三方app

    https://www.znds.com/jc/article/2952-1.html .开始安装(以安装当贝桌面为例): adb connect 192.168.XXX.XXX(电视IP) adb ...

  4. <转载>MacOS下安装小米SQL优化工具soar

    原文链接:https://www.cnblogs.com/QuestionsZhang/p/10326105.html 1 下载源码包 赋予权限 wget https://github.com/Xia ...

  5. LeetCode--437--路径总和3

    问题描述: 给定一个二叉树,它的每个结点都存放着一个整数值. 找出路径和等于给定数值的路径总数. 路径不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点到子节点). 二 ...

  6. LeetCode--367--有效的完全平方数

    问题描述: 给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False. 说明:不要使用任何内置的库函数,如  sqrt. 示例 1: 输入:16 输 ...

  7. pytorch 中的 split

    Pytorch中的split问题: 1.使用torch.nn.Conv2d中有个参数是groups会将输入的feature map分组,此处需要注意的一点是分组之后各组的feature map的cha ...

  8. Music in Car CodeForces - 746F (贪心,模拟)

    大意: n首歌, 第$i$首歌时间$t_i$, 播放完获得贡献$a_i$, 最多播放k分钟, 可以任选一首歌开始按顺序播放, 最多选w首歌半曲播放(花费时间上取整), 求贡献最大值. 挺简单的一个题, ...

  9. 文件名简体转繁体bat

    @echo off rem 指定文件夹路径 set "fd=D:\下载的图片" rem 0为转换文件名,1为转换文件夹名,2为同时转换文件名和文件夹名 set f=0 rem 0为 ...

  10. ATOM常用插件推荐

    转载:http://blog.csdn.net/qq_30100043/article/details/53558381 ATOM常用插件推荐 simplified-chinese-menu ATOM ...