Leetcode 2. ADD Numbers Swift

Source: Internet
Author: User

////Main.swift//LEETCODE02////Created by Guola on 16/1/21.//copyright©2016 year Guola. All rights reserved.//import Foundationfunc input ()-String {let keyboard=nsfilehandle.filehandlewithstandardinput () let Inputdata=Keyboard.availabledata Let Strdata= NSString (Data:inputdata, encoding:nsutf8stringencoding)!returnStrdata.stringbytrimmingcharactersinset (Nscharacterset.newlinecharacterset ())}classlistnode{varVal:intvarNext:listnode?Init (_ Num:int) {val=Num Next=Nil} func desc ()-string{varRet:string =String (val)varnode =Next whileNode! =Nil {ret= Ret.stringbyappendingformat ("->%d", (node?). val)!) Node=node!. Next}returnret}} Public classsolutionaddtwonumbers{func addtwonumbers (L1:listnode?, L2:listnode?) -listnode{let Ret:listnode= ListNode (-1)        varp1=L1varP2=L2varP:listnode =retifL1 = = Nil {ret.next = L2;returnret;} ifL2 = = Nil {ret.next = L1;returnret;} varP1count =0         whileP1! =Nil {P1=p1?. Next P1count++        }        varP2count =0         whileP2! =Nil {P2=p2?. Next P2count++        }                ifP2count >P1count {P1=L2 P2=L1}Else{P1=L1 P2=L2} whileP1! =Nil {ifP2 = =Nil {varsum=p1!. ValifP.next! =Nil {sum+ = (p.next!. val); } P.next=listnode (sum%Ten) P=p.next!ifsum/Ten>0{P.next = ListNode (1)} p1=p1?. Next} whileP2! =Nil {varsum=p1!. val+p2!. ValifP.next! =Nil {sum+ = (p.next!. val); } P.next=listnode (sum%Ten) P=p.next!ifsum/Ten>0{P.next = ListNode (1)} p1=p1!. Next P2=p2!. Next}} returnret.next!; }}func listnodecreate (str:string)-ListNode {let str:[string]= Str.componentsseparatedbystring (" -")    varList:listnode = ListNode (-1) let ret=List forSinchStr {list.next= ListNode (Int (s)!)) List=list.next!    }        returnret.next!}varTestcaseaddtownumber =solutionaddtwonumbers ()varL1:string =""Repeat{print ("Enter ListNode1: (Input exit,program end!)")//var l1 = "2->3->4"L1 =input ()ifL1 = ="Exit"{ Break}print ("Enter ListNode2:")//var L2 = "1->2->3"varL2 =input () let Nodel1=listnodecreate (L1) let Nodel2=listnodecreate (L2) print (Nodel1.desc ()) print (Nodel2.desc ())varTow =solutionaddtwonumbers ()varRET =tow.addtwonumbers (Nodel1, L2:nodel2) print (Ret.desc ())} whileL1! ="Exit"

Test input

1->8

0

Results: 1->8

0

7->3

Result 7->3

3
4->2
Results: 7 2

6
4->2
Results: 0 3

3->4->5
1->2->3
Results: 4->6->8

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

3->8->5
1->2->3
Results: 4->0->9

3->4->7
1->2->3
Results: 4->6->0->1

Leetcode 2. ADD Numbers Swift

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.