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 ...
随机推荐
- 日常英语---四、vis.js是什么
日常英语---四.vis.js是什么 一.总结 一句话总结:A dynamic, browser based visualization library. 动态基于浏览器的可视库 http://vis ...
- RHEL 5 , 用安装CD作为YUM的Repository
官方文档写的非常好 14.5. Upgrading the System Off-line with ISO and Yum Create a target directory to mount yo ...
- 雷林鹏分享:C# 数组(Array)
C# 数组(Array) 数组是一个存储相同类型元素的固定大小的顺序集合.数组是用来存储数据的集合,通常认为数组是一个同一类型变量的集合. 声明数组变量并不是声明 number0.number1... ...
- C#生成XSD规范
首先在开始菜单中找到:Visual Studio 2005 命令提示 大柏树按:VS2010在:开始—> Microsoft Visual Studio 2010 —> Visual St ...
- PCM数据格式
PCM数据格式 1. 音频简介 经常见到这样的描述: 44100HZ 16bit stereo 或者 22050HZ 8bit mono 等等. 44100HZ 16bit ster ...
- 20170923xlVBA_UpdateClientDetailSQL_Dictionary
Sub UpdateClientDetailWGQ() Dim Wb As Workbook Dim Sht As Worksheet Dim Rng As Range Dim Arr As Vari ...
- android -------- 打开本地浏览器或指定浏览器加载,打电话,打开第三方app
开发中常常有打开本地浏览器加载url或者指定浏览器加载, 还有打开第三方app, 如 打开高德地图 百度地图等 在Android程序中我们可以通过发送隐式Intent来启动系统默认的浏览器. 如果手机 ...
- sqlserver 优化
避免在索引列上使用计算 where子句中,如果索引列是函数的一部分,优化器将不使用索引而使用全表扫描.例如: (低效)select ... from [dept] where [sal]*12> ...
- Leetcode 144
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- 重写nyoj2——括号匹配
#include "bits/stdc++.h" using namespace std; int comp(char s1,char s2){ ; ; } int main() ...