zoukankan      html  css  js  c++  java
  • LeetCode#2.Add Two Numbers

    题目

    You are given two linked lists representing two non-negative numbers. 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.

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

    思想

    很简单的一道题,对于两个链表一次将每一项相加。考虑以下问题

    1.进位,取出十位加入下一个结果

    2.最后还剩下进位,新建一个节点

    3.有一个链表为空

    代码

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
            vector<int> resVec;
            int result=0;
            int carry=0;
            while (!(l1 == NULL&&l2 == NULL)) {
                if (l1 == NULL) {
                    result = l2->val+carry;
                    carry = result / 10;
                    result %= 10;
                    resVec.push_back(result);
                    l2 = l2->next;
                    continue;
                }
                if (l2 == NULL) {
                    result = l1->val + carry;
                    carry = result / 10;
                    result %= 10;
                    resVec.push_back(result);
                    l1 = l1->next;
                    continue;
                }
                if (l1 != NULL&&l2 != NULL) {
                    result = l1->val + l2->val+carry;
                    carry = result / 10;
                    result %= 10;
                    resVec.push_back(result);
                    l1 = l1->next;
                    l2 = l2->next;
                }
                
            }
            if (carry != 0) {
                resVec.push_back(carry);
            }
            ListNode* r = new ListNode(0);
            ListNode *k = r;
            for (int i = 0; i < resVec.size(); i++) {
                r->next = new ListNode(resVec[i]);
                r = r->next;
            }
            k = k->next;
            return k;
        }
    };

    注:为了方便生成结果链表,我这里把结果存在vector里,所以一共扫描两次,O(2n)。

    在讨论区发现的非常简洁优雅的代码

    class Solution {
    public:
        ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
            ListNode* head = new ListNode(0);
            ListNode* cur = head;
            int plus = 0;
            while (l1 || l2) {
                int num = (l1 ? l1->val : 0) + (l2 ? l2->val : 0) + plus;
                if (num >= 10) {
                    num -= 10;
                    plus = 1;
                } else plus = 0;
                cur->next = new ListNode(num);
                cur = cur->next;
                if (l1) l1 = l1->next;
                if (l2) l2 = l2->next;
            }
            if (plus) cur->next = new ListNode(1);
            return head->next;
        }
    };
  • 相关阅读:
    windows 安装 nodejs指定版本
    Xshell通过ssh登录远程服务器(秘钥)
    CentOS7 防火墙操作
    Linux系统MySQL开启远程连接
    PHP 下载图片文件并压缩文件成zip
    thinkphp 中更新数据字段,同时某字段值++操作(报错TP5.1不支持的数据表达式:[exp]的解决办法)
    layui的loading加载中
    Linux下面安装swoole
    windows 下cmd命令删除文件或者文件夹
    PHP 删除某目录下的全部文件
  • 原文地址:https://www.cnblogs.com/yatesxu/p/5546500.html
Copyright © 2011-2022 走看看