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()
leetcode python 002的更多相关文章
- Leetcode Python Solution(continue update)
		leetcode python solution 1. two sum (easy) Given an array of integers, return indices of the two num ... 
- LeetCode python实现题解(持续更新)
		目录 LeetCode Python实现算法简介 0001 两数之和 0002 两数相加 0003 无重复字符的最长子串 0004 寻找两个有序数组的中位数 0005 最长回文子串 0006 Z字型变 ... 
- [LeetCode][Python]Container With Most Water
		# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/container-with-most-water/ Given n non-neg ... 
- LeetCode Python 位操作 1
		Python 位操作: 按位与 &, 按位或 | 体会不到 按位异或 ^ num ^ num = 0 左移 << num << 1 == num * 2**1 右移 & ... 
- 【leetcode❤python】Sum Of Two Number
		#-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100 ... 
- [Leetcode][Python]56: Merge Intervals
		# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ... 
- [Leetcode][Python]55: Jump Game
		# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 55: Jump Gamehttps://leetcode.com/probl ... 
- [Leetcode][Python]54: Spiral Matrix
		# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 54: Spiral Matrixhttps://leetcode.com/p ... 
- [Leetcode][Python]53: Maximum Subarray
		# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ... 
随机推荐
- gradlew 的https代理设定
			在内网编译vlc for Android 时, 总是在 [./gradlew assemble] 卡住, 在网上找到了设置代理的方法: 在gradlew 的同一目录,建立一个 gradle.prope ... 
- Lasso linear model实例 | Proliferation index | 评估单细胞的增殖指数
			背景:We developed a cell-cycle scoring approach that uses expression data to compute an index for ever ... 
- GenomicConsensus (quiver, arrow)使用方法 | 序列 consensus
			https://github.com/PacificBiosciences/GenomicConsensus GenomicConsensus 是pacbio开发的,我个人非常不喜欢pacbio开发的 ... 
- 请问WCF 跟 WebService之间的相同跟异同
			https://social.msdn.microsoft.com/Forums/zh-CN/c06420d1-69ba-4aa6-abe5-242e3213b68f/wcf-webservice W ... 
- Lab 1-4
			Analyze the file Lab01-04.exe. Questions and Short Answers Upload the Lab01-04.exe file to http://ww ... 
- LeetCode--026--删除排序数组中的重复项
			问题描述: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成 ... 
- p2739 Shuttle Puzzle
			观察样例得知就是和离'_'左边最近的'w'交换位置,然后和离'_'右边最近的'b'交换位置,轮流进行. #include <iostream> #include <cstdio> ... 
- gcc -02引起内存溢出'unsigned i'应修订为'volatile unsigned i'
			2017-12-13 10:44:19gcc -02引起内存溢出'unsigned i'应修订为'volatile unsigned i' 1.3.100 driver/char/random.cst ... 
- BigInteger 类 和 BigDecimal 类
			一 .BigInteger BigInteger类在计算和处理任意大小的整数方面是很有用的. BigInteger 任意大的整数,原则上是,只要你的计算机的内存足够大,可以有无限位的. BigInte ... 
- 141. Linked List Cycle&142. Linked List Cycle II(剑指Offer-链表中环的入口节点)
			题目: 141.Given a linked list, determine if it has a cycle in it. 142.Given a linked list, return the ... 
