LeetCode OJ #2 Add Two Numbers

https://leetcode.com/problems/add-two-numbers/

题意:两个链表表示两个数 ,

2 4 3

5 6 4

按照加法的规则从左往右加以及 进位。

输出:和的链表。

仍然是水题。一些特殊情况需要考虑(这些做题的时候我考虑到了)

1、如果两个长度不等,如l1>l2  ,那么需要l1比l2多出来的部分加上进位copy到ans里

2、5+5的情况,虽然长度相等,但是最高位进位了

可惜,WA了两发,因为

正确的应该是

int v = l1->val + l2->val +add;
ans -> val = v%10;

我写成

int v = l1->val + l2->val;
ans -> val = v%10+add;

真尼玛二笔。。。另外数据结构确实不熟了,得写一写STL源码剖析了。。。

下面是测试程序

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;

 struct ListNode {
      int val;
      ListNode *next;
      ListNode(int x) : val(x), next(NULL) {}
  };

ListNode * a=NULL, * b=NULL;
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        int add = 0;
        ListNode* ans = 0;
        ListNode* ret=0;
        int f = 0;
        if(l1||l2)ans = new ListNode(0);
        while(l1 && l2){

            int v = l1->val + l2->val +add;
            //cout <<"DEBUG" <<  l1->val << "  " << l2->val << endl;
            if(f == 0){
                 ret = ans;
                 f++;
                 ans -> val = v%10;
            }else{
                ans -> next = new ListNode(0);
                ans = ans ->next;
                ans -> val = v%10;
                ans ->next = 0;
            }
            add = v/10;
            l1 = l1->next;
            l2 = l2->next;
        }
        //最后需要处理Add
        if(l1 || l2){
            ListNode* remain = l1 ? l1 : l2;
            while(remain){
                    //cout <<"DEBUG" <<  remain->val << endl;
                int v = remain->val+add;// + l2->val;
                ans->next = new ListNode(v%10);
                add = v/10;
                ans = ans->next;
                ans->next = NULL;

                remain = remain->next;
            }
        }
            if(add){
                ans->next = new ListNode(add);
                ans->next->next = 0;
            }
        return ret;
    }
};

int main(){
    //freopen("in.txt","r",stdin);
    string str1, str2;
    cin >> str1 >> str2;
    ListNode *sta=NULL, *stb=NULL;
    if(str1.size())sta = a = new ListNode(0);
    for(int i=0;i<str1.size();i++){
        a->val = str1[i]-'0';
        a->next = (i==str1.size()-1)?NULL:new ListNode(0);
        a = a->next;
    }
    if(str2.size())stb = b = new ListNode(0);
    for(int i=0;i<str2.size();i++){
        b->val = str2[i]-'0';
        b->next = (i == str2.size()-1)?NULL:new ListNode(0);
        b = b->next;
    }

    ListNode* ans = ((new Solution())->addTwoNumbers(sta,stb));
//ListNode* ans = NULL;
    while(ans){
        cout << ans->val << endl;
        ans = ans -> next;
    }
    return 0;
}
时间: 2024-10-13 08:22:45

LeetCode OJ #2 Add Two Numbers的相关文章

【LeetCode】002 Add Two Numbers

题目:LeetCode 002 Add Two Numbers 题意:给定表达非负数的两个链表,这些数字按照反向顺序存储,每个节点包含一个单独的数字,将这两个数相加,返回一个新链表. 样例: Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 链表每个节点的结构: 1 struct ListNode { 2 int val; 3 ListNode *next; 4 ListNode(int x) : val(

LeetCode No.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 ->

【Leetcode】2. Add Two Numbers

Problem: 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 tw

练习编程之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 ->

[Leetcode 2, Medium] Add Two Numbers

Problem: 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 ->

LeetCode Problem 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 ->

【Leetcode】445. Add Two Numbers II

You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first 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

【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

LeetCode OJ:Compare Version Numbers(比较版本字符串)

Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings are non-empty and contain only digits and the . character.The . characte