zoukankan      html  css  js  c++  java
  • leetcode python 002

    ##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()

    ----蚂蚁不在线
  • 相关阅读:
    0455分发饼干 Marathon
    0078子集 Marathon
    python 实现JWT Marathon
    0376摆动序列 Marathon
    0216.组合总和 III Marathon
    028实现strStr() Marathon
    0738单调递增的数字 Marathon
    0051N皇后 Marathon
    0047全排列II Marathon
    0037解数独 Marathon
  • 原文地址:https://www.cnblogs.com/offline-ant/p/9309249.html
Copyright © 2011-2022 走看看