problem0002

package
v0.0.0-...-6d8a413 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 18, 2019 License: BSD-3-Clause Imports: 0 Imported by: 0

README

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.

Example:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

耗时

7:40~8:40 约 1 小时 实现了 反转,递归 还剩余一个递归生成 listnode 结果(在这里卡住了1个小时)

23:00~00:00

反转整数后再转 listnode

终于做出来了。。。

耗时:2个小时,性能非常差 // Benchmark_addTwoNumbers-8 2000000 745 ns/op

提交 LeetCode 后发现,我的测试用例未覆盖到比较多的 listnode 的情况,也就是无法转为整型的时候,不能用此方法来转换和实现。

宣告本题解答失败。。。(耗时3小时左右)

错误的原因是因为我未考虑到进位溢出和整数最大值的问题。

参考伪代码实现:07:37~07:50

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ListNode

type ListNode struct {
	Val  int
	Next *ListNode
}

[2] Add Two Numbers

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

algorithms Medium (30.7%) Total Accepted: 793.5K(793487) Total Submissions: 2.6M(2582883) Testcase Example: ` Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 = 807. `

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.

**Example:**

``` Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 = 807. ```

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL