添加 problem 66、372

This commit is contained in:
YDZ
2019-08-15 19:13:03 +08:00
parent d5e6cbe41b
commit 79347c30ec
7 changed files with 264 additions and 2 deletions

View File

@@ -0,0 +1,21 @@
package leetcode
func plusOne(digits []int) []int {
if len(digits) == 0 {
return []int{}
}
carry := 1
for i := len(digits) - 1; i >= 0; i-- {
if digits[i]+carry > 9 {
digits[i] = 0
carry = 1
} else {
digits[i] += carry
carry = 0
}
}
if digits[0] == 0 && carry == 1 {
digits = append([]int{1}, digits...)
}
return digits
}

View File

@@ -0,0 +1,57 @@
package leetcode
import (
"fmt"
"testing"
)
type question66 struct {
para66
ans66
}
// para 是参数
// one 代表第一个参数
type para66 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans66 struct {
one []int
}
func Test_Problem66(t *testing.T) {
qs := []question66{
question66{
para66{[]int{1, 2, 3}},
ans66{[]int{1, 2, 4}},
},
question66{
para66{[]int{4, 3, 2, 1}},
ans66{[]int{4, 3, 2, 2}},
},
question66{
para66{[]int{9, 9}},
ans66{[]int{1, 0, 0}},
},
question66{
para66{[]int{0}},
ans66{[]int{0}},
},
}
fmt.Printf("------------------------Leetcode Problem 66------------------------\n")
for _, q := range qs {
_, p := q.ans66, q.para66
fmt.Printf("【input】:%v 【output】:%v\n", p, plusOne(p.one))
}
fmt.Printf("\n\n\n")
}

View File

@@ -0,0 +1,35 @@
# [66. Plus One](https://leetcode.com/problems/plus-one/)
## 题目:
Given a **non-empty** array of digits representing a non-negative integer, plus one to the integer.
The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.
You may assume the integer does not contain any leading zero, except the number 0 itself.
**Example 1:**
Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
**Example 2:**
Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.
## 题目大意
给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。你可以假设除了整数 0 之外,这个整数不会以零开头。
## 解题思路
- 给出一个数组,代表一个十进制数,数组的 0 下标是十进制数的高位。要求计算这个十进制数加一以后的结果。
- 简单的模拟题。从数组尾部开始往前扫,逐位进位即可。最高位如果还有进位需要在数组里面第 0 位再插入一个 1 。

View File

@@ -0,0 +1,57 @@
package leetcode
// 解法一 快速幂 res = res^10 * qpow(a, b[i])
// 模运算性质一:(a + b) % p = (a % p + b % p) % p
// 模运算性质二:(a - b) % p = (a % p - b % p + p) % p
// 模运算性质三:(a * b) % p = (a % p * b % p) % p
// 模运算性质四a ^ b % p = ((a % p)^b) % p
// 模运算性质五ab % p = ((a % p) * ( b % p)) % p, 其中 ab 是一个数字,如:287498374 等等
// 举个例子
// 12345^678 % 1337 = (12345^670 * 12345^8) % 1337
// = ((12345^670 % 1337) * (12345^8 % 1337)) % 1337 ---> 利用性质 三
// = (((12345^67)^10 % 1337) * (12345^8 % 1337)) % 1337 ---> 乘方性质
// = ((12345^67 % 1337)^10) % 1337 * (12345^8 % 1337)) % 1337 ---> 利用性质 四
// = (((12345^67 % 1337)^10) * (12345^8 % 1337)) % 1337 ---> 反向利用性质 三
func superPow(a int, b []int) int {
res := 1
for i := 0; i < len(b); i++ {
res = (qpow(res, 10) * qpow(a, b[i])) % 1337
}
return res
}
// 快速幂计算 x^n
func qpow(x, n int) int {
res := 1
x %= 1337
for n > 0 {
if (n & 1) == 1 {
res = (res * x) % 1337
}
x = (x * x) % 1337
n >>= 1
}
return res
}
// 解法二 暴力解法
// 利用上面的性质可以得到a^1234567 % 1337 = (a^1234560 % 1337) * (a^7 % 1337) % k = ((((a^123456) % 1337)^10)% 1337 * (a^7 % 1337))% 1337;
func superPow1(a int, b []int) int {
if len(b) == 0 {
return 1
}
last := b[len(b)-1]
l := 1
// 先计算个位的 a^x 结果,对应上面例子中的 (a^7 % 1337)% 1337
for i := 1; i <= last; i++ {
l = l * a % 1337
}
// 再计算除去个位以外的 a^y 的结果,对应上面例子中的 (a^123456) % 1337)
temp := superPow1(a, b[:len(b)-1])
f := 1
// 对应上面例子中的 (((a^123456) % 1337)^10)% 1337
for i := 1; i <= 10; i++ {
f = f * temp % 1337
}
return f * l % 1337
}

