//
// main.swift
// leetcode02
//
// Created by GuoLa on 16/1/21.
// Copyright © 2016年 GuoLa. All rights reserved.
// import Foundation func input() -> String {
let keyboard = NSFileHandle.fileHandleWithStandardInput()
let inputData = keyboard.availableData
let strData = NSString(data: inputData, encoding: NSUTF8StringEncoding)!
return strData.stringByTrimmingCharactersInSet(NSCharacterSet.newlineCharacterSet())
} class ListNode{
var val:Int
var next:ListNode?
init(_ num: Int)
{
val=num
next=nil
} func desc()->String{ var ret:String = String(val)
var node = next
while node != nil {
ret = ret.stringByAppendingFormat("->%d", (node?.val)!)
node=node!.next
}
return ret
}
} public class SolutionAddTwoNumbers{
func addTwoNumbers(l1: ListNode?,l2: ListNode?) ->ListNode{
let ret:ListNode = ListNode(-)
var p1=l1
var p2=l2
var p:ListNode = ret if l1 == nil { ret.next = l2; return ret;}
if l2 == nil { ret.next = l1; return ret;} var p1count =
while p1 != nil {
p1=p1?.next
p1count++
}
var p2count =
while p2 != nil {
p2=p2?.next
p2count++
} if p2count > p1count {
p1=l2
p2=l1
}else
{
p1=l1
p2=l2
} while p1 != nil { if p2 == nil {
var sum=p1!.val
if p.next != nil {
sum+=(p.next!.val);
}
p.next=ListNode(sum%)
p=p.next! if sum/ > { p.next = ListNode() }
p1=p1?.next }
while p2 != nil {
var sum=p1!.val+p2!.val
if p.next != nil {
sum+=(p.next!.val);
} p.next=ListNode(sum%)
p=p.next! if sum/ > { p.next = ListNode() } p1=p1!.next
p2=p2!.next }
} return ret.next!;
}
} func ListNodeCreate(str: String) ->ListNode {
let str:[String] = str.componentsSeparatedByString("->")
var list:ListNode = ListNode(-)
let ret = list for s in str {
list.next = ListNode(Int(s)!)
list=list.next!
} return ret.next!
} var testCaseAddTowNumber = SolutionAddTwoNumbers() var l1:String = ""
repeat{ print("Enter ListNode1:(Input exit,Program end!)") //var l1 = "2->3->4"
l1 = input()
if l1 == "exit" {break} print("Enter ListNode2:")
//var l2 = "1->2->3"
var l2 = input() let nodel1 = ListNodeCreate(l1)
let nodel2 = ListNodeCreate(l2) print(nodel1.desc())
print(nodel2.desc()) var tow = SolutionAddTwoNumbers()
var ret = tow.addTwoNumbers(nodel1, l2: nodel2) print(ret.desc())
} while l1 != "exit"

测试输入

1->8

0

结果:1->8

0

7->3

结果 7->3

3
4->2
结果:7 2

6
4->2
结果:0 3

3->4->5
1->2->3
结果:4->6->8

9->4->5
1->2->3
结果:0->7->8

3->8->5
1->2->3
结果:4->0->9

3->4->7
1->2->3
结果:4->6->0->1

LeetCode 2. Add Two Numbers swift的更多相关文章

  1. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  2. LeetCode:1. Add Two Numbers

    题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...

  3. [LeetCode] 445. Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  4. LeetCode 面试:Add Two Numbers

    1 题目 You are given two linked lists representing two non-negative numbers. The digits are stored in ...

  5. LeetCode #002# Add Two Numbers(js描述)

    索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...

  6. [Leetcode Week15] Add Two Numbers

    Add Two Numbers 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/add-two-numbers/description/ Descrip ...

  7. [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现

    [LeetCode] Add Two Numbers 两个数字相加   You are given two non-empty linked lists representing two non-ne ...

  8. [LeetCode] 2. Add Two Numbers 两个数字相加

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  9. LeetCode之Add Two Numbers

    Add Two Numbers 方法一: 考虑到有进位的问题,首先想到的思路是: 先分位求总和得到 totalsum,然后再将totalsum按位拆分转成链表: ListNode* addTwoNum ...

随机推荐

  1. OC self = [super init] , 点语法 , @property

    OC self = [super init] , 点语法 , @property 构造方法为啥这么写? self = [super init]; [super init] 的结果可能有三种: 第一种: ...

  2. WPExpress78_update 离线包

    http://115.com/lb/5lb7rnbr#Windows Phone SDK update for WP 7.8115网盘礼包码:5lb7rnbr 使用/layout下载的,相信很多朋友已 ...

  3. FusionCharts的使用方法(php)

    我们公司一直用这个图表统计, 最近整理了一下相关文档,提供大家学习. 首先可以看看 http://www.cnblogs.com/xuhongfei/archive/2013/04/12/301688 ...

  4. Git的常用操作,记录下

    首先生成密钥 ssh-keygen -t rsa -C "your_email@youremail.com" 然后打开workdir,敲 git init 设置一个远端库 git ...

  5. 用了星型转换的sql跑了5小时--->5mins的过程

    =================START================================ BI数据仓库环境里面跑着一个crontab job,一旦sql运行超过4hours,就会接 ...

  6. 海量IT资料 + 各种平台下的Oracle安装文件 + 公开课录像 + 各种视频教程资料

    觉得老师的公开课讲的都挺好的,这里把我录的一些公开课视频分享给大家,都用的是<屏幕录像专家>来录制的,是lxe格式的,大家用这个软件来播放就可以了,后边的公开课录像文件也会慢慢添加进去的, ...

  7. 3、eclipse和maven环境安装以及HDFS读写的demo

    安装eclipse和maven环境   $ mkdir /home/beifeng/.m2 $ tar zxf repository.tar.gz -C /home/beifeng/.m2 $ /co ...

  8. sql server 2008 相关基础(物理备份还原)

    1. restore database KLJY_juncsoft from disk= 'D:\\kljy_2012_12_28.bak' with recovery 注意在还原时,进度一直显示0% ...

  9. Remote Desktop File Format

    转自:http://engrmosaic.uncc.edu/mosaic-anywhere/remote-desktop-file-format The new Terminal Services c ...

  10. 漏洞科普:对于XSS和CSRF你究竟了解多少

    转自:http://www.freebuf.com/articles/web/39234.html 随着Web2.0.社交网络.微博等等一系列新型的互联网产品的诞生,基于Web环境的互联网应用越来越广 ...