规范格式

This commit is contained in:
YDZ
2020-08-07 15:50:06 +08:00
parent 854a339abc
commit 4e11f4028a
1438 changed files with 907 additions and 924 deletions

View File

@ -0,0 +1,69 @@
package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// ListNode define
type ListNode = structures.ListNode
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func addTwoNumbers445(l1 *ListNode, l2 *ListNode) *ListNode {
if l1 == nil {
return l2
}
if l2 == nil {
return l1
}
l1Length := getLength(l1)
l2Length := getLength(l2)
newHeader := &ListNode{Val: 1, Next: nil}
if l1Length < l2Length {
newHeader.Next = addNode(l2, l1, l2Length-l1Length)
} else {
newHeader.Next = addNode(l1, l2, l1Length-l2Length)
}
if newHeader.Next.Val > 9 {
newHeader.Next.Val = newHeader.Next.Val % 10
return newHeader
}
return newHeader.Next
}
func addNode(l1 *ListNode, l2 *ListNode, offset int) *ListNode {
if l1 == nil {
return nil
}
var (
res, node *ListNode
)
if offset == 0 {
res = &ListNode{Val: l1.Val + l2.Val, Next: nil}
node = addNode(l1.Next, l2.Next, 0)
} else {
res = &ListNode{Val: l1.Val, Next: nil}
node = addNode(l1.Next, l2, offset-1)
}
if node != nil && node.Val > 9 {
res.Val++
node.Val = node.Val % 10
}
res.Next = node
return res
}
func getLength(l *ListNode) int {
count := 0
cur := l
for cur != nil {
count++
cur = cur.Next
}
return count
}

View File

@ -0,0 +1,81 @@
package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question445 struct {
para445
ans445
}
// para 是参数
// one 代表第一个参数
type para445 struct {
one []int
another []int
}
// ans 是答案
// one 代表第一个答案
type ans445 struct {
one []int
}
func Test_Problem445(t *testing.T) {
qs := []question445{
question445{
para445{[]int{}, []int{}},
ans445{[]int{}},
},
question445{
para445{[]int{1}, []int{1}},
ans445{[]int{2}},
},
question445{
para445{[]int{1, 2, 3, 4}, []int{1, 2, 3, 4}},
ans445{[]int{2, 4, 6, 8}},
},
question445{
para445{[]int{1, 2, 3, 4, 5}, []int{1, 2, 3, 4, 5}},
ans445{[]int{2, 4, 6, 9, 0}},
},
question445{
para445{[]int{1}, []int{9, 9, 9, 9, 9}},
ans445{[]int{1, 0, 0, 0, 0, 0}},
},
question445{
para445{[]int{9, 9, 9, 9, 9}, []int{1}},
ans445{[]int{1, 0, 0, 0, 0, 0}},
},
question445{
para445{[]int{2, 4, 3}, []int{5, 6, 4}},
ans445{[]int{8, 0, 7}},
},
question445{
para445{[]int{1, 8, 3}, []int{7, 1}},
ans445{[]int{2, 5, 4}},
},
// 如需多个测试,可以复制上方元素。
}
fmt.Printf("------------------------Leetcode Problem 445------------------------\n")
for _, q := range qs {
_, p := q.ans445, q.para445
fmt.Printf("【input】:%v 【output】:%v\n", p, structures.List2Ints(addTwoNumbers445(structures.Ints2List(p.one), structures.Ints2List(p.another))))
}
fmt.Printf("\n\n\n")
}

View File

@ -0,0 +1,25 @@
# [445. Add Two Numbers II](https://leetcode.com/problems/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 do not contain any leading zero, except the number 0 itself.
Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.
Example:
```text
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7
```
## 题目大意
这道题是第 2 题的变种题,第 2 题中的 2 个数是从个位逆序排到高位这样相加只用从头交到尾进位一直进位即可。这道题目中强制要求不能把链表逆序。2 个数字是从高位排到低位的,这样进位是倒着来的。
## 解题思路
思路也不难,加法只用把短的链表依次加到长的链表上面来就可以了,最终判断一下最高位有没有进位,有进位再往前进一位。加法的过程可以用到递归。