View File

@@ -0,0 +1,49 @@
package leetcode
import (
"fmt"
"testing"
)
type question372 struct {
para372
ans372
}
// para 是参数
// one 代表第一个参数
type para372 struct {
a int
b []int
}
// ans 是答案
// one 代表第一个答案
type ans372 struct {
one int
}
func Test_Problem372(t *testing.T) {
qs := []question372{
question372{
para372{2, []int{3}},
ans372{8},
},
question372{
para372{2, []int{1, 0}},
ans372{1024},
},
// 如需多个测试,可以复制上方元素。
}
fmt.Printf("------------------------Leetcode Problem 372------------------------\n")
for _, q := range qs {
_, p := q.ans372, q.para372
fmt.Printf("【input】:%v 【output】:%v\n", p, superPow(p.a, p.b))
}
fmt.Printf("\n\n\n")
}

View File

@@ -0,0 +1,43 @@
# [372. Super Pow](https://leetcode.com/problems/super-pow/)
## 题目:
Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.
**Example 1:**
Input: a = 2, b = [3]
Output: 8
**Example 2:**
Input: a = 2, b = [1,0]
Output: 1024
## 题目大意
你的任务是计算 a^b 对 1337 取模a 是一个正整数b 是一个非常大的正整数且会以数组形式给出。
## 解题思路
- 求 a^b mod p 的结果b 是大数。
- 这一题可以用暴力解法尝试。需要用到 mod 计算的几个运算性质:
模运算性质一:(a + b) % p = (a % p + b % p) % p
模运算性质二:(a - b) % p = (a % p - b % p + p) % p
模运算性质三:(a * b) % p = (a % p * b % p) % p
模运算性质四a ^ b % p = ((a % p)^b) % p
模运算性质五ab % p = ((a % p) * ( b % p)) % p, 其中 ab 是一个数字,如:287498374 等等
这一题需要用到性质三、四、五。举个例子:
12345^678 % 1337 = (12345^670 * 12345^8) % 1337
= ((12345^670 % 1337) * (12345^8 % 1337)) % 1337 ---> 利用性质 三
= (((12345^67)^10 % 1337) * (12345^8 % 1337)) % 1337 ---> 乘方性质
= ((12345^67 % 1337)^10) % 1337 * (12345^8 % 1337)) % 1337 ---> 利用性质 四
= (((12345^67 % 1337)^10) * (12345^8 % 1337)) % 1337 ---> 反向利用性质 三
经过上面这样的变换,把指数 678 的个位分离出来了,可以单独求解。继续经过上面的变换,可以把指数的 6 和 7 也分离出来。最终可以把大数 b 一位一位的分离出来。至于计算 a^b 就结果快速幂求解。

View File

@@ -129,7 +129,7 @@
| 0063 | Unique Paths II | [Go](https://github.com/halfrost/LeetCode-Go/tree/master/Algorithms/0063.%20Unique%20Paths%20II) | 33.50% | Medium | |
| 0064 | Minimum Path Sum | [Go](https://github.com/halfrost/LeetCode-Go/tree/master/Algorithms/0064.%20Minimum%20Path%20Sum) | 47.30% | Medium | |
| 0065 | Valid Number | | 14.10% | Hard | |
| 0066 | Plus One | | 41.40% | Easy | |
| 0066 | Plus One | [Go](https://github.com/halfrost/LeetCode-Go/tree/master/Algorithms/0066.%20Plus%20One) | 41.40% | Easy | |
| 0067 | Add Binary | | 39.50% | Easy | |
| 0068 | Text Justification | | 23.50% | Hard | |
| 0069 | Sqrt(x) | [Go](https://github.com/halfrost/LeetCode-Go/tree/master/Algorithms/0069.%20Sqrt(x)) | 31.50% | Easy | |
@@ -435,7 +435,7 @@
| 0369 | Plus One Linked List | | 56.40% | Medium | |
| 0370 | Range Addition | | 60.60% | Medium | |
| 0371 | Sum of Two Integers |[Go](https://github.com/halfrost/LeetCode-Go/tree/master/Algorithms/0371.%20Sum%20of%20Two%20Integers) | 50.90% | Easy | |
| 0372 | Super Pow | | 35.70% | Medium | |
| 0372 | Super Pow | [Go](https://github.com/halfrost/LeetCode-Go/tree/master/Algorithms/0372.%20Super%20Pow) | 35.70% | Medium | |
| 0373 | Find K Pairs with Smallest Sums | | 34.00% | Medium | |
| 0374 | Guess Number Higher or Lower | | 39.60% | Easy | |
| 0375 | Guess Number Higher or Lower II | | 37.80% | Medium | |