zoukankan      html  css  js  c++  java
  • [leetcode-2-Add Two Numbers]

    You are given two non - empty linked lists representing two non - negative integers.
    The digits are stored in reverse order and each of their nodes contain a single digit.
    Add the two numbers and return it as a linked list.
    You may assume the two numbers do not contain any leading zero, except the number 0 itself.


    Input : (2 -> 4 -> 3) + (5 -> 6 -> 4)
    Output : 7 -> 0 -> 8

    刚开始ac的代码如下:简直是啰嗦到家

    就是两个链表从头到尾非空的情况下往前计算

    提防着进位等等 还要处理最后一个为空的边界条件

    运行效率简直惨不忍睹:先贴这儿。

    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2)
        {
            int carry = 0;//进位
            ListNode* result = l1;
            while (l1->next != NULL && l2->next != NULL)
            {
                l1->val = l1->val + l2->val + carry;                            
                if (l1->val >= 10)
                {
                    l1->val -= 10;
                    carry = 1;
                }
                else carry = 0;
    
                l1 = l1->next;
                l2 = l2->next;
            }
            l1->val = l1->val + l2->val + carry;
            if (l1->val >= 10)
            {
                l1->val -= 10;
                carry = 1;
            }
            else carry = 0;
            if (l1->next == NULL && l2->next == NULL)
            {
                if (carry == 1)
                {
                    ListNode* newNode = new ListNode(1);
                    l1->next = newNode;
                }
            }
            else
            {            
                if (l1->next == NULL &&  l2->next != NULL)//l2非空
                {            
                    l1->next = l2->next;
                    l1 = l1->next;
                    while (carry && l1->next!=NULL)
                    {
                        l1->val += carry;
                        if (l1->val >= 10)
                        {
                            l1->val -= 10;
                            carry = 1;
                        }
                        else carry = 0;
                        l1 = l1->next;
                    }
                    if (carry ==1)//最后一位
                    {
                        l1->val += carry;
                        if (l1->val >= 10)
                        {
                            l1->val -= 10;
                            ListNode* newNode = new ListNode(1);//新建结点
                            l1->next = newNode;
                            carry = 1;
                        }
                        else carry = 0;
                    }
                    
                }
                else if (l1->next != NULL && l2->next == NULL)
                {                
                    l1 = l1->next;
                    while (carry && l1->next != NULL)
                    {
                        l1->val += carry;
                        if (l1->val >= 10)
                        {
                            l1->val -= 10;
                            carry = 1;
                        }
                        else carry = 0;
                        l1 = l1->next;
                    }
                    if (carry == 1)//最后一位
                    {
                        l1->val += carry;
                        if (l1->val >= 10)
                        {
                            l1->val -= 10;
                            ListNode* newNode = new ListNode(1);//新建结点
                            l1->next = newNode;
                            carry = 1;
                        }
                        else carry = 0;
                    }
                }
            }    
            
            return result;
        }

    然后参考别人大牛的代码

    简洁到家啊~~~ 如下:

     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            ListNode c1 = l1;
            ListNode c2 = l2;
            ListNode sentinel = new ListNode(0);
            ListNode d = sentinel;
            int sum = 0;
            while (c1 != null || c2 != null) {
                sum /= 10;
                if (c1 != null) {
                    sum += c1.val;
                    c1 = c1.next;
                }
                if (c2 != null) {
                    sum += c2.val;
                    c2 = c2.next;
                }
                d.next = new ListNode(sum % 10);
                d = d.next;
            }
            if (sum / 10 == 1)
                d.next = new ListNode(1);
            return sentinel.next;
        }

    不光简洁 效率也还过得去

     
  • 相关阅读:
    Linux命令之 文件归档管理
    C#总结项目《影院售票系统》编写总结完结篇
    C#总结项目《影院售票系统》编写总结三
    C#中MD5加密
    C#中的序列化与反序列化
    C#总结项目《影院售票系统》编写总结二
    C#总结项目《影院售票系统》编写总结一
    java多线程与线程并发四:线程范围内的共享数据
    java多线程与线程并发三:线程同步通信
    java多线程与线程并发二:线程互斥
  • 原文地址:https://www.cnblogs.com/hellowooorld/p/6434143.html
Copyright © 2011-2022 走看看