LeetCode-Add Two Numbers_002

最容易想到的就是把链表里面的数取出来按倒序组成数,然后两个链表中的数字相加得到一个新数

再把这个数拆开放到链表里面。 注意要用long 型,用int 型会溢出。这个方法如果输入的数字再多一点就没法求了,毕竟long型也是有限的。运行了40ms

ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
    ListNode *result,*tempnode,*prenode;
	ListNode *l1_current = l1;
	ListNode *l2_current = l2;
	int p = 0;
	long sum1 = 0;
	long sum2 = 0;
	long sum3 = 0;
	vector<int> ivec;
	while (l1_current != NULL )
	{
		sum1 += l1_current->val*pow(10, p);
		++p;
		l1_current = l1_current->next;

	}
	p = 0;
	while (l2_current != NULL)
	{
		sum2 += l2_current->val*pow(10, p);
		++p;
		l2_current = l2_current->next;
	}
	sum3 = sum1 + sum2;
	long s = sum3;
	result = new ListNode(s % 10);
	prenode = result;
	s /= 10;
	while(s)
	{
        tempnode = new ListNode( s % 10);
        prenode->next = tempnode;
        prenode = tempnode;
        s /= 10;
	}

	return result;
    }

还有一种方法就是一位一位的加,每次加的时候保存一个进位数字,我用的是容器保存,代码也没有简化,最容易理解。这个运行了48ms

  ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
   ListNode *result = NULL,*tempnode,*prenode;
	ListNode *l1_current = l1;
	ListNode *l2_current = l2;
	vector<int> ivec;
	vector<int> ivec1;
	vector<int> ivec2;
	while (l1_current != NULL)
	{
		ivec1.push_back(l1_current->val);
		l1_current = l1_current->next;
	}
	while (l2_current != NULL)
	{
		ivec2.push_back(l2_current->val);
		l2_current = l2_current->next;
	}

	int cnt = 0;
	int i = 0;
	for (; i != ivec1.size() && i != ivec2.size(); ++i)
	{
		ivec.push_back((ivec1[i] + ivec2[i] + cnt) % 10);
		cnt = (ivec1[i] + ivec2[i] + cnt) / 10;
	}
	if (ivec1.size() <= ivec2.size())
	{
		for (i; i != ivec2.size(); ++i)
		{
			ivec.push_back((ivec2[i] + cnt) % 10);
			cnt = (ivec2[i] + cnt) / 10;
		}
	}
	else
	{
		for (i; i != ivec1.size(); ++i)
		{
			ivec.push_back((ivec1[i] + cnt) % 10);
			cnt = (ivec1[i] + cnt) / 10;
		}
	}

	if (cnt == 1)
		ivec.push_back(1);

	if (ivec.size() > 0)
	{
		tempnode = new ListNode(ivec[0]);
		result = tempnode;
		prenode = tempnode;
	}
	else
	{
		return NULL;
	}

	for (int i = 1; i != ivec.size(); ++i)
	{
		tempnode = new ListNode(ivec[i]);
		prenode->next = tempnode;
		prenode = tempnode;
	}

	return result;
    }

如果简化后应该是这样的:

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        if (listLength(l1) < listLength(l2))
            return addTwoNumbers(l2, l1);
        ListNode *r1 = l1, *r2 = l2;
        int c = 0;
        bool isEnd = false;
        while (r2) {
            int val = r1 -> val + r2 -> val + c;
            r1 -> val = val % 10;
            c = val / 10;
            if (r1 -> next) r1 = r1 -> next;
            else isEnd = true;
            r2 = r2 -> next;
        }
        while (c) {
            int val = isEnd ? c : r1 -> val + c;
            if (isEnd) r1 -> next = new ListNode(val % 10);
            else r1 -> val = val % 10;
            c = val / 10;
            if (r1 -> next) r1 = r1 -> next;
            else isEnd = true;
        }
        return l1;
    }
private:
    int listLength(ListNode* head) {
        return head ? 1 + listLength(head -> next) : 0;
    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-22 03:28:31

LeetCode-Add Two Numbers_002的相关文章

LeetCode: 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——Add Binary

Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 求数字字符串的二进制和.同之前的数组代表数字,两个数组相加一样,只不过进位变成了2.可能两个串的长度不一样,故逆转,从左到右加下去,最后再逆转. public static String addBinary(String a,

[leetcode]Add Binary @ Python

原题地址:https://oj.leetcode.com/problems/add-binary/ 题意: Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 解题思路:提供两种实现方式吧. 代码一: class Solution: # @param a, a string # @pa

LeetCode——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——Add Two Numbers 两个链表表示的正整数对其求和(AC)

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] [Add Binary 2012-04-02 ]

Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". string 的操作,短string补位.两个"0"会输出一个"00",要特殊处理,plus如果最后为"1",要补上. ? 1 2 3 4 5 6 7 8 9 10 1

[LeetCode] Add Digits

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it. 分析一:最简单的循环方法 class Solution { public:

[LeetCode] Add Strings 字符串相加

Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. Note: The length of both num1 and num2 is < 5100. Both num1 and num2 contains only digits 0-9. Both num1 and num2 does not contain any leading zero.

LeetCode: Add Two Numbers 解题报告

Add Two NumbersYou 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] 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 ->