mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2026-03-13 10:02:05 +08:00
20
leetcode/0009.Palindrome-Number/9. Palindrome Number.go
Normal file
20
leetcode/0009.Palindrome-Number/9. Palindrome Number.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package leetcode
|
||||
|
||||
import "strconv"
|
||||
|
||||
func isPalindrome(x int) bool {
|
||||
if x < 0 {
|
||||
return false
|
||||
}
|
||||
if x < 10 {
|
||||
return true
|
||||
}
|
||||
s := strconv.Itoa(x)
|
||||
length := len(s)
|
||||
for i := 0; i <= length/2; i++ {
|
||||
if s[i] != s[length-1-i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
72
leetcode/0009.Palindrome-Number/9. Palindrome Number_test.go
Normal file
72
leetcode/0009.Palindrome-Number/9. Palindrome Number_test.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question9 struct {
|
||||
para9
|
||||
ans9
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para9 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans9 struct {
|
||||
one bool
|
||||
}
|
||||
|
||||
func Test_Problem9(t *testing.T) {
|
||||
|
||||
qs := []question9{
|
||||
|
||||
question9{
|
||||
para9{121},
|
||||
ans9{true},
|
||||
},
|
||||
|
||||
question9{
|
||||
para9{-121},
|
||||
ans9{false},
|
||||
},
|
||||
|
||||
question9{
|
||||
para9{10},
|
||||
ans9{false},
|
||||
},
|
||||
|
||||
question9{
|
||||
para9{321},
|
||||
ans9{false},
|
||||
},
|
||||
|
||||
question9{
|
||||
para9{-123},
|
||||
ans9{false},
|
||||
},
|
||||
|
||||
question9{
|
||||
para9{120},
|
||||
ans9{false},
|
||||
},
|
||||
|
||||
question9{
|
||||
para9{1534236469},
|
||||
ans9{false},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 9------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans9, q.para9
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p.one, isPalindrome(p.one))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
69
leetcode/0009.Palindrome-Number/README.md
Normal file
69
leetcode/0009.Palindrome-Number/README.md
Normal file
@@ -0,0 +1,69 @@
|
||||
# [9. Palindrome Number](https://leetcode.com/problems/palindrome-number/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: 121
|
||||
Output: true
|
||||
```
|
||||
|
||||
**Example 2**:
|
||||
|
||||
```
|
||||
Input: -121
|
||||
Output: false
|
||||
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
|
||||
```
|
||||
|
||||
**Example 3**:
|
||||
|
||||
```
|
||||
Input: 10
|
||||
Output: false
|
||||
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
|
||||
```
|
||||
|
||||
**Follow up**:
|
||||
|
||||
Coud you solve it without converting the integer to a string?
|
||||
|
||||
## 题目大意
|
||||
|
||||
判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 判断一个整数是不是回文数。
|
||||
- 简单题。注意会有负数的情况,负数,个位数,10 都不是回文数。其他的整数再按照回文的规则判断。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
import "strconv"
|
||||
|
||||
func isPalindrome(x int) bool {
|
||||
if x < 0 {
|
||||
return false
|
||||
}
|
||||
if x < 10 {
|
||||
return true
|
||||
}
|
||||
s := strconv.Itoa(x)
|
||||
length := len(s)
|
||||
for i := 0; i <= length/2; i++ {
|
||||
if s[i] != s[length-1-i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
```
|
||||
29
leetcode/0013.Roman-to-Integer/13. Roman to Integer.go
Normal file
29
leetcode/0013.Roman-to-Integer/13. Roman to Integer.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package leetcode
|
||||
|
||||
var roman = map[string]int{
|
||||
"I": 1,
|
||||
"V": 5,
|
||||
"X": 10,
|
||||
"L": 50,
|
||||
"C": 100,
|
||||
"D": 500,
|
||||
"M": 1000,
|
||||
}
|
||||
|
||||
func romanToInt(s string) int {
|
||||
if s == "" {
|
||||
return 0
|
||||
}
|
||||
num, lastint, total := 0, 0, 0
|
||||
for i := 0; i < len(s); i++ {
|
||||
char := s[len(s)-(i+1) : len(s)-i]
|
||||
num = roman[char]
|
||||
if num < lastint {
|
||||
total = total - num
|
||||
} else {
|
||||
total = total + num
|
||||
}
|
||||
lastint = num
|
||||
}
|
||||
return total
|
||||
}
|
||||
67
leetcode/0013.Roman-to-Integer/13. Roman to Integer_test.go
Normal file
67
leetcode/0013.Roman-to-Integer/13. Roman to Integer_test.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question13 struct {
|
||||
para13
|
||||
ans13
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para13 struct {
|
||||
one string
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans13 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem13(t *testing.T) {
|
||||
|
||||
qs := []question13{
|
||||
|
||||
question13{
|
||||
para13{"III"},
|
||||
ans13{3},
|
||||
},
|
||||
|
||||
question13{
|
||||
para13{"IV"},
|
||||
ans13{4},
|
||||
},
|
||||
|
||||
question13{
|
||||
para13{"IX"},
|
||||
ans13{9},
|
||||
},
|
||||
|
||||
question13{
|
||||
para13{"LVIII"},
|
||||
ans13{58},
|
||||
},
|
||||
|
||||
question13{
|
||||
para13{"MCMXCIV"},
|
||||
ans13{1994},
|
||||
},
|
||||
|
||||
question13{
|
||||
para13{"MCMXICIVI"},
|
||||
ans13{2014},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 13------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans13, q.para13
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p.one, romanToInt(p.one))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
132
leetcode/0013.Roman-to-Integer/README.md
Normal file
132
leetcode/0013.Roman-to-Integer/README.md
Normal file
@@ -0,0 +1,132 @@
|
||||
# [13. Roman to Integer](https://leetcode.com/problems/roman-to-integer/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Roman numerals are represented by seven different symbols: `I`, `V`, `X`, `L`, `C`, `D` and `M`.
|
||||
|
||||
```
|
||||
Symbol Value
|
||||
I 1
|
||||
V 5
|
||||
X 10
|
||||
L 50
|
||||
C 100
|
||||
D 500
|
||||
M 1000
|
||||
```
|
||||
|
||||
For example, two is written as `II` in Roman numeral, just two one's added together. Twelve is written as, `XII`, which is simply `X` + `II`. The number twenty seven is written as `XXVII`, which is `XX` + `V` + `II`.
|
||||
|
||||
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not `IIII`. Instead, the number four is written as `IV`. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as `IX`. There are six instances where subtraction is used:
|
||||
|
||||
- `I` can be placed before `V` (5) and `X` (10) to make 4 and 9.
|
||||
- `X` can be placed before `L` (50) and `C` (100) to make 40 and 90.
|
||||
- `C` can be placed before `D` (500) and `M` (1000) to make 400 and 900.
|
||||
|
||||
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: "III"
|
||||
Output: 3
|
||||
```
|
||||
|
||||
**Example 2**:
|
||||
|
||||
```
|
||||
Input: "IV"
|
||||
Output: 4
|
||||
```
|
||||
|
||||
**Example 3**:
|
||||
|
||||
```
|
||||
Input: "IX"
|
||||
Output: 9
|
||||
```
|
||||
|
||||
**Example 4**:
|
||||
|
||||
```
|
||||
Input: "LVIII"
|
||||
Output: 58
|
||||
Explanation: L = 50, V= 5, III = 3.
|
||||
```
|
||||
|
||||
**Example 5**:
|
||||
|
||||
```
|
||||
Input: "MCMXCIV"
|
||||
Output: 1994
|
||||
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
|
||||
```
|
||||
|
||||
## 题目大意
|
||||
|
||||
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。
|
||||
|
||||
```go
|
||||
|
||||
字符 数值
|
||||
I 1
|
||||
V 5
|
||||
X 10
|
||||
L 50
|
||||
C 100
|
||||
D 500
|
||||
M 1000
|
||||
|
||||
```
|
||||
|
||||
例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做 XXVII, 即为 XX + V + II 。
|
||||
|
||||
通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:
|
||||
|
||||
- I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
|
||||
- X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。
|
||||
- C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。
|
||||
|
||||
给定一个罗马数字,将其转换成整数。输入确保在 1 到 3999 的范围内。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给定一个罗马数字,将其转换成整数。输入确保在 1 到 3999 的范围内。
|
||||
- 简单题。按照题目中罗马数字的字符数值,计算出对应罗马数字的十进制数即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
var roman = map[string]int{
|
||||
"I": 1,
|
||||
"V": 5,
|
||||
"X": 10,
|
||||
"L": 50,
|
||||
"C": 100,
|
||||
"D": 500,
|
||||
"M": 1000,
|
||||
}
|
||||
|
||||
func romanToInt(s string) int {
|
||||
if s == "" {
|
||||
return 0
|
||||
}
|
||||
num, lastint, total := 0, 0, 0
|
||||
for i := 0; i < len(s); i++ {
|
||||
char := s[len(s)-(i+1) : len(s)-i]
|
||||
num = roman[char]
|
||||
if num < lastint {
|
||||
total = total - num
|
||||
} else {
|
||||
total = total + num
|
||||
}
|
||||
lastint = num
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
```
|
||||
38
leetcode/0067.Add-Binary/67. Add Binary.go
Normal file
38
leetcode/0067.Add-Binary/67. Add Binary.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func addBinary(a string, b string) string {
|
||||
if len(b) > len(a) {
|
||||
a, b = b, a
|
||||
}
|
||||
|
||||
res := make([]string, len(a)+1)
|
||||
i, j, k, c := len(a)-1, len(b)-1, len(a), 0
|
||||
for i >= 0 && j >= 0 {
|
||||
ai, _ := strconv.Atoi(string(a[i]))
|
||||
bj, _ := strconv.Atoi(string(b[j]))
|
||||
res[k] = strconv.Itoa((ai + bj + c) % 2)
|
||||
c = (ai + bj + c) / 2
|
||||
i--
|
||||
j--
|
||||
k--
|
||||
}
|
||||
|
||||
for i >= 0 {
|
||||
ai, _ := strconv.Atoi(string(a[i]))
|
||||
res[k] = strconv.Itoa((ai + c) % 2)
|
||||
c = (ai + c) / 2
|
||||
i--
|
||||
k--
|
||||
}
|
||||
|
||||
if c > 0 {
|
||||
res[k] = strconv.Itoa(c)
|
||||
}
|
||||
|
||||
return strings.Join(res, "")
|
||||
}
|
||||
48
leetcode/0067.Add-Binary/67. Add Binary_test.go
Normal file
48
leetcode/0067.Add-Binary/67. Add Binary_test.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question67 struct {
|
||||
para67
|
||||
ans67
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para67 struct {
|
||||
a string
|
||||
b string
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans67 struct {
|
||||
one string
|
||||
}
|
||||
|
||||
func Test_Problem67(t *testing.T) {
|
||||
|
||||
qs := []question67{
|
||||
|
||||
question67{
|
||||
para67{"11", "1"},
|
||||
ans67{"100"},
|
||||
},
|
||||
|
||||
question67{
|
||||
para67{"1010", "1011"},
|
||||
ans67{"10101"},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 67------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans67, q.para67
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, addBinary(p.a, p.b))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
76
leetcode/0067.Add-Binary/README.md
Normal file
76
leetcode/0067.Add-Binary/README.md
Normal file
@@ -0,0 +1,76 @@
|
||||
# [67. Add Binary](https://leetcode.com/problems/add-binary/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given two binary strings, return their sum (also a binary string).
|
||||
|
||||
The input strings are both **non-empty** and contains only characters `1` or `0`.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: a = "11", b = "1"
|
||||
Output: "100"
|
||||
```
|
||||
|
||||
**Example 2**:
|
||||
|
||||
```
|
||||
Input: a = "1010", b = "1011"
|
||||
Output: "10101"
|
||||
```
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你两个二进制字符串,返回它们的和(用二进制表示)。输入为 非空 字符串且只包含数字 1 和 0。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 要求输出 2 个二进制数的和,结果也用二进制表示。
|
||||
- 简单题。按照二进制的加法规则做加法即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func addBinary(a string, b string) string {
|
||||
if len(b) > len(a) {
|
||||
a, b = b, a
|
||||
}
|
||||
|
||||
res := make([]string, len(a)+1)
|
||||
i, j, k, c := len(a)-1, len(b)-1, len(a), 0
|
||||
for i >= 0 && j >= 0 {
|
||||
ai, _ := strconv.Atoi(string(a[i]))
|
||||
bj, _ := strconv.Atoi(string(b[j]))
|
||||
res[k] = strconv.Itoa((ai + bj + c) % 2)
|
||||
c = (ai + bj + c) / 2
|
||||
i--
|
||||
j--
|
||||
k--
|
||||
}
|
||||
|
||||
for i >= 0 {
|
||||
ai, _ := strconv.Atoi(string(a[i]))
|
||||
res[k] = strconv.Itoa((ai + c) % 2)
|
||||
c = (ai + c) / 2
|
||||
i--
|
||||
k--
|
||||
}
|
||||
|
||||
if c > 0 {
|
||||
res[k] = strconv.Itoa(c)
|
||||
}
|
||||
|
||||
return strings.Join(res, "")
|
||||
}
|
||||
|
||||
```
|
||||
@@ -0,0 +1,13 @@
|
||||
package leetcode
|
||||
|
||||
func convertToTitle(n int) string {
|
||||
result := []byte{}
|
||||
for n > 0 {
|
||||
result = append(result, 'A'+byte((n-1)%26))
|
||||
n = (n - 1) / 26
|
||||
}
|
||||
for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 {
|
||||
result[i], result[j] = result[j], result[i]
|
||||
}
|
||||
return string(result)
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question168 struct {
|
||||
para168
|
||||
ans168
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para168 struct {
|
||||
n int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans168 struct {
|
||||
one string
|
||||
}
|
||||
|
||||
func Test_Problem168(t *testing.T) {
|
||||
|
||||
qs := []question168{
|
||||
|
||||
question168{
|
||||
para168{1},
|
||||
ans168{"A"},
|
||||
},
|
||||
|
||||
question168{
|
||||
para168{28},
|
||||
ans168{"AB"},
|
||||
},
|
||||
|
||||
question168{
|
||||
para168{701},
|
||||
ans168{"ZY"},
|
||||
},
|
||||
|
||||
question168{
|
||||
para168{10011},
|
||||
ans168{"NUA"},
|
||||
},
|
||||
|
||||
question168{
|
||||
para168{999},
|
||||
ans168{"ALK"},
|
||||
},
|
||||
|
||||
question168{
|
||||
para168{681},
|
||||
ans168{"ZE"},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 168------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans168, q.para168
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, convertToTitle(p.n))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
80
leetcode/0168.Excel-Sheet-Column-Title/README.md
Normal file
80
leetcode/0168.Excel-Sheet-Column-Title/README.md
Normal file
@@ -0,0 +1,80 @@
|
||||
# [168. Excel Sheet Column Title](https://leetcode.com/problems/excel-sheet-column-title/)
|
||||
|
||||
## 题目
|
||||
|
||||
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
|
||||
|
||||
For example:
|
||||
|
||||
```
|
||||
1 -> A
|
||||
2 -> B
|
||||
3 -> C
|
||||
...
|
||||
26 -> Z
|
||||
27 -> AA
|
||||
28 -> AB
|
||||
...
|
||||
```
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: 1
|
||||
Output: "A"
|
||||
```
|
||||
|
||||
**Example 2**:
|
||||
|
||||
```
|
||||
Input: 28
|
||||
Output: "AB"
|
||||
```
|
||||
|
||||
**Example 3**:
|
||||
|
||||
```
|
||||
Input: 701
|
||||
Output: "ZY"
|
||||
```
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个正整数,返回它在 Excel 表中相对应的列名称。
|
||||
|
||||
例如,
|
||||
|
||||
1 -> A
|
||||
2 -> B
|
||||
3 -> C
|
||||
...
|
||||
26 -> Z
|
||||
27 -> AA
|
||||
28 -> AB
|
||||
...
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给定一个正整数,返回它在 Excel 表中的对应的列名称
|
||||
- 简单题。这一题就类似短除法的计算过程。以 26 进制的字母编码。按照短除法先除,然后余数逆序输出即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
func convertToTitle(n int) string {
|
||||
result := []byte{}
|
||||
for n > 0 {
|
||||
result = append(result, 'A'+byte((n-1)%26))
|
||||
n = (n - 1) / 26
|
||||
}
|
||||
for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 {
|
||||
result[i], result[j] = result[j], result[i]
|
||||
}
|
||||
return string(result)
|
||||
}
|
||||
|
||||
```
|
||||
@@ -0,0 +1,10 @@
|
||||
package leetcode
|
||||
|
||||
func titleToNumber(s string) int {
|
||||
val, res := 0, 0
|
||||
for i := 0; i < len(s); i++ {
|
||||
val = int(s[i] - 'A' + 1)
|
||||
res = res*26 + val
|
||||
}
|
||||
return res
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question171 struct {
|
||||
para171
|
||||
ans171
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para171 struct {
|
||||
s string
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans171 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem171(t *testing.T) {
|
||||
|
||||
qs := []question171{
|
||||
|
||||
question171{
|
||||
para171{"A"},
|
||||
ans171{1},
|
||||
},
|
||||
|
||||
question171{
|
||||
para171{"AB"},
|
||||
ans171{28},
|
||||
},
|
||||
|
||||
question171{
|
||||
para171{"ZY"},
|
||||
ans171{701},
|
||||
},
|
||||
|
||||
question171{
|
||||
para171{"ABC"},
|
||||
ans171{731},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 171------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans171, q.para171
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, titleToNumber(p.s))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
67
leetcode/0171.Excel-Sheet-Column-Number/README.md
Normal file
67
leetcode/0171.Excel-Sheet-Column-Number/README.md
Normal file
@@ -0,0 +1,67 @@
|
||||
# [171. Excel Sheet Column Number](https://leetcode.com/problems/excel-sheet-column-number/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given a column title as appear in an Excel sheet, return its corresponding column number.
|
||||
|
||||
For example:
|
||||
|
||||
```
|
||||
A -> 1
|
||||
B -> 2
|
||||
C -> 3
|
||||
...
|
||||
Z -> 26
|
||||
AA -> 27
|
||||
AB -> 28
|
||||
...
|
||||
```
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: "A"
|
||||
Output: 1
|
||||
```
|
||||
|
||||
**Example 2**:
|
||||
|
||||
```
|
||||
Input: "AB"
|
||||
Output: 28
|
||||
```
|
||||
|
||||
**Example 3**:
|
||||
|
||||
```
|
||||
Input: "ZY"
|
||||
Output: 701
|
||||
```
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个 Excel 表格中的列名称,返回其相应的列序号。
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给出 Excel 中列的名称,输出其对应的列序号。
|
||||
- 简单题。这一题是第 168 题的逆序题。按照 26 进制还原成十进制即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
func titleToNumber(s string) int {
|
||||
val, res := 0, 0
|
||||
for i := 0; i < len(s); i++ {
|
||||
val = int(s[i] - 'A' + 1)
|
||||
res = res*26 + val
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
```
|
||||
13
leetcode/0258.Add-Digits/258. Add Digits.go
Normal file
13
leetcode/0258.Add-Digits/258. Add Digits.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package leetcode
|
||||
|
||||
func addDigits(num int) int {
|
||||
for num > 9 {
|
||||
cur := 0
|
||||
for num != 0 {
|
||||
cur += num % 10
|
||||
num /= 10
|
||||
}
|
||||
num = cur
|
||||
}
|
||||
return num
|
||||
}
|
||||
52
leetcode/0258.Add-Digits/258. Add Digits_test.go
Normal file
52
leetcode/0258.Add-Digits/258. Add Digits_test.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question258 struct {
|
||||
para258
|
||||
ans258
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para258 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans258 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem258(t *testing.T) {
|
||||
|
||||
qs := []question258{
|
||||
|
||||
question258{
|
||||
para258{38},
|
||||
ans258{2},
|
||||
},
|
||||
|
||||
question258{
|
||||
para258{88},
|
||||
ans258{7},
|
||||
},
|
||||
|
||||
question258{
|
||||
para258{96},
|
||||
ans258{6},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 258------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans258, q.para258
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, addDigits(p.one))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
47
leetcode/0258.Add-Digits/README.md
Normal file
47
leetcode/0258.Add-Digits/README.md
Normal file
@@ -0,0 +1,47 @@
|
||||
# [258. Add Digits](https://leetcode.com/problems/add-digits/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given a non-negative integer `num`, repeatedly add all its digits until the result has only one digit.
|
||||
|
||||
**Example**:
|
||||
|
||||
```
|
||||
Input: 38
|
||||
Output: 2
|
||||
Explanation: The process is like: 3 + 8 = 11, 1 + 1 = 2.
|
||||
Since 2 has only one digit, return it.
|
||||
```
|
||||
|
||||
**Follow up**: Could you do it without any loop/recursion in O(1) runtime?
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数。
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给定一个非负整数,反复加各个位上的数,直到结果为一位数为止,最后输出这一位数。
|
||||
- 简单题。按照题意循环累加即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
func addDigits(num int) int {
|
||||
for num > 9 {
|
||||
cur := 0
|
||||
for num != 0 {
|
||||
cur += num % 10
|
||||
num /= 10
|
||||
}
|
||||
num = cur
|
||||
}
|
||||
return num
|
||||
}
|
||||
|
||||
```
|
||||
@@ -0,0 +1,14 @@
|
||||
package leetcode
|
||||
|
||||
import "math"
|
||||
|
||||
func minMoves(nums []int) int {
|
||||
sum, min, l := 0, math.MaxInt32, len(nums)
|
||||
for _, v := range nums {
|
||||
sum += v
|
||||
if min > v {
|
||||
min = v
|
||||
}
|
||||
}
|
||||
return sum - min*l
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question453 struct {
|
||||
para453
|
||||
ans453
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para453 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans453 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem453(t *testing.T) {
|
||||
|
||||
qs := []question453{
|
||||
|
||||
question453{
|
||||
para453{[]int{4, 3, 2, 7, 8, 2, 3, 1}},
|
||||
ans453{22},
|
||||
},
|
||||
|
||||
question453{
|
||||
para453{[]int{1, 2, 3}},
|
||||
ans453{3},
|
||||
},
|
||||
// 如需多个测试,可以复制上方元素。
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 453------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans453, q.para453
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, minMoves(p.one))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
# [453. Minimum Moves to Equal Array Elements](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given a **non-empty** integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1.
|
||||
|
||||
**Example**:
|
||||
|
||||
```
|
||||
Input:
|
||||
[1,2,3]
|
||||
|
||||
Output:
|
||||
3
|
||||
|
||||
Explanation:
|
||||
Only three moves are needed (remember each move increments two elements):
|
||||
|
||||
[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
|
||||
```
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个长度为 n 的非空整数数组,找到让数组所有元素相等的最小移动次数。每次移动将会使 n - 1 个元素增加 1。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给定一个数组,要求输出让所有元素都相等的最小步数。每移动一步都会使得 n - 1 个元素 + 1 。
|
||||
- 数学题。这道题正着思考会考虑到排序或者暴力的方法上去。反过来思考一下,使得每个元素都相同,意思让所有元素的差异变为 0 。每次移动的过程中,都有 n - 1 个元素 + 1,那么没有 + 1 的那个元素和其他 n - 1 个元素相对差异就缩小了。所以这道题让所有元素都变为相等的最少步数,即等于让所有元素相对差异减少到最小的那个数。想到这里,此题就可以优雅的解出来了。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
import "math"
|
||||
|
||||
func minMoves(nums []int) int {
|
||||
sum, min, l := 0, math.MaxInt32, len(nums)
|
||||
for _, v := range nums {
|
||||
sum += v
|
||||
if min > v {
|
||||
min = v
|
||||
}
|
||||
}
|
||||
return sum - min*l
|
||||
}
|
||||
|
||||
```
|
||||
24
leetcode/0507.Perfect-Number/507. Perfect Number.go
Normal file
24
leetcode/0507.Perfect-Number/507. Perfect Number.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package leetcode
|
||||
|
||||
import "math"
|
||||
|
||||
// 方法一
|
||||
func checkPerfectNumber(num int) bool {
|
||||
if num <= 1 {
|
||||
return false
|
||||
}
|
||||
sum, bound := 1, int(math.Sqrt(float64(num)))+1
|
||||
for i := 2; i < bound; i++ {
|
||||
if num%i != 0 {
|
||||
continue
|
||||
}
|
||||
corrDiv := num / i
|
||||
sum += corrDiv + i
|
||||
}
|
||||
return sum == num
|
||||
}
|
||||
|
||||
// 方法二 打表
|
||||
func checkPerfectNumber_(num int) bool {
|
||||
return num == 6 || num == 28 || num == 496 || num == 8128 || num == 33550336
|
||||
}
|
||||
53
leetcode/0507.Perfect-Number/507. Perfect Number_test.go
Normal file
53
leetcode/0507.Perfect-Number/507. Perfect Number_test.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question507 struct {
|
||||
para507
|
||||
ans507
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para507 struct {
|
||||
num int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans507 struct {
|
||||
one bool
|
||||
}
|
||||
|
||||
func Test_Problem507(t *testing.T) {
|
||||
|
||||
qs := []question507{
|
||||
|
||||
question507{
|
||||
para507{28},
|
||||
ans507{true},
|
||||
},
|
||||
|
||||
question507{
|
||||
para507{496},
|
||||
ans507{true},
|
||||
},
|
||||
|
||||
question507{
|
||||
para507{500},
|
||||
ans507{false},
|
||||
},
|
||||
// 如需多个测试,可以复制上方元素。
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 507------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans507, q.para507
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, checkPerfectNumber(p.num))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
64
leetcode/0507.Perfect-Number/README.md
Normal file
64
leetcode/0507.Perfect-Number/README.md
Normal file
@@ -0,0 +1,64 @@
|
||||
# [507. Perfect Number](https://leetcode.com/problems/perfect-number/)
|
||||
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
We define the Perfect Number is a **positive** integer that is equal to the sum of all its **positive** divisors except itself.
|
||||
|
||||
Now, given an
|
||||
|
||||
**integer**
|
||||
|
||||
n, write a function that returns true when it is a perfect number and false when it is not.
|
||||
|
||||
**Example**:
|
||||
|
||||
```
|
||||
Input: 28
|
||||
Output: True
|
||||
Explanation: 28 = 1 + 2 + 4 + 7 + 14
|
||||
```
|
||||
|
||||
**Note**: The input number **n** will not exceed 100,000,000. (1e8)
|
||||
|
||||
## 题目大意
|
||||
|
||||
对于一个 正整数,如果它和除了它自身以外的所有正因子之和相等,我们称它为“完美数”。给定一个 整数 n, 如果他是完美数,返回 True,否则返回 False
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给定一个整数,要求判断这个数是不是完美数。整数的取值范围小于 1e8 。
|
||||
- 简单题。按照题意描述,先获取这个整数的所有正因子,如果正因子的和等于原来这个数,那么它就是完美数。
|
||||
- 这一题也可以打表,1e8 以下的完美数其实并不多,就 5 个。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
import "math"
|
||||
|
||||
// 方法一
|
||||
func checkPerfectNumber(num int) bool {
|
||||
if num <= 1 {
|
||||
return false
|
||||
}
|
||||
sum, bound := 1, int(math.Sqrt(float64(num)))+1
|
||||
for i := 2; i < bound; i++ {
|
||||
if num%i != 0 {
|
||||
continue
|
||||
}
|
||||
corrDiv := num / i
|
||||
sum += corrDiv + i
|
||||
}
|
||||
return sum == num
|
||||
}
|
||||
|
||||
// 方法二 打表
|
||||
func checkPerfectNumber_(num int) bool {
|
||||
return num == 6 || num == 28 || num == 496 || num == 8128 || num == 33550336
|
||||
}
|
||||
|
||||
```
|
||||
@@ -0,0 +1,21 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func complexNumberMultiply(a string, b string) string {
|
||||
realA, imagA := parse(a)
|
||||
realB, imagB := parse(b)
|
||||
real := realA*realB - imagA*imagB
|
||||
imag := realA*imagB + realB*imagA
|
||||
return strconv.Itoa(real) + "+" + strconv.Itoa(imag) + "i"
|
||||
}
|
||||
|
||||
func parse(s string) (int, int) {
|
||||
ss := strings.Split(s, "+")
|
||||
r, _ := strconv.Atoi(ss[0])
|
||||
i, _ := strconv.Atoi(ss[1][:len(ss[1])-1])
|
||||
return r, i
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question537 struct {
|
||||
para537
|
||||
ans537
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para537 struct {
|
||||
a string
|
||||
b string
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans537 struct {
|
||||
one string
|
||||
}
|
||||
|
||||
func Test_Problem537(t *testing.T) {
|
||||
|
||||
qs := []question537{
|
||||
|
||||
question537{
|
||||
para537{"1+1i", "1+1i"},
|
||||
ans537{"0+2i"},
|
||||
},
|
||||
|
||||
question537{
|
||||
para537{"1+-1i", "1+-1i"},
|
||||
ans537{"0+-2i"},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 537------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans537, q.para537
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, complexNumberMultiply(p.a, p.b))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
73
leetcode/0537.Complex-Number-Multiplication/README.md
Normal file
73
leetcode/0537.Complex-Number-Multiplication/README.md
Normal file
@@ -0,0 +1,73 @@
|
||||
# [537. Complex Number Multiplication](https://leetcode.com/problems/complex-number-multiplication/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given two strings representing two [complex numbers](https://en.wikipedia.org/wiki/Complex_number).
|
||||
|
||||
You need to return a string representing their multiplication. Note i2 = -1 according to the definition.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: "1+1i", "1+1i"
|
||||
Output: "0+2i"
|
||||
Explanation: (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i, and you need convert it to the form of 0+2i.
|
||||
```
|
||||
|
||||
**Example 2**:
|
||||
|
||||
```
|
||||
Input: "1+-1i", "1+-1i"
|
||||
Output: "0+-2i"
|
||||
Explanation: (1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i, and you need convert it to the form of 0+-2i.
|
||||
```
|
||||
|
||||
**Note**:
|
||||
|
||||
1. The input strings will not have extra blank.
|
||||
2. The input strings will be given in the form of **a+bi**, where the integer **a** and **b** will both belong to the range of [-100, 100]. And **the output should be also in this form**.
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定两个表示复数的字符串。返回表示它们乘积的字符串。注意,根据定义 i^2 = -1 。
|
||||
|
||||
注意:
|
||||
|
||||
- 输入字符串不包含额外的空格。
|
||||
- 输入字符串将以 a+bi 的形式给出,其中整数 a 和 b 的范围均在 [-100, 100] 之间。输出也应当符合这种形式。
|
||||
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给定 2 个字符串,要求这两个复数的乘积,输出也是字符串格式。
|
||||
- 数学题。按照复数的运算法则,i^2 = -1,最后输出字符串结果即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func complexNumberMultiply(a string, b string) string {
|
||||
realA, imagA := parse(a)
|
||||
realB, imagB := parse(b)
|
||||
real := realA*realB - imagA*imagB
|
||||
imag := realA*imagB + realB*imagA
|
||||
return strconv.Itoa(real) + "+" + strconv.Itoa(imag) + "i"
|
||||
}
|
||||
|
||||
func parse(s string) (int, int) {
|
||||
ss := strings.Split(s, "+")
|
||||
r, _ := strconv.Atoi(ss[0])
|
||||
i, _ := strconv.Atoi(ss[1][:len(ss[1])-1])
|
||||
return r, i
|
||||
}
|
||||
|
||||
```
|
||||
19
leetcode/0561.Array-Partition-I/561. Array Partition I.go
Normal file
19
leetcode/0561.Array-Partition-I/561. Array Partition I.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package leetcode
|
||||
|
||||
func arrayPairSum(nums []int) int {
|
||||
array := [20001]int{}
|
||||
for i := 0; i < len(nums); i++ {
|
||||
array[nums[i]+10000]++
|
||||
}
|
||||
flag, sum := true, 0
|
||||
for i := 0; i < len(array); i++ {
|
||||
for array[i] > 0 {
|
||||
if flag {
|
||||
sum = sum + i - 10000
|
||||
}
|
||||
flag = !flag
|
||||
array[i]--
|
||||
}
|
||||
}
|
||||
return sum
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question561 struct {
|
||||
para561
|
||||
ans561
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para561 struct {
|
||||
nums []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans561 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem561(t *testing.T) {
|
||||
|
||||
qs := []question561{
|
||||
|
||||
question561{
|
||||
para561{[]int{}},
|
||||
ans561{0},
|
||||
},
|
||||
|
||||
question561{
|
||||
para561{[]int{1, 4, 3, 2}},
|
||||
ans561{4},
|
||||
},
|
||||
|
||||
// 如需多个测试,可以复制上方元素。
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 561------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans561, q.para561
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, arrayPairSum(p.nums))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
58
leetcode/0561.Array-Partition-I/README.md
Normal file
58
leetcode/0561.Array-Partition-I/README.md
Normal file
@@ -0,0 +1,58 @@
|
||||
# [561. Array Partition I](https://leetcode.com/problems/array-partition-i/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given an array of **2n** integers, your task is to group these integers into **n** pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: [1,4,3,2]
|
||||
|
||||
Output: 4
|
||||
Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).
|
||||
```
|
||||
|
||||
**Note**:
|
||||
|
||||
1. **n** is a positive integer, which is in the range of [1, 10000].
|
||||
2. All the integers in the array will be in the range of [-10000, 10000].
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最大。
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给定一个 2n 个数组,要求把它们分为 n 组一行,求出各组最小值的总和的最大值。
|
||||
- 由于题目给的数据范围不大,[-10000, 10000],所以我们可以考虑用一个哈希表数组,里面存储 i - 10000 元素的频次,偏移量是 10000。这个哈希表能按递增的顺序访问数组,这样可以减少排序的耗时。题目要求求出分组以后求和的最大值,那么所有偏小的元素尽量都安排在一组里面,这样取 min 以后,对最大和影响不大。例如,(1 , 1) 这样安排在一起,min 以后就是 1 。但是如果把相差很大的两个元素安排到一起,那么较大的那个元素就“牺牲”了。例如,(1 , 10000),取 min 以后就是 1,于是 10000 就“牺牲”了。所以需要优先考虑较小值。
|
||||
- 较小值出现的频次可能是奇数也可能是偶数。如果是偶数,那比较简单,把它们俩俩安排在一起就可以了。如果是奇数,那么它会落单一次,落单的那个需要和距离它最近的一个元素进行配对,这样对最终的和影响最小。较小值如果是奇数,那么就会影响后面元素的选择,后面元素如果是偶数,由于需要一个元素和前面的较小值配对,所以它剩下的又是奇数个。这个影响会依次传递到后面。所以用一个 flag 标记,如果当前集合中有剩余元素将被再次考虑,则此标志设置为 1。在从下一组中选择元素时,会考虑已考虑的相同额外元素。
|
||||
- 最后扫描过程中动态的维护 sum 值就可以了。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
func arrayPairSum(nums []int) int {
|
||||
array := [20001]int{}
|
||||
for i := 0; i < len(nums); i++ {
|
||||
array[nums[i]+10000]++
|
||||
}
|
||||
flag, sum := true, 0
|
||||
for i := 0; i < len(array); i++ {
|
||||
for array[i] > 0 {
|
||||
if flag {
|
||||
sum = sum + i - 10000
|
||||
}
|
||||
flag = !flag
|
||||
array[i]--
|
||||
}
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
```
|
||||
17
leetcode/0598.Range-Addition-II/598. Range Addition II.go
Normal file
17
leetcode/0598.Range-Addition-II/598. Range Addition II.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package leetcode
|
||||
|
||||
func maxCount(m int, n int, ops [][]int) int {
|
||||
minM, minN := m, n
|
||||
for _, op := range ops {
|
||||
minM = min(minM, op[0])
|
||||
minN = min(minN, op[1])
|
||||
}
|
||||
return minM * minN
|
||||
}
|
||||
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question598 struct {
|
||||
para598
|
||||
ans598
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para598 struct {
|
||||
m int
|
||||
n int
|
||||
ops [][]int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans598 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem598(t *testing.T) {
|
||||
|
||||
qs := []question598{
|
||||
|
||||
question598{
|
||||
para598{3, 3, [][]int{[]int{2, 2}, []int{3, 3}}},
|
||||
ans598{4},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 598------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans598, q.para598
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, maxCount(p.m, p.n, p.ops))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
82
leetcode/0598.Range-Addition-II/README.md
Normal file
82
leetcode/0598.Range-Addition-II/README.md
Normal file
@@ -0,0 +1,82 @@
|
||||
# [598. Range Addition II](https://leetcode.com/problems/range-addition-ii/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given an m * n matrix **M** initialized with all **0**'s and several update operations.
|
||||
|
||||
Operations are represented by a 2D array, and each operation is represented by an array with two **positive** integers **a** and **b**, which means **M[i][j]** should be **added by one** for all **0 <= i < a** and **0 <= j < b**.
|
||||
|
||||
You need to count and return the number of maximum integers in the matrix after performing all the operations.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input:
|
||||
m = 3, n = 3
|
||||
operations = [[2,2],[3,3]]
|
||||
Output: 4
|
||||
Explanation:
|
||||
Initially, M =
|
||||
[[0, 0, 0],
|
||||
[0, 0, 0],
|
||||
[0, 0, 0]]
|
||||
|
||||
After performing [2,2], M =
|
||||
[[1, 1, 0],
|
||||
[1, 1, 0],
|
||||
[0, 0, 0]]
|
||||
|
||||
After performing [3,3], M =
|
||||
[[2, 2, 1],
|
||||
[2, 2, 1],
|
||||
[1, 1, 1]]
|
||||
|
||||
So the maximum integer in M is 2, and there are four of it in M. So return 4.
|
||||
```
|
||||
|
||||
**Note**:
|
||||
|
||||
1. The range of m and n is [1,40000].
|
||||
2. The range of a is [1,m], and the range of b is [1,n].
|
||||
3. The range of operations size won't exceed 10,000.
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个初始元素全部为 0,大小为 m*n 的矩阵 M 以及在 M 上的一系列更新操作。操作用二维数组表示,其中的每个操作用一个含有两个正整数 a 和 b 的数组表示,含义是将所有符合 0 <= i < a 以及 0 <= j < b 的元素 M[i][j] 的值都增加 1。在执行给定的一系列操作后,你需要返回矩阵中含有最大整数的元素个数。
|
||||
|
||||
注意:
|
||||
|
||||
- m 和 n 的范围是 [1,40000]。
|
||||
- a 的范围是 [1,m],b 的范围是 [1,n]。
|
||||
- 操作数目不超过 10000。
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给定一个初始都为 0 的 m * n 的矩阵,和一个操作数组。经过一系列的操作以后,最终输出矩阵中最大整数的元素个数。每次操作都使得一个矩形内的元素都 + 1 。
|
||||
- 这一题乍一看像线段树的区间覆盖问题,但是实际上很简单。如果此题是任意的矩阵,那就可能用到线段树了。这一题每个矩阵的起点都包含 [0 , 0] 这个元素,也就是说每次操作都会影响第一个元素。那么这道题就很简单了。经过 n 次操作以后,被覆盖次数最多的矩形区间,一定就是最大整数所在的区间。由于起点都是第一个元素,所以我们只用关心矩形的右下角那个坐标。右下角怎么计算呢?只用每次动态的维护一下矩阵长和宽的最小值即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
func maxCount(m int, n int, ops [][]int) int {
|
||||
minM, minN := m, n
|
||||
for _, op := range ops {
|
||||
minM = min(minM, op[0])
|
||||
minN = min(minN, op[1])
|
||||
}
|
||||
return minM * minN
|
||||
}
|
||||
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
```
|
||||
@@ -0,0 +1,31 @@
|
||||
package leetcode
|
||||
|
||||
func largestTriangleArea(points [][]int) float64 {
|
||||
maxArea, n := 0.0, len(points)
|
||||
for i := 0; i < n; i++ {
|
||||
for j := i + 1; j < n; j++ {
|
||||
for k := j + 1; k < n; k++ {
|
||||
maxArea = max(maxArea, area(points[i], points[j], points[k]))
|
||||
}
|
||||
}
|
||||
}
|
||||
return maxArea
|
||||
}
|
||||
|
||||
func area(p1, p2, p3 []int) float64 {
|
||||
return abs(p1[0]*p2[1]+p2[0]*p3[1]+p3[0]*p1[1]-p1[0]*p3[1]-p2[0]*p1[1]-p3[0]*p2[1]) / 2
|
||||
}
|
||||
|
||||
func abs(num int) float64 {
|
||||
if num < 0 {
|
||||
num = -num
|
||||
}
|
||||
return float64(num)
|
||||
}
|
||||
|
||||
func max(a, b float64) float64 {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question812 struct {
|
||||
para812
|
||||
ans812
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para812 struct {
|
||||
one [][]int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans812 struct {
|
||||
one float64
|
||||
}
|
||||
|
||||
func Test_Problem812(t *testing.T) {
|
||||
|
||||
qs := []question812{
|
||||
|
||||
question812{
|
||||
para812{[][]int{[]int{0, 0}, []int{0, 1}, []int{1, 0}, []int{0, 2}, []int{2, 0}}},
|
||||
ans812{2.0},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 812------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans812, q.para812
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, largestTriangleArea(p.one))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
70
leetcode/0812.Largest-Triangle-Area/README.md
Normal file
70
leetcode/0812.Largest-Triangle-Area/README.md
Normal file
@@ -0,0 +1,70 @@
|
||||
# [812. Largest Triangle Area](https://leetcode.com/problems/largest-triangle-area/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
You have a list of points in the plane. Return the area of the largest triangle that can be formed by any 3 of the points.
|
||||
|
||||
```
|
||||
Example:
|
||||
Input: points = [[0,0],[0,1],[1,0],[0,2],[2,0]]
|
||||
Output: 2
|
||||
Explanation:
|
||||
The five points are show in the figure below. The red triangle is the largest.
|
||||
```
|
||||
|
||||

|
||||
|
||||
**Notes**:
|
||||
|
||||
- `3 <= points.length <= 50`.
|
||||
- No points will be duplicated.
|
||||
- `-50 <= points[i][j] <= 50`.
|
||||
- Answers within `10^-6` of the true value will be accepted as correct.
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定包含多个点的集合,从其中取三个点组成三角形,返回能组成的最大三角形的面积。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给出一组点的坐标,要求找出能组成三角形面积最大的点集合,输出这个最大面积。
|
||||
- 数学题。按照数学定义,分别计算这些能构成三角形的点形成的三角形面积,最终输出最大面积即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
func largestTriangleArea(points [][]int) float64 {
|
||||
maxArea, n := 0.0, len(points)
|
||||
for i := 0; i < n; i++ {
|
||||
for j := i + 1; j < n; j++ {
|
||||
for k := j + 1; k < n; k++ {
|
||||
maxArea = max(maxArea, area(points[i], points[j], points[k]))
|
||||
}
|
||||
}
|
||||
}
|
||||
return maxArea
|
||||
}
|
||||
|
||||
func area(p1, p2, p3 []int) float64 {
|
||||
return abs(p1[0]*p2[1]+p2[0]*p3[1]+p3[0]*p1[1]-p1[0]*p3[1]-p2[0]*p1[1]-p3[0]*p2[1]) / 2
|
||||
}
|
||||
|
||||
func abs(num int) float64 {
|
||||
if num < 0 {
|
||||
num = -num
|
||||
}
|
||||
return float64(num)
|
||||
}
|
||||
|
||||
func max(a, b float64) float64 {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
```
|
||||
13
leetcode/0832.Flipping-an-Image/832. Flipping an Image.go
Normal file
13
leetcode/0832.Flipping-an-Image/832. Flipping an Image.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package leetcode
|
||||
|
||||
func flipAndInvertImage(A [][]int) [][]int {
|
||||
for i := 0; i < len(A); i++ {
|
||||
for a, b := 0, len(A[i])-1; a < b; a, b = a+1, b-1 {
|
||||
A[i][a], A[i][b] = A[i][b], A[i][a]
|
||||
}
|
||||
for a := 0; a < len(A[i]); a++ {
|
||||
A[i][a] = (A[i][a] + 1) % 2
|
||||
}
|
||||
}
|
||||
return A
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question832 struct {
|
||||
para832
|
||||
ans832
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para832 struct {
|
||||
A [][]int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans832 struct {
|
||||
one [][]int
|
||||
}
|
||||
|
||||
func Test_Problem832(t *testing.T) {
|
||||
|
||||
qs := []question832{
|
||||
|
||||
question832{
|
||||
para832{[][]int{[]int{1, 1, 0}, []int{1, 0, 1}, []int{0, 0, 0}}},
|
||||
ans832{[][]int{[]int{1, 0, 0}, []int{0, 1, 0}, []int{1, 1, 1}}},
|
||||
},
|
||||
|
||||
question832{
|
||||
para832{[][]int{[]int{1, 1, 0, 0}, []int{1, 0, 0, 1}, []int{0, 1, 1, 1}, []int{1, 0, 1, 0}}},
|
||||
ans832{[][]int{[]int{1, 1, 0, 0}, []int{0, 1, 1, 0}, []int{0, 0, 0, 1}, []int{1, 0, 1, 0}}},
|
||||
},
|
||||
|
||||
question832{
|
||||
para832{[][]int{[]int{1, 1, 1}, []int{1, 1, 1}, []int{0, 0, 0}}},
|
||||
ans832{[][]int{[]int{0, 0, 0}, []int{0, 0, 0}, []int{1, 1, 1}}},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 832------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans832, q.para832
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, flipAndInvertImage(p.A))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
63
leetcode/0832.Flipping-an-Image/README.md
Normal file
63
leetcode/0832.Flipping-an-Image/README.md
Normal file
@@ -0,0 +1,63 @@
|
||||
# [832. Flipping an Image](https://leetcode.com/problems/flipping-an-image/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given a binary matrix `A`, we want to flip the image horizontally, then invert it, and return the resulting image.
|
||||
|
||||
To flip an image horizontally means that each row of the image is reversed. For example, flipping `[1, 1, 0]` horizontally results in `[0, 1, 1]`.
|
||||
|
||||
To invert an image means that each `0` is replaced by `1`, and each `1` is replaced by `0`. For example, inverting `[0, 1, 1]` results in `[1, 0, 0]`.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: [[1,1,0],[1,0,1],[0,0,0]]
|
||||
Output: [[1,0,0],[0,1,0],[1,1,1]]
|
||||
Explanation: First reverse each row: [[0,1,1],[1,0,1],[0,0,0]].
|
||||
Then, invert the image: [[1,0,0],[0,1,0],[1,1,1]]
|
||||
```
|
||||
|
||||
**Example 2**:
|
||||
|
||||
```
|
||||
Input: [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]
|
||||
Output: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
|
||||
Explanation: First reverse each row: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]].
|
||||
Then invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
|
||||
```
|
||||
|
||||
**Notes**:
|
||||
|
||||
- `1 <= A.length = A[0].length <= 20`
|
||||
- `0 <= A[i][j] <= 1`
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果。水平翻转图片就是将图片的每一行都进行翻转,即逆序。例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, 1]。反转图片的意思是图片中的 0 全部被 1 替换, 1 全部被 0 替换。例如,反转 [0, 1, 1] 的结果是 [1, 0, 0]。
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给定一个二进制矩阵,要求先水平翻转,然后再反转( 1→0 , 0→1 )。
|
||||
- 简单题,按照题意先水平翻转,再反转即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
func flipAndInvertImage(A [][]int) [][]int {
|
||||
for i := 0; i < len(A); i++ {
|
||||
for a, b := 0, len(A[i])-1; a < b; a, b = a+1, b-1 {
|
||||
A[i][a], A[i][b] = A[i][b], A[i][a]
|
||||
}
|
||||
for a := 0; a < len(A[i]); a++ {
|
||||
A[i][a] = (A[i][a] + 1) % 2
|
||||
}
|
||||
}
|
||||
return A
|
||||
}
|
||||
|
||||
```
|
||||
@@ -0,0 +1,41 @@
|
||||
package leetcode
|
||||
|
||||
func surfaceArea(grid [][]int) int {
|
||||
area := 0
|
||||
for i := 0; i < len(grid); i++ {
|
||||
for j := 0; j < len(grid[0]); j++ {
|
||||
if grid[i][j] == 0 {
|
||||
continue
|
||||
}
|
||||
area += grid[i][j]*4 + 2
|
||||
// up
|
||||
if i > 0 {
|
||||
m := min(grid[i][j], grid[i-1][j])
|
||||
area -= m
|
||||
}
|
||||
// down
|
||||
if i < len(grid)-1 {
|
||||
m := min(grid[i][j], grid[i+1][j])
|
||||
area -= m
|
||||
}
|
||||
// left
|
||||
if j > 0 {
|
||||
m := min(grid[i][j], grid[i][j-1])
|
||||
area -= m
|
||||
}
|
||||
// right
|
||||
if j < len(grid[i])-1 {
|
||||
m := min(grid[i][j], grid[i][j+1])
|
||||
area -= m
|
||||
}
|
||||
}
|
||||
}
|
||||
return area
|
||||
}
|
||||
|
||||
func min(a, b int) int {
|
||||
if a > b {
|
||||
return b
|
||||
}
|
||||
return a
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question892 struct {
|
||||
para892
|
||||
ans892
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para892 struct {
|
||||
one [][]int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans892 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem892(t *testing.T) {
|
||||
|
||||
qs := []question892{
|
||||
|
||||
question892{
|
||||
para892{[][]int{[]int{2}}},
|
||||
ans892{10},
|
||||
},
|
||||
|
||||
question892{
|
||||
para892{[][]int{[]int{1, 2}, []int{3, 4}}},
|
||||
ans892{34},
|
||||
},
|
||||
|
||||
question892{
|
||||
para892{[][]int{[]int{1, 0}, []int{0, 2}}},
|
||||
ans892{16},
|
||||
},
|
||||
|
||||
question892{
|
||||
para892{[][]int{[]int{1, 1, 1}, []int{1, 0, 1}, []int{1, 1, 1}}},
|
||||
ans892{32},
|
||||
},
|
||||
|
||||
question892{
|
||||
para892{[][]int{[]int{2, 2, 2}, []int{2, 1, 2}, []int{2, 2, 2}}},
|
||||
ans892{46},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 892------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans892, q.para892
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, surfaceArea(p.one))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
108
leetcode/0892.Surface-Area-of-3D-Shapes/README.md
Normal file
108
leetcode/0892.Surface-Area-of-3D-Shapes/README.md
Normal file
@@ -0,0 +1,108 @@
|
||||
# [892. Surface Area of 3D Shapes](https://leetcode.com/problems/surface-area-of-3d-shapes/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
On a `N * N` grid, we place some `1 * 1 * 1` cubes.
|
||||
|
||||
Each value `v = grid[i][j]` represents a tower of `v` cubes placed on top of grid cell `(i, j)`.
|
||||
|
||||
Return the total surface area of the resulting shapes.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: [[2]]
|
||||
Output: 10
|
||||
```
|
||||
|
||||
**Example 2**:
|
||||
|
||||
```
|
||||
Input: [[1,2],[3,4]]
|
||||
Output: 34
|
||||
```
|
||||
|
||||
**Example 3**:
|
||||
|
||||
```
|
||||
Input: [[1,0],[0,2]]
|
||||
Output: 16
|
||||
```
|
||||
|
||||
**Example 4**:
|
||||
|
||||
```
|
||||
Input: [[1,1,1],[1,0,1],[1,1,1]]
|
||||
Output: 32
|
||||
```
|
||||
|
||||
**Example 5**:
|
||||
|
||||
```
|
||||
Input: [[2,2,2],[2,1,2],[2,2,2]]
|
||||
Output: 46
|
||||
```
|
||||
|
||||
**Note**:
|
||||
|
||||
- `1 <= N <= 50`
|
||||
- `0 <= grid[i][j] <= 50`
|
||||
|
||||
## 题目大意
|
||||
|
||||
在 N * N 的网格上,我们放置一些 1 * 1 * 1 的立方体。每个值 v = grid[i][j] 表示 v 个正方体叠放在对应单元格 (i, j) 上。请你返回最终形体的表面积。
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给定一个网格数组,数组里面装的是立方体叠放在所在的单元格,求最终这些叠放的立方体的表面积。
|
||||
- 简单题。按照题目意思,找到叠放时,重叠的面,然后用总表面积减去这些重叠的面积即为最终答案。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
func surfaceArea(grid [][]int) int {
|
||||
area := 0
|
||||
for i := 0; i < len(grid); i++ {
|
||||
for j := 0; j < len(grid[0]); j++ {
|
||||
if grid[i][j] == 0 {
|
||||
continue
|
||||
}
|
||||
area += grid[i][j]*4 + 2
|
||||
// up
|
||||
if i > 0 {
|
||||
m := min(grid[i][j], grid[i-1][j])
|
||||
area -= m
|
||||
}
|
||||
// down
|
||||
if i < len(grid)-1 {
|
||||
m := min(grid[i][j], grid[i+1][j])
|
||||
area -= m
|
||||
}
|
||||
// left
|
||||
if j > 0 {
|
||||
m := min(grid[i][j], grid[i][j-1])
|
||||
area -= m
|
||||
}
|
||||
// right
|
||||
if j < len(grid[i])-1 {
|
||||
m := min(grid[i][j], grid[i][j+1])
|
||||
area -= m
|
||||
}
|
||||
}
|
||||
}
|
||||
return area
|
||||
}
|
||||
|
||||
func min(a, b int) int {
|
||||
if a > b {
|
||||
return b
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
```
|
||||
@@ -0,0 +1,33 @@
|
||||
package leetcode
|
||||
|
||||
import "fmt"
|
||||
|
||||
func largestTimeFromDigits(A []int) string {
|
||||
flag, res := false, 0
|
||||
for i := 0; i < 4; i++ {
|
||||
for j := 0; j < 4; j++ {
|
||||
if i == j {
|
||||
continue
|
||||
}
|
||||
for k := 0; k < 4; k++ {
|
||||
if i == k || j == k {
|
||||
continue
|
||||
}
|
||||
l := 6 - i - j - k
|
||||
hour := A[i]*10 + A[j]
|
||||
min := A[k]*10 + A[l]
|
||||
if hour < 24 && min < 60 {
|
||||
if hour*60+min >= res {
|
||||
res = hour*60 + min
|
||||
flag = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if flag {
|
||||
return fmt.Sprintf("%02d:%02d", res/60, res%60)
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question949 struct {
|
||||
para949
|
||||
ans949
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para949 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans949 struct {
|
||||
one string
|
||||
}
|
||||
|
||||
func Test_Problem949(t *testing.T) {
|
||||
|
||||
qs := []question949{
|
||||
question949{
|
||||
para949{[]int{1, 2, 3, 4}},
|
||||
ans949{"23:41"},
|
||||
},
|
||||
|
||||
question949{
|
||||
para949{[]int{5, 5, 5, 5}},
|
||||
ans949{""},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 949------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans949, q.para949
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, largestTimeFromDigits(p.one))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
78
leetcode/0949.Largest-Time-for-Given-Digits/README.md
Normal file
78
leetcode/0949.Largest-Time-for-Given-Digits/README.md
Normal file
@@ -0,0 +1,78 @@
|
||||
# [949. Largest Time for Given Digits](https://leetcode.com/problems/largest-time-for-given-digits/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given an array of 4 digits, return the largest 24 hour time that can be made.
|
||||
|
||||
The smallest 24 hour time is 00:00, and the largest is 23:59. Starting from 00:00, a time is larger if more time has elapsed since midnight.
|
||||
|
||||
Return the answer as a string of length 5. If no valid time can be made, return an empty string.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: [1,2,3,4]
|
||||
Output: "23:41"
|
||||
```
|
||||
|
||||
**Example 2**:
|
||||
|
||||
```
|
||||
Input: [5,5,5,5]
|
||||
Output: ""
|
||||
```
|
||||
|
||||
**Note**:
|
||||
|
||||
1. `A.length == 4`
|
||||
2. `0 <= A[i] <= 9`
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个由 4 位数字组成的数组,返回可以设置的符合 24 小时制的最大时间。最小的 24 小时制时间是 00:00,而最大的是 23:59。从 00:00 (午夜)开始算起,过得越久,时间越大。以长度为 5 的字符串返回答案。如果不能确定有效时间,则返回空字符串。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给出 4 个数字,要求返回一个字符串,代表由这 4 个数字能组成的最大 24 小时制的时间。
|
||||
- 简单题,这一题直接暴力枚举就可以了。依次检查给出的 4 个数字每个排列组合是否是时间合法的。例如检查 10 * A[i] + A[j] 是不是小于 24, 10 * A[k] + A[l] 是不是小于 60。如果合法且比目前存在的最大时间更大,就更新这个最大时间。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
import "fmt"
|
||||
|
||||
func largestTimeFromDigits(A []int) string {
|
||||
flag, res := false, 0
|
||||
for i := 0; i < 4; i++ {
|
||||
for j := 0; j < 4; j++ {
|
||||
if i == j {
|
||||
continue
|
||||
}
|
||||
for k := 0; k < 4; k++ {
|
||||
if i == k || j == k {
|
||||
continue
|
||||
}
|
||||
l := 6 - i - j - k
|
||||
hour := A[i]*10 + A[j]
|
||||
min := A[k]*10 + A[l]
|
||||
if hour < 24 && min < 60 {
|
||||
if hour*60+min >= res {
|
||||
res = hour*60 + min
|
||||
flag = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if flag {
|
||||
return fmt.Sprintf("%02d:%02d", res/60, res%60)
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
5
leetcode/1037.Valid-Boomerang/1037. Valid Boomerang.go
Normal file
5
leetcode/1037.Valid-Boomerang/1037. Valid Boomerang.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package leetcode
|
||||
|
||||
func isBoomerang(points [][]int) bool {
|
||||
return (points[0][0]-points[1][0])*(points[0][1]-points[2][1]) != (points[0][0]-points[2][0])*(points[0][1]-points[1][1])
|
||||
}
|
||||
46
leetcode/1037.Valid-Boomerang/1037. Valid Boomerang_test.go
Normal file
46
leetcode/1037.Valid-Boomerang/1037. Valid Boomerang_test.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1037 struct {
|
||||
para1037
|
||||
ans1037
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1037 struct {
|
||||
one [][]int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1037 struct {
|
||||
one bool
|
||||
}
|
||||
|
||||
func Test_Problem1037(t *testing.T) {
|
||||
|
||||
qs := []question1037{
|
||||
question1037{
|
||||
para1037{[][]int{[]int{1, 2}, []int{2, 3}, []int{3, 2}}},
|
||||
ans1037{true},
|
||||
},
|
||||
|
||||
question1037{
|
||||
para1037{[][]int{[]int{1, 1}, []int{2, 2}, []int{3, 3}}},
|
||||
ans1037{false},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1037------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1037, q.para1037
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, isBoomerang(p.one))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
49
leetcode/1037.Valid-Boomerang/README.md
Normal file
49
leetcode/1037.Valid-Boomerang/README.md
Normal file
@@ -0,0 +1,49 @@
|
||||
# [1037. Valid Boomerang](https://leetcode.com/problems/valid-boomerang/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
A *boomerang* is a set of 3 points that are all distinct and **not** in a straight line.
|
||||
|
||||
Given a list of three points in the plane, return whether these points are a boomerang.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: [[1,1],[2,3],[3,2]]
|
||||
Output: true
|
||||
```
|
||||
|
||||
**Example 2**:
|
||||
|
||||
```
|
||||
Input: [[1,1],[2,2],[3,3]]
|
||||
Output: false
|
||||
```
|
||||
|
||||
**Note**:
|
||||
|
||||
1. `points.length == 3`
|
||||
2. `points[i].length == 2`
|
||||
3. `0 <= points[i][j] <= 100`
|
||||
|
||||
## 题目大意
|
||||
|
||||
回旋镖定义为一组三个点,这些点各不相同且不在一条直线上。给出平面上三个点组成的列表,判断这些点是否可以构成回旋镖。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 判断给出的 3 组点能否满足回旋镖。
|
||||
- 简单题。判断 3 个点组成的 2 条直线的斜率是否相等。由于斜率的计算是除法,还可能遇到分母为 0 的情况,那么可以转换成乘法,交叉相乘再判断是否相等,就可以省去判断分母为 0 的情况了,代码也简洁成一行了。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
func isBoomerang(points [][]int) bool {
|
||||
return (points[0][0]-points[1][0])*(points[0][1]-points[2][1]) != (points[0][0]-points[2][0])*(points[0][1]-points[1][1])
|
||||
}
|
||||
|
||||
```
|
||||
@@ -0,0 +1,11 @@
|
||||
package leetcode
|
||||
|
||||
func decompressRLElist(nums []int) []int {
|
||||
res := []int{}
|
||||
for i := 0; i < len(nums); i += 2 {
|
||||
for j := 0; j < nums[i]; j++ {
|
||||
res = append(res, nums[i+1])
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1313 struct {
|
||||
para1313
|
||||
ans1313
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1313 struct {
|
||||
nums []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1313 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
func Test_Problem1313(t *testing.T) {
|
||||
|
||||
qs := []question1313{
|
||||
|
||||
question1313{
|
||||
para1313{[]int{1, 2, 3, 4}},
|
||||
ans1313{[]int{2, 4, 4, 4}},
|
||||
},
|
||||
|
||||
question1313{
|
||||
para1313{[]int{1, 1, 2, 3}},
|
||||
ans1313{[]int{1, 3, 3}},
|
||||
},
|
||||
|
||||
question1313{
|
||||
para1313{[]int{}},
|
||||
ans1313{[]int{}},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1313------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1313, q.para1313
|
||||
fmt.Printf("【input】:%v ", p)
|
||||
fmt.Printf("【output】:%v \n", decompressRLElist(p.nums))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
60
leetcode/1313.Decompress-Run-Length-Encoded-List/README.md
Normal file
60
leetcode/1313.Decompress-Run-Length-Encoded-List/README.md
Normal file
@@ -0,0 +1,60 @@
|
||||
# [1313. Decompress Run-Length Encoded List](https://leetcode.com/problems/decompress-run-length-encoded-list/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
We are given a list `nums` of integers representing a list compressed with run-length encoding.
|
||||
|
||||
Consider each adjacent pair of elements `[freq, val] = [nums[2*i], nums[2*i+1]]` (with `i >= 0`). For each such pair, there are `freq` elements with value `val` concatenated in a sublist. Concatenate all the sublists from left to right to generate the decompressed list.
|
||||
|
||||
Return the decompressed list.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: nums = [1,2,3,4]
|
||||
Output: [2,4,4,4]
|
||||
Explanation: The first pair [1,2] means we have freq = 1 and val = 2 so we generate the array [2].
|
||||
The second pair [3,4] means we have freq = 3 and val = 4 so we generate [4,4,4].
|
||||
At the end the concatenation [2] + [4,4,4] is [2,4,4,4].
|
||||
```
|
||||
|
||||
**Example 2**:
|
||||
|
||||
```
|
||||
Input: nums = [1,1,2,3]
|
||||
Output: [1,3,3]
|
||||
```
|
||||
|
||||
**Constraints**:
|
||||
|
||||
- `2 <= nums.length <= 100`
|
||||
- `nums.length % 2 == 0`
|
||||
- `1 <= nums[i] <= 100`
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你一个以行程长度编码压缩的整数列表 nums 。考虑每对相邻的两个元素 [freq, val] = [nums[2*i], nums[2*i+1]] (其中 i >= 0 ),每一对都表示解压后子列表中有 freq 个值为 val 的元素,你需要从左到右连接所有子列表以生成解压后的列表。请你返回解压后的列表。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给定一个带编码长度的数组,要求解压这个数组。
|
||||
- 简单题。按照题目要求,下标从 0 开始,奇数位下标为前一个下标对应元素重复次数,那么就把这个元素 append 几次。最终输出解压后的数组即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
func decompressRLElist(nums []int) []int {
|
||||
res := []int{}
|
||||
for i := 0; i < len(nums); i += 2 {
|
||||
for j := 0; j < nums[i]; j++ {
|
||||
res = append(res, nums[i+1])
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
```
|
||||
@@ -0,0 +1,22 @@
|
||||
package leetcode
|
||||
|
||||
func getNoZeroIntegers(n int) []int {
|
||||
noZeroPair := []int{}
|
||||
for i := 1; i <= n/2; i++ {
|
||||
if isNoZero(i) && isNoZero(n-i) {
|
||||
noZeroPair = append(noZeroPair, []int{i, n - i}...)
|
||||
break
|
||||
}
|
||||
}
|
||||
return noZeroPair
|
||||
}
|
||||
|
||||
func isNoZero(n int) bool {
|
||||
for n != 0 {
|
||||
if n%10 == 0 {
|
||||
return false
|
||||
}
|
||||
n /= 10
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1317 struct {
|
||||
para1317
|
||||
ans1317
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1317 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1317 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
func Test_Problem1317(t *testing.T) {
|
||||
|
||||
qs := []question1317{
|
||||
|
||||
question1317{
|
||||
para1317{5},
|
||||
ans1317{[]int{1, 4}},
|
||||
},
|
||||
|
||||
question1317{
|
||||
para1317{0},
|
||||
ans1317{[]int{}},
|
||||
},
|
||||
|
||||
question1317{
|
||||
para1317{3},
|
||||
ans1317{[]int{1, 2}},
|
||||
},
|
||||
|
||||
question1317{
|
||||
para1317{1},
|
||||
ans1317{[]int{}},
|
||||
},
|
||||
|
||||
question1317{
|
||||
para1317{2},
|
||||
ans1317{[]int{1, 1}},
|
||||
},
|
||||
|
||||
question1317{
|
||||
para1317{11},
|
||||
ans1317{[]int{2, 9}},
|
||||
},
|
||||
|
||||
question1317{
|
||||
para1317{10000},
|
||||
ans1317{[]int{1, 9999}},
|
||||
},
|
||||
|
||||
question1317{
|
||||
para1317{69},
|
||||
ans1317{[]int{1, 68}},
|
||||
},
|
||||
|
||||
question1317{
|
||||
para1317{1010},
|
||||
ans1317{[]int{11, 999}},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1317------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1317, q.para1317
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, getNoZeroIntegers(p.one))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
# [1317. Convert Integer to the Sum of Two No-Zero Integers](https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given an integer `n`. No-Zero integer is a positive integer which **doesn't contain any 0** in its decimal representation.
|
||||
|
||||
Return *a list of two integers* `[A, B]` where:
|
||||
|
||||
- `A` and `B` are No-Zero integers.
|
||||
- `A + B = n`
|
||||
|
||||
It's guarateed that there is at least one valid solution. If there are many valid solutions you can return any of them.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: n = 2
|
||||
Output: [1,1]
|
||||
Explanation: A = 1, B = 1. A + B = n and both A and B don't contain any 0 in their decimal representation.
|
||||
```
|
||||
|
||||
**Example 2**:
|
||||
|
||||
```
|
||||
Input: n = 11
|
||||
Output: [2,9]
|
||||
```
|
||||
|
||||
**Example 3**:
|
||||
|
||||
```
|
||||
Input: n = 10000
|
||||
Output: [1,9999]
|
||||
```
|
||||
|
||||
**Example 4**:
|
||||
|
||||
```
|
||||
Input: n = 69
|
||||
Output: [1,68]
|
||||
```
|
||||
|
||||
**Example 5**:
|
||||
|
||||
```
|
||||
Input: n = 1010
|
||||
Output: [11,999]
|
||||
```
|
||||
|
||||
**Constraints**:
|
||||
|
||||
- `2 <= n <= 10^4`
|
||||
|
||||
## 题目大意
|
||||
|
||||
「无零整数」是十进制表示中 不含任何 0 的正整数。给你一个整数 n,请你返回一个 由两个整数组成的列表 [A, B],满足:
|
||||
|
||||
- A 和 B 都是无零整数
|
||||
- A + B = n
|
||||
|
||||
题目数据保证至少有一个有效的解决方案。如果存在多个有效解决方案,你可以返回其中任意一个。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给定一个整数 n,要求把它分解为 2 个十进制位中不含 0 的正整数且这两个正整数之和为 n。
|
||||
- 简单题。在 [1, n/2] 区间内搜索,只要有一组满足条件的解就 break。题目保证了至少有一组解,并且多组解返回任意一组即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
func getNoZeroIntegers(n int) []int {
|
||||
noZeroPair := []int{}
|
||||
for i := 1; i <= n/2; i++ {
|
||||
if isNoZero(i) && isNoZero(n-i) {
|
||||
noZeroPair = append(noZeroPair, []int{i, n - i}...)
|
||||
break
|
||||
}
|
||||
}
|
||||
return noZeroPair
|
||||
}
|
||||
|
||||
func isNoZero(n int) bool {
|
||||
for n != 0 {
|
||||
if n%10 == 0 {
|
||||
return false
|
||||
}
|
||||
n /= 10
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
```
|
||||
@@ -0,0 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import "strings"
|
||||
|
||||
func isPrefixOfWord(sentence string, searchWord string) int {
|
||||
for i, v := range strings.Split(sentence, " ") {
|
||||
if strings.HasPrefix(v, searchWord) {
|
||||
return i + 1
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1455 struct {
|
||||
para1455
|
||||
ans1455
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1455 struct {
|
||||
sentence string
|
||||
searchWord string
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1455 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem1455(t *testing.T) {
|
||||
|
||||
qs := []question1455{
|
||||
|
||||
question1455{
|
||||
para1455{"i love eating burger", "burg"},
|
||||
ans1455{4},
|
||||
},
|
||||
|
||||
question1455{
|
||||
para1455{"this problem is an easy problem", "pro"},
|
||||
ans1455{2},
|
||||
},
|
||||
|
||||
question1455{
|
||||
para1455{"i am tired", "you"},
|
||||
ans1455{-1},
|
||||
},
|
||||
|
||||
question1455{
|
||||
para1455{"i use triple pillow", "pill"},
|
||||
ans1455{4},
|
||||
},
|
||||
|
||||
question1455{
|
||||
para1455{"hello from the other side", "they"},
|
||||
ans1455{-1},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1455------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1455, q.para1455
|
||||
fmt.Printf("【input】:%v ", p)
|
||||
fmt.Printf("【output】:%v \n", isPrefixOfWord(p.sentence, p.searchWord))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
# [1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence](https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given a `sentence` that consists of some words separated by a **single space**, and a `searchWord`.
|
||||
|
||||
You have to check if `searchWord` is a prefix of any word in `sentence`.
|
||||
|
||||
Return *the index of the word* in `sentence` where `searchWord` is a prefix of this word (**1-indexed**).
|
||||
|
||||
If `searchWord` is a prefix of more than one word, return the index of the first word **(minimum index)**. If there is no such word return **-1**.
|
||||
|
||||
A **prefix** of a string `S` is any leading contiguous substring of `S`.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: sentence = "i love eating burger", searchWord = "burg"
|
||||
Output: 4
|
||||
Explanation: "burg" is prefix of "burger" which is the 4th word in the sentence.
|
||||
|
||||
```
|
||||
|
||||
**Example 2**:
|
||||
|
||||
```
|
||||
Input: sentence = "this problem is an easy problem", searchWord = "pro"
|
||||
Output: 2
|
||||
Explanation: "pro" is prefix of "problem" which is the 2nd and the 6th word in the sentence, but we return 2 as it's the minimal index.
|
||||
|
||||
```
|
||||
|
||||
**Example 3**:
|
||||
|
||||
```
|
||||
Input: sentence = "i am tired", searchWord = "you"
|
||||
Output: -1
|
||||
Explanation: "you" is not a prefix of any word in the sentence.
|
||||
|
||||
```
|
||||
|
||||
**Example 4**:
|
||||
|
||||
```
|
||||
Input: sentence = "i use triple pillow", searchWord = "pill"
|
||||
Output: 4
|
||||
|
||||
```
|
||||
|
||||
**Example 5**:
|
||||
|
||||
```
|
||||
Input: sentence = "hello from the other side", searchWord = "they"
|
||||
Output: -1
|
||||
|
||||
```
|
||||
|
||||
**Constraints**:
|
||||
|
||||
- `1 <= sentence.length <= 100`
|
||||
- `1 <= searchWord.length <= 10`
|
||||
- `sentence` consists of lowercase English letters and spaces.
|
||||
- `searchWord` consists of lowercase English letters.
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你一个字符串 sentence 作为句子并指定检索词为 searchWord ,其中句子由若干用 单个空格 分隔的单词组成。请你检查检索词 searchWord 是否为句子 sentence 中任意单词的前缀。
|
||||
|
||||
- 如果 searchWord 是某一个单词的前缀,则返回句子 sentence 中该单词所对应的下标(下标从 1 开始)。
|
||||
- 如果 searchWord 是多个单词的前缀,则返回匹配的第一个单词的下标(最小下标)。
|
||||
- 如果 searchWord 不是任何单词的前缀,则返回 -1 。
|
||||
|
||||
字符串 S 的 「前缀」是 S 的任何前导连续子字符串。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给出 2 个字符串,一个是匹配串,另外一个是句子。在句子里面查找带匹配串前缀的单词,并返回第一个匹配单词的下标。
|
||||
- 简单题。按照题意,扫描一遍句子,一次匹配即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
import "strings"
|
||||
|
||||
func isPrefixOfWord(sentence string, searchWord string) int {
|
||||
for i, v := range strings.Split(sentence, " ") {
|
||||
if strings.HasPrefix(v, searchWord) {
|
||||
return i + 1
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
```
|
||||
@@ -0,0 +1,14 @@
|
||||
package leetcode
|
||||
|
||||
func maxProduct(nums []int) int {
|
||||
max1, max2 := 0, 0
|
||||
for _, num := range nums {
|
||||
if num >= max1 {
|
||||
max2 = max1
|
||||
max1 = num
|
||||
} else if num <= max1 && num >= max2 {
|
||||
max2 = num
|
||||
}
|
||||
}
|
||||
return (max1 - 1) * (max2 - 1)
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1464 struct {
|
||||
para1464
|
||||
ans1464
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1464 struct {
|
||||
nums []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1464 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem1464(t *testing.T) {
|
||||
|
||||
qs := []question1464{
|
||||
|
||||
question1464{
|
||||
para1464{[]int{3, 4, 5, 2}},
|
||||
ans1464{12},
|
||||
},
|
||||
|
||||
question1464{
|
||||
para1464{[]int{1, 5, 4, 5}},
|
||||
ans1464{16},
|
||||
},
|
||||
|
||||
question1464{
|
||||
para1464{[]int{3, 7}},
|
||||
ans1464{12},
|
||||
},
|
||||
|
||||
question1464{
|
||||
para1464{[]int{1}},
|
||||
ans1464{0},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1464------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1464, q.para1464
|
||||
fmt.Printf("【input】:%v ", p)
|
||||
fmt.Printf("【output】:%v \n", maxProduct(p.nums))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
# [1464. Maximum Product of Two Elements in an Array](https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given the array of integers `nums`, you will choose two different indices `i` and `j` of that array. Return the maximum value of `(nums[i]-1)*(nums[j]-1)`.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: nums = [3,4,5,2]
|
||||
Output: 12
|
||||
Explanation: If you choose the indices i=1 and j=2 (indexed from 0), you will get the maximum value, that is, (nums[1]-1)*(nums[2]-1) = (4-1)*(5-1) = 3*4 = 12.
|
||||
|
||||
```
|
||||
|
||||
**Example 2**:
|
||||
|
||||
```
|
||||
Input: nums = [1,5,4,5]
|
||||
Output: 16
|
||||
Explanation: Choosing the indices i=1 and j=3 (indexed from 0), you will get the maximum value of (5-1)*(5-1) = 16.
|
||||
|
||||
```
|
||||
|
||||
**Example 3**:
|
||||
|
||||
```
|
||||
Input: nums = [3,7]
|
||||
Output: 12
|
||||
|
||||
```
|
||||
|
||||
**Constraints**:
|
||||
|
||||
- `2 <= nums.length <= 500`
|
||||
- `1 <= nums[i] <= 10^3`
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你一个整数数组 nums,请你选择数组的两个不同下标 i 和 j,使 (nums[i]-1)*(nums[j]-1) 取得最大值。请你计算并返回该式的最大值。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 简单题。循环一次,按照题意动态维护 2 个最大值,从而也使得 `(nums[i]-1)*(nums[j]-1)` 能取到最大值。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
func maxProduct(nums []int) int {
|
||||
max1, max2 := 0, 0
|
||||
for _, num := range nums {
|
||||
if num >= max1 {
|
||||
max2 = max1
|
||||
max1 = num
|
||||
} else if num <= max1 && num >= max2 {
|
||||
max2 = num
|
||||
}
|
||||
}
|
||||
return (max1 - 1) * (max2 - 1)
|
||||
}
|
||||
|
||||
```
|
||||
10
leetcode/1470.Shuffle-the-Array/1470. Shuffle the Array.go
Normal file
10
leetcode/1470.Shuffle-the-Array/1470. Shuffle the Array.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package leetcode
|
||||
|
||||
func shuffle(nums []int, n int) []int {
|
||||
result := make([]int, 0)
|
||||
for i := 0; i < n; i++ {
|
||||
result = append(result, nums[i])
|
||||
result = append(result, nums[n+i])
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1470 struct {
|
||||
para1470
|
||||
ans1470
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1470 struct {
|
||||
nums []int
|
||||
n int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1470 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
func Test_Problem1470(t *testing.T) {
|
||||
|
||||
qs := []question1470{
|
||||
|
||||
question1470{
|
||||
para1470{[]int{2, 5, 1, 3, 4, 7}, 3},
|
||||
ans1470{[]int{2, 3, 5, 4, 1, 7}},
|
||||
},
|
||||
|
||||
question1470{
|
||||
para1470{[]int{1, 2, 3, 4, 4, 3, 2, 1}, 4},
|
||||
ans1470{[]int{1, 4, 2, 3, 3, 2, 4, 1}},
|
||||
},
|
||||
|
||||
question1470{
|
||||
para1470{[]int{1, 1, 2, 2}, 2},
|
||||
ans1470{[]int{1, 2, 1, 2}},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1470------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1470, q.para1470
|
||||
fmt.Printf("【input】:%v 【output】:%v \n", p, shuffle(p.nums, p.n))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
64
leetcode/1470.Shuffle-the-Array/README.md
Normal file
64
leetcode/1470.Shuffle-the-Array/README.md
Normal file
@@ -0,0 +1,64 @@
|
||||
# [1470. Shuffle the Array](https://leetcode.com/problems/shuffle-the-array/)
|
||||
|
||||
## 题目
|
||||
|
||||
Given the array `nums` consisting of `2n` elements in the form `[x1,x2,...,xn,y1,y2,...,yn]`.
|
||||
|
||||
*Return the array in the form* `[x1,y1,x2,y2,...,xn,yn]`.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: nums = [2,5,1,3,4,7], n = 3
|
||||
Output: [2,3,5,4,1,7]
|
||||
Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7].
|
||||
|
||||
```
|
||||
|
||||
**Example 2**:
|
||||
|
||||
```
|
||||
Input: nums = [1,2,3,4,4,3,2,1], n = 4
|
||||
Output: [1,4,2,3,3,2,4,1]
|
||||
|
||||
```
|
||||
|
||||
**Example 3**:
|
||||
|
||||
```
|
||||
Input: nums = [1,1,2,2], n = 2
|
||||
Output: [1,2,1,2]
|
||||
|
||||
```
|
||||
|
||||
**Constraints**:
|
||||
|
||||
- `1 <= n <= 500`
|
||||
- `nums.length == 2n`
|
||||
- `1 <= nums[i] <= 10^3`
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你一个数组 nums ,数组中有 2n 个元素,按 [x1,x2,...,xn,y1,y2,...,yn] 的格式排列。请你将数组按 [x1,y1,x2,y2,...,xn,yn] 格式重新排列,返回重排后的数组。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给定一个 2n 的数组,把后 n 个元素插空放到前 n 个元素里面。输出最终完成的数组。
|
||||
- 简单题,按照题意插空即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
func shuffle(nums []int, n int) []int {
|
||||
result := make([]int, 0)
|
||||
for i := 0; i < n; i++ {
|
||||
result = append(result, nums[i])
|
||||
result = append(result, nums[n+i])
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
```
|
||||
69
website/content/ChapterFour/0009.Palindrome-Number.md
Normal file
69
website/content/ChapterFour/0009.Palindrome-Number.md
Normal file
@@ -0,0 +1,69 @@
|
||||
# [9. Palindrome Number](https://leetcode.com/problems/palindrome-number/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: 121
|
||||
Output: true
|
||||
```
|
||||
|
||||
**Example 2**:
|
||||
|
||||
```
|
||||
Input: -121
|
||||
Output: false
|
||||
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
|
||||
```
|
||||
|
||||
**Example 3**:
|
||||
|
||||
```
|
||||
Input: 10
|
||||
Output: false
|
||||
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
|
||||
```
|
||||
|
||||
**Follow up**:
|
||||
|
||||
Coud you solve it without converting the integer to a string?
|
||||
|
||||
## 题目大意
|
||||
|
||||
判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 判断一个整数是不是回文数。
|
||||
- 简单题。注意会有负数的情况,负数,个位数,10 都不是回文数。其他的整数再按照回文的规则判断。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
import "strconv"
|
||||
|
||||
func isPalindrome(x int) bool {
|
||||
if x < 0 {
|
||||
return false
|
||||
}
|
||||
if x < 10 {
|
||||
return true
|
||||
}
|
||||
s := strconv.Itoa(x)
|
||||
length := len(s)
|
||||
for i := 0; i <= length/2; i++ {
|
||||
if s[i] != s[length-1-i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
```
|
||||
132
website/content/ChapterFour/0013.Roman-to-Integer.md
Normal file
132
website/content/ChapterFour/0013.Roman-to-Integer.md
Normal file
@@ -0,0 +1,132 @@
|
||||
# [13. Roman to Integer](https://leetcode.com/problems/roman-to-integer/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Roman numerals are represented by seven different symbols: `I`, `V`, `X`, `L`, `C`, `D` and `M`.
|
||||
|
||||
```
|
||||
Symbol Value
|
||||
I 1
|
||||
V 5
|
||||
X 10
|
||||
L 50
|
||||
C 100
|
||||
D 500
|
||||
M 1000
|
||||
```
|
||||
|
||||
For example, two is written as `II` in Roman numeral, just two one's added together. Twelve is written as, `XII`, which is simply `X` + `II`. The number twenty seven is written as `XXVII`, which is `XX` + `V` + `II`.
|
||||
|
||||
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not `IIII`. Instead, the number four is written as `IV`. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as `IX`. There are six instances where subtraction is used:
|
||||
|
||||
- `I` can be placed before `V` (5) and `X` (10) to make 4 and 9.
|
||||
- `X` can be placed before `L` (50) and `C` (100) to make 40 and 90.
|
||||
- `C` can be placed before `D` (500) and `M` (1000) to make 400 and 900.
|
||||
|
||||
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: "III"
|
||||
Output: 3
|
||||
```
|
||||
|
||||
**Example 2**:
|
||||
|
||||
```
|
||||
Input: "IV"
|
||||
Output: 4
|
||||
```
|
||||
|
||||
**Example 3**:
|
||||
|
||||
```
|
||||
Input: "IX"
|
||||
Output: 9
|
||||
```
|
||||
|
||||
**Example 4**:
|
||||
|
||||
```
|
||||
Input: "LVIII"
|
||||
Output: 58
|
||||
Explanation: L = 50, V= 5, III = 3.
|
||||
```
|
||||
|
||||
**Example 5**:
|
||||
|
||||
```
|
||||
Input: "MCMXCIV"
|
||||
Output: 1994
|
||||
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
|
||||
```
|
||||
|
||||
## 题目大意
|
||||
|
||||
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。
|
||||
|
||||
```go
|
||||
|
||||
字符 数值
|
||||
I 1
|
||||
V 5
|
||||
X 10
|
||||
L 50
|
||||
C 100
|
||||
D 500
|
||||
M 1000
|
||||
|
||||
```
|
||||
|
||||
例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做 XXVII, 即为 XX + V + II 。
|
||||
|
||||
通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:
|
||||
|
||||
- I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
|
||||
- X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。
|
||||
- C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。
|
||||
|
||||
给定一个罗马数字,将其转换成整数。输入确保在 1 到 3999 的范围内。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给定一个罗马数字,将其转换成整数。输入确保在 1 到 3999 的范围内。
|
||||
- 简单题。按照题目中罗马数字的字符数值,计算出对应罗马数字的十进制数即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
var roman = map[string]int{
|
||||
"I": 1,
|
||||
"V": 5,
|
||||
"X": 10,
|
||||
"L": 50,
|
||||
"C": 100,
|
||||
"D": 500,
|
||||
"M": 1000,
|
||||
}
|
||||
|
||||
func romanToInt(s string) int {
|
||||
if s == "" {
|
||||
return 0
|
||||
}
|
||||
num, lastint, total := 0, 0, 0
|
||||
for i := 0; i < len(s); i++ {
|
||||
char := s[len(s)-(i+1) : len(s)-i]
|
||||
num = roman[char]
|
||||
if num < lastint {
|
||||
total = total - num
|
||||
} else {
|
||||
total = total + num
|
||||
}
|
||||
lastint = num
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
```
|
||||
76
website/content/ChapterFour/0067.Add-Binary.md
Normal file
76
website/content/ChapterFour/0067.Add-Binary.md
Normal file
@@ -0,0 +1,76 @@
|
||||
# [67. Add Binary](https://leetcode.com/problems/add-binary/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given two binary strings, return their sum (also a binary string).
|
||||
|
||||
The input strings are both **non-empty** and contains only characters `1` or `0`.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: a = "11", b = "1"
|
||||
Output: "100"
|
||||
```
|
||||
|
||||
**Example 2**:
|
||||
|
||||
```
|
||||
Input: a = "1010", b = "1011"
|
||||
Output: "10101"
|
||||
```
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你两个二进制字符串,返回它们的和(用二进制表示)。输入为 非空 字符串且只包含数字 1 和 0。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 要求输出 2 个二进制数的和,结果也用二进制表示。
|
||||
- 简单题。按照二进制的加法规则做加法即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func addBinary(a string, b string) string {
|
||||
if len(b) > len(a) {
|
||||
a, b = b, a
|
||||
}
|
||||
|
||||
res := make([]string, len(a)+1)
|
||||
i, j, k, c := len(a)-1, len(b)-1, len(a), 0
|
||||
for i >= 0 && j >= 0 {
|
||||
ai, _ := strconv.Atoi(string(a[i]))
|
||||
bj, _ := strconv.Atoi(string(b[j]))
|
||||
res[k] = strconv.Itoa((ai + bj + c) % 2)
|
||||
c = (ai + bj + c) / 2
|
||||
i--
|
||||
j--
|
||||
k--
|
||||
}
|
||||
|
||||
for i >= 0 {
|
||||
ai, _ := strconv.Atoi(string(a[i]))
|
||||
res[k] = strconv.Itoa((ai + c) % 2)
|
||||
c = (ai + c) / 2
|
||||
i--
|
||||
k--
|
||||
}
|
||||
|
||||
if c > 0 {
|
||||
res[k] = strconv.Itoa(c)
|
||||
}
|
||||
|
||||
return strings.Join(res, "")
|
||||
}
|
||||
|
||||
```
|
||||
80
website/content/ChapterFour/0168.Excel-Sheet-Column-Title.md
Normal file
80
website/content/ChapterFour/0168.Excel-Sheet-Column-Title.md
Normal file
@@ -0,0 +1,80 @@
|
||||
# [168. Excel Sheet Column Title](https://leetcode.com/problems/excel-sheet-column-title/)
|
||||
|
||||
## 题目
|
||||
|
||||
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
|
||||
|
||||
For example:
|
||||
|
||||
```
|
||||
1 -> A
|
||||
2 -> B
|
||||
3 -> C
|
||||
...
|
||||
26 -> Z
|
||||
27 -> AA
|
||||
28 -> AB
|
||||
...
|
||||
```
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: 1
|
||||
Output: "A"
|
||||
```
|
||||
|
||||
**Example 2**:
|
||||
|
||||
```
|
||||
Input: 28
|
||||
Output: "AB"
|
||||
```
|
||||
|
||||
**Example 3**:
|
||||
|
||||
```
|
||||
Input: 701
|
||||
Output: "ZY"
|
||||
```
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个正整数,返回它在 Excel 表中相对应的列名称。
|
||||
|
||||
例如,
|
||||
|
||||
1 -> A
|
||||
2 -> B
|
||||
3 -> C
|
||||
...
|
||||
26 -> Z
|
||||
27 -> AA
|
||||
28 -> AB
|
||||
...
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给定一个正整数,返回它在 Excel 表中的对应的列名称
|
||||
- 简单题。这一题就类似短除法的计算过程。以 26 进制的字母编码。按照短除法先除,然后余数逆序输出即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
func convertToTitle(n int) string {
|
||||
result := []byte{}
|
||||
for n > 0 {
|
||||
result = append(result, 'A'+byte((n-1)%26))
|
||||
n = (n - 1) / 26
|
||||
}
|
||||
for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 {
|
||||
result[i], result[j] = result[j], result[i]
|
||||
}
|
||||
return string(result)
|
||||
}
|
||||
|
||||
```
|
||||
@@ -0,0 +1,67 @@
|
||||
# [171. Excel Sheet Column Number](https://leetcode.com/problems/excel-sheet-column-number/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given a column title as appear in an Excel sheet, return its corresponding column number.
|
||||
|
||||
For example:
|
||||
|
||||
```
|
||||
A -> 1
|
||||
B -> 2
|
||||
C -> 3
|
||||
...
|
||||
Z -> 26
|
||||
AA -> 27
|
||||
AB -> 28
|
||||
...
|
||||
```
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: "A"
|
||||
Output: 1
|
||||
```
|
||||
|
||||
**Example 2**:
|
||||
|
||||
```
|
||||
Input: "AB"
|
||||
Output: 28
|
||||
```
|
||||
|
||||
**Example 3**:
|
||||
|
||||
```
|
||||
Input: "ZY"
|
||||
Output: 701
|
||||
```
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个 Excel 表格中的列名称,返回其相应的列序号。
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给出 Excel 中列的名称,输出其对应的列序号。
|
||||
- 简单题。这一题是第 168 题的逆序题。按照 26 进制还原成十进制即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
func titleToNumber(s string) int {
|
||||
val, res := 0, 0
|
||||
for i := 0; i < len(s); i++ {
|
||||
val = int(s[i] - 'A' + 1)
|
||||
res = res*26 + val
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
```
|
||||
47
website/content/ChapterFour/0258.Add-Digits.md
Normal file
47
website/content/ChapterFour/0258.Add-Digits.md
Normal file
@@ -0,0 +1,47 @@
|
||||
# [258. Add Digits](https://leetcode.com/problems/add-digits/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given a non-negative integer `num`, repeatedly add all its digits until the result has only one digit.
|
||||
|
||||
**Example**:
|
||||
|
||||
```
|
||||
Input: 38
|
||||
Output: 2
|
||||
Explanation: The process is like: 3 + 8 = 11, 1 + 1 = 2.
|
||||
Since 2 has only one digit, return it.
|
||||
```
|
||||
|
||||
**Follow up**: Could you do it without any loop/recursion in O(1) runtime?
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数。
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给定一个非负整数,反复加各个位上的数,直到结果为一位数为止,最后输出这一位数。
|
||||
- 简单题。按照题意循环累加即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
func addDigits(num int) int {
|
||||
for num > 9 {
|
||||
cur := 0
|
||||
for num != 0 {
|
||||
cur += num % 10
|
||||
num /= 10
|
||||
}
|
||||
num = cur
|
||||
}
|
||||
return num
|
||||
}
|
||||
|
||||
```
|
||||
@@ -0,0 +1,51 @@
|
||||
# [453. Minimum Moves to Equal Array Elements](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given a **non-empty** integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1.
|
||||
|
||||
**Example**:
|
||||
|
||||
```
|
||||
Input:
|
||||
[1,2,3]
|
||||
|
||||
Output:
|
||||
3
|
||||
|
||||
Explanation:
|
||||
Only three moves are needed (remember each move increments two elements):
|
||||
|
||||
[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
|
||||
```
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个长度为 n 的非空整数数组,找到让数组所有元素相等的最小移动次数。每次移动将会使 n - 1 个元素增加 1。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给定一个数组,要求输出让所有元素都相等的最小步数。每移动一步都会使得 n - 1 个元素 + 1 。
|
||||
- 数学题。这道题正着思考会考虑到排序或者暴力的方法上去。反过来思考一下,使得每个元素都相同,意思让所有元素的差异变为 0 。每次移动的过程中,都有 n - 1 个元素 + 1,那么没有 + 1 的那个元素和其他 n - 1 个元素相对差异就缩小了。所以这道题让所有元素都变为相等的最少步数,即等于让所有元素相对差异减少到最小的那个数。想到这里,此题就可以优雅的解出来了。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
import "math"
|
||||
|
||||
func minMoves(nums []int) int {
|
||||
sum, min, l := 0, math.MaxInt32, len(nums)
|
||||
for _, v := range nums {
|
||||
sum += v
|
||||
if min > v {
|
||||
min = v
|
||||
}
|
||||
}
|
||||
return sum - min*l
|
||||
}
|
||||
|
||||
```
|
||||
64
website/content/ChapterFour/0507.Perfect-Number.md
Normal file
64
website/content/ChapterFour/0507.Perfect-Number.md
Normal file
@@ -0,0 +1,64 @@
|
||||
# [507. Perfect Number](https://leetcode.com/problems/perfect-number/)
|
||||
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
We define the Perfect Number is a **positive** integer that is equal to the sum of all its **positive** divisors except itself.
|
||||
|
||||
Now, given an
|
||||
|
||||
**integer**
|
||||
|
||||
n, write a function that returns true when it is a perfect number and false when it is not.
|
||||
|
||||
**Example**:
|
||||
|
||||
```
|
||||
Input: 28
|
||||
Output: True
|
||||
Explanation: 28 = 1 + 2 + 4 + 7 + 14
|
||||
```
|
||||
|
||||
**Note**: The input number **n** will not exceed 100,000,000. (1e8)
|
||||
|
||||
## 题目大意
|
||||
|
||||
对于一个 正整数,如果它和除了它自身以外的所有正因子之和相等,我们称它为“完美数”。给定一个 整数 n, 如果他是完美数,返回 True,否则返回 False
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给定一个整数,要求判断这个数是不是完美数。整数的取值范围小于 1e8 。
|
||||
- 简单题。按照题意描述,先获取这个整数的所有正因子,如果正因子的和等于原来这个数,那么它就是完美数。
|
||||
- 这一题也可以打表,1e8 以下的完美数其实并不多,就 5 个。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
import "math"
|
||||
|
||||
// 方法一
|
||||
func checkPerfectNumber(num int) bool {
|
||||
if num <= 1 {
|
||||
return false
|
||||
}
|
||||
sum, bound := 1, int(math.Sqrt(float64(num)))+1
|
||||
for i := 2; i < bound; i++ {
|
||||
if num%i != 0 {
|
||||
continue
|
||||
}
|
||||
corrDiv := num / i
|
||||
sum += corrDiv + i
|
||||
}
|
||||
return sum == num
|
||||
}
|
||||
|
||||
// 方法二 打表
|
||||
func checkPerfectNumber_(num int) bool {
|
||||
return num == 6 || num == 28 || num == 496 || num == 8128 || num == 33550336
|
||||
}
|
||||
|
||||
```
|
||||
@@ -0,0 +1,73 @@
|
||||
# [537. Complex Number Multiplication](https://leetcode.com/problems/complex-number-multiplication/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given two strings representing two [complex numbers](https://en.wikipedia.org/wiki/Complex_number).
|
||||
|
||||
You need to return a string representing their multiplication. Note i2 = -1 according to the definition.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: "1+1i", "1+1i"
|
||||
Output: "0+2i"
|
||||
Explanation: (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i, and you need convert it to the form of 0+2i.
|
||||
```
|
||||
|
||||
**Example 2**:
|
||||
|
||||
```
|
||||
Input: "1+-1i", "1+-1i"
|
||||
Output: "0+-2i"
|
||||
Explanation: (1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i, and you need convert it to the form of 0+-2i.
|
||||
```
|
||||
|
||||
**Note**:
|
||||
|
||||
1. The input strings will not have extra blank.
|
||||
2. The input strings will be given in the form of **a+bi**, where the integer **a** and **b** will both belong to the range of [-100, 100]. And **the output should be also in this form**.
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定两个表示复数的字符串。返回表示它们乘积的字符串。注意,根据定义 i^2 = -1 。
|
||||
|
||||
注意:
|
||||
|
||||
- 输入字符串不包含额外的空格。
|
||||
- 输入字符串将以 a+bi 的形式给出,其中整数 a 和 b 的范围均在 [-100, 100] 之间。输出也应当符合这种形式。
|
||||
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给定 2 个字符串,要求这两个复数的乘积,输出也是字符串格式。
|
||||
- 数学题。按照复数的运算法则,i^2 = -1,最后输出字符串结果即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func complexNumberMultiply(a string, b string) string {
|
||||
realA, imagA := parse(a)
|
||||
realB, imagB := parse(b)
|
||||
real := realA*realB - imagA*imagB
|
||||
imag := realA*imagB + realB*imagA
|
||||
return strconv.Itoa(real) + "+" + strconv.Itoa(imag) + "i"
|
||||
}
|
||||
|
||||
func parse(s string) (int, int) {
|
||||
ss := strings.Split(s, "+")
|
||||
r, _ := strconv.Atoi(ss[0])
|
||||
i, _ := strconv.Atoi(ss[1][:len(ss[1])-1])
|
||||
return r, i
|
||||
}
|
||||
|
||||
```
|
||||
58
website/content/ChapterFour/0561.Array-Partition-I.md
Normal file
58
website/content/ChapterFour/0561.Array-Partition-I.md
Normal file
@@ -0,0 +1,58 @@
|
||||
# [561. Array Partition I](https://leetcode.com/problems/array-partition-i/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given an array of **2n** integers, your task is to group these integers into **n** pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: [1,4,3,2]
|
||||
|
||||
Output: 4
|
||||
Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).
|
||||
```
|
||||
|
||||
**Note**:
|
||||
|
||||
1. **n** is a positive integer, which is in the range of [1, 10000].
|
||||
2. All the integers in the array will be in the range of [-10000, 10000].
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最大。
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给定一个 2n 个数组,要求把它们分为 n 组一行,求出各组最小值的总和的最大值。
|
||||
- 由于题目给的数据范围不大,[-10000, 10000],所以我们可以考虑用一个哈希表数组,里面存储 i - 10000 元素的频次,偏移量是 10000。这个哈希表能按递增的顺序访问数组,这样可以减少排序的耗时。题目要求求出分组以后求和的最大值,那么所有偏小的元素尽量都安排在一组里面,这样取 min 以后,对最大和影响不大。例如,(1 , 1) 这样安排在一起,min 以后就是 1 。但是如果把相差很大的两个元素安排到一起,那么较大的那个元素就“牺牲”了。例如,(1 , 10000),取 min 以后就是 1,于是 10000 就“牺牲”了。所以需要优先考虑较小值。
|
||||
- 较小值出现的频次可能是奇数也可能是偶数。如果是偶数,那比较简单,把它们俩俩安排在一起就可以了。如果是奇数,那么它会落单一次,落单的那个需要和距离它最近的一个元素进行配对,这样对最终的和影响最小。较小值如果是奇数,那么就会影响后面元素的选择,后面元素如果是偶数,由于需要一个元素和前面的较小值配对,所以它剩下的又是奇数个。这个影响会依次传递到后面。所以用一个 flag 标记,如果当前集合中有剩余元素将被再次考虑,则此标志设置为 1。在从下一组中选择元素时,会考虑已考虑的相同额外元素。
|
||||
- 最后扫描过程中动态的维护 sum 值就可以了。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
func arrayPairSum(nums []int) int {
|
||||
array := [20001]int{}
|
||||
for i := 0; i < len(nums); i++ {
|
||||
array[nums[i]+10000]++
|
||||
}
|
||||
flag, sum := true, 0
|
||||
for i := 0; i < len(array); i++ {
|
||||
for array[i] > 0 {
|
||||
if flag {
|
||||
sum = sum + i - 10000
|
||||
}
|
||||
flag = !flag
|
||||
array[i]--
|
||||
}
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
```
|
||||
82
website/content/ChapterFour/0598.Range-Addition-II.md
Normal file
82
website/content/ChapterFour/0598.Range-Addition-II.md
Normal file
@@ -0,0 +1,82 @@
|
||||
# [598. Range Addition II](https://leetcode.com/problems/range-addition-ii/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given an m * n matrix **M** initialized with all **0**'s and several update operations.
|
||||
|
||||
Operations are represented by a 2D array, and each operation is represented by an array with two **positive** integers **a** and **b**, which means **M[i][j]** should be **added by one** for all **0 <= i < a** and **0 <= j < b**.
|
||||
|
||||
You need to count and return the number of maximum integers in the matrix after performing all the operations.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input:
|
||||
m = 3, n = 3
|
||||
operations = [[2,2],[3,3]]
|
||||
Output: 4
|
||||
Explanation:
|
||||
Initially, M =
|
||||
[[0, 0, 0],
|
||||
[0, 0, 0],
|
||||
[0, 0, 0]]
|
||||
|
||||
After performing [2,2], M =
|
||||
[[1, 1, 0],
|
||||
[1, 1, 0],
|
||||
[0, 0, 0]]
|
||||
|
||||
After performing [3,3], M =
|
||||
[[2, 2, 1],
|
||||
[2, 2, 1],
|
||||
[1, 1, 1]]
|
||||
|
||||
So the maximum integer in M is 2, and there are four of it in M. So return 4.
|
||||
```
|
||||
|
||||
**Note**:
|
||||
|
||||
1. The range of m and n is [1,40000].
|
||||
2. The range of a is [1,m], and the range of b is [1,n].
|
||||
3. The range of operations size won't exceed 10,000.
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个初始元素全部为 0,大小为 m*n 的矩阵 M 以及在 M 上的一系列更新操作。操作用二维数组表示,其中的每个操作用一个含有两个正整数 a 和 b 的数组表示,含义是将所有符合 0 <= i < a 以及 0 <= j < b 的元素 M[i][j] 的值都增加 1。在执行给定的一系列操作后,你需要返回矩阵中含有最大整数的元素个数。
|
||||
|
||||
注意:
|
||||
|
||||
- m 和 n 的范围是 [1,40000]。
|
||||
- a 的范围是 [1,m],b 的范围是 [1,n]。
|
||||
- 操作数目不超过 10000。
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给定一个初始都为 0 的 m * n 的矩阵,和一个操作数组。经过一系列的操作以后,最终输出矩阵中最大整数的元素个数。每次操作都使得一个矩形内的元素都 + 1 。
|
||||
- 这一题乍一看像线段树的区间覆盖问题,但是实际上很简单。如果此题是任意的矩阵,那就可能用到线段树了。这一题每个矩阵的起点都包含 [0 , 0] 这个元素,也就是说每次操作都会影响第一个元素。那么这道题就很简单了。经过 n 次操作以后,被覆盖次数最多的矩形区间,一定就是最大整数所在的区间。由于起点都是第一个元素,所以我们只用关心矩形的右下角那个坐标。右下角怎么计算呢?只用每次动态的维护一下矩阵长和宽的最小值即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
func maxCount(m int, n int, ops [][]int) int {
|
||||
minM, minN := m, n
|
||||
for _, op := range ops {
|
||||
minM = min(minM, op[0])
|
||||
minN = min(minN, op[1])
|
||||
}
|
||||
return minM * minN
|
||||
}
|
||||
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
```
|
||||
70
website/content/ChapterFour/0812.Largest-Triangle-Area.md
Normal file
70
website/content/ChapterFour/0812.Largest-Triangle-Area.md
Normal file
@@ -0,0 +1,70 @@
|
||||
# [812. Largest Triangle Area](https://leetcode.com/problems/largest-triangle-area/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
You have a list of points in the plane. Return the area of the largest triangle that can be formed by any 3 of the points.
|
||||
|
||||
```
|
||||
Example:
|
||||
Input: points = [[0,0],[0,1],[1,0],[0,2],[2,0]]
|
||||
Output: 2
|
||||
Explanation:
|
||||
The five points are show in the figure below. The red triangle is the largest.
|
||||
```
|
||||
|
||||

|
||||
|
||||
**Notes**:
|
||||
|
||||
- `3 <= points.length <= 50`.
|
||||
- No points will be duplicated.
|
||||
- `-50 <= points[i][j] <= 50`.
|
||||
- Answers within `10^-6` of the true value will be accepted as correct.
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定包含多个点的集合,从其中取三个点组成三角形,返回能组成的最大三角形的面积。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给出一组点的坐标,要求找出能组成三角形面积最大的点集合,输出这个最大面积。
|
||||
- 数学题。按照数学定义,分别计算这些能构成三角形的点形成的三角形面积,最终输出最大面积即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
func largestTriangleArea(points [][]int) float64 {
|
||||
maxArea, n := 0.0, len(points)
|
||||
for i := 0; i < n; i++ {
|
||||
for j := i + 1; j < n; j++ {
|
||||
for k := j + 1; k < n; k++ {
|
||||
maxArea = max(maxArea, area(points[i], points[j], points[k]))
|
||||
}
|
||||
}
|
||||
}
|
||||
return maxArea
|
||||
}
|
||||
|
||||
func area(p1, p2, p3 []int) float64 {
|
||||
return abs(p1[0]*p2[1]+p2[0]*p3[1]+p3[0]*p1[1]-p1[0]*p3[1]-p2[0]*p1[1]-p3[0]*p2[1]) / 2
|
||||
}
|
||||
|
||||
func abs(num int) float64 {
|
||||
if num < 0 {
|
||||
num = -num
|
||||
}
|
||||
return float64(num)
|
||||
}
|
||||
|
||||
func max(a, b float64) float64 {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
```
|
||||
63
website/content/ChapterFour/0832.Flipping-an-Image.md
Normal file
63
website/content/ChapterFour/0832.Flipping-an-Image.md
Normal file
@@ -0,0 +1,63 @@
|
||||
# [832. Flipping an Image](https://leetcode.com/problems/flipping-an-image/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given a binary matrix `A`, we want to flip the image horizontally, then invert it, and return the resulting image.
|
||||
|
||||
To flip an image horizontally means that each row of the image is reversed. For example, flipping `[1, 1, 0]` horizontally results in `[0, 1, 1]`.
|
||||
|
||||
To invert an image means that each `0` is replaced by `1`, and each `1` is replaced by `0`. For example, inverting `[0, 1, 1]` results in `[1, 0, 0]`.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: [[1,1,0],[1,0,1],[0,0,0]]
|
||||
Output: [[1,0,0],[0,1,0],[1,1,1]]
|
||||
Explanation: First reverse each row: [[0,1,1],[1,0,1],[0,0,0]].
|
||||
Then, invert the image: [[1,0,0],[0,1,0],[1,1,1]]
|
||||
```
|
||||
|
||||
**Example 2**:
|
||||
|
||||
```
|
||||
Input: [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]
|
||||
Output: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
|
||||
Explanation: First reverse each row: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]].
|
||||
Then invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
|
||||
```
|
||||
|
||||
**Notes**:
|
||||
|
||||
- `1 <= A.length = A[0].length <= 20`
|
||||
- `0 <= A[i][j] <= 1`
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果。水平翻转图片就是将图片的每一行都进行翻转,即逆序。例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, 1]。反转图片的意思是图片中的 0 全部被 1 替换, 1 全部被 0 替换。例如,反转 [0, 1, 1] 的结果是 [1, 0, 0]。
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给定一个二进制矩阵,要求先水平翻转,然后再反转( 1→0 , 0→1 )。
|
||||
- 简单题,按照题意先水平翻转,再反转即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
func flipAndInvertImage(A [][]int) [][]int {
|
||||
for i := 0; i < len(A); i++ {
|
||||
for a, b := 0, len(A[i])-1; a < b; a, b = a+1, b-1 {
|
||||
A[i][a], A[i][b] = A[i][b], A[i][a]
|
||||
}
|
||||
for a := 0; a < len(A[i]); a++ {
|
||||
A[i][a] = (A[i][a] + 1) % 2
|
||||
}
|
||||
}
|
||||
return A
|
||||
}
|
||||
|
||||
```
|
||||
108
website/content/ChapterFour/0892.Surface-Area-of-3D-Shapes.md
Normal file
108
website/content/ChapterFour/0892.Surface-Area-of-3D-Shapes.md
Normal file
@@ -0,0 +1,108 @@
|
||||
# [892. Surface Area of 3D Shapes](https://leetcode.com/problems/surface-area-of-3d-shapes/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
On a `N * N` grid, we place some `1 * 1 * 1` cubes.
|
||||
|
||||
Each value `v = grid[i][j]` represents a tower of `v` cubes placed on top of grid cell `(i, j)`.
|
||||
|
||||
Return the total surface area of the resulting shapes.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: [[2]]
|
||||
Output: 10
|
||||
```
|
||||
|
||||
**Example 2**:
|
||||
|
||||
```
|
||||
Input: [[1,2],[3,4]]
|
||||
Output: 34
|
||||
```
|
||||
|
||||
**Example 3**:
|
||||
|
||||
```
|
||||
Input: [[1,0],[0,2]]
|
||||
Output: 16
|
||||
```
|
||||
|
||||
**Example 4**:
|
||||
|
||||
```
|
||||
Input: [[1,1,1],[1,0,1],[1,1,1]]
|
||||
Output: 32
|
||||
```
|
||||
|
||||
**Example 5**:
|
||||
|
||||
```
|
||||
Input: [[2,2,2],[2,1,2],[2,2,2]]
|
||||
Output: 46
|
||||
```
|
||||
|
||||
**Note**:
|
||||
|
||||
- `1 <= N <= 50`
|
||||
- `0 <= grid[i][j] <= 50`
|
||||
|
||||
## 题目大意
|
||||
|
||||
在 N * N 的网格上,我们放置一些 1 * 1 * 1 的立方体。每个值 v = grid[i][j] 表示 v 个正方体叠放在对应单元格 (i, j) 上。请你返回最终形体的表面积。
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给定一个网格数组,数组里面装的是立方体叠放在所在的单元格,求最终这些叠放的立方体的表面积。
|
||||
- 简单题。按照题目意思,找到叠放时,重叠的面,然后用总表面积减去这些重叠的面积即为最终答案。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
func surfaceArea(grid [][]int) int {
|
||||
area := 0
|
||||
for i := 0; i < len(grid); i++ {
|
||||
for j := 0; j < len(grid[0]); j++ {
|
||||
if grid[i][j] == 0 {
|
||||
continue
|
||||
}
|
||||
area += grid[i][j]*4 + 2
|
||||
// up
|
||||
if i > 0 {
|
||||
m := min(grid[i][j], grid[i-1][j])
|
||||
area -= m
|
||||
}
|
||||
// down
|
||||
if i < len(grid)-1 {
|
||||
m := min(grid[i][j], grid[i+1][j])
|
||||
area -= m
|
||||
}
|
||||
// left
|
||||
if j > 0 {
|
||||
m := min(grid[i][j], grid[i][j-1])
|
||||
area -= m
|
||||
}
|
||||
// right
|
||||
if j < len(grid[i])-1 {
|
||||
m := min(grid[i][j], grid[i][j+1])
|
||||
area -= m
|
||||
}
|
||||
}
|
||||
}
|
||||
return area
|
||||
}
|
||||
|
||||
func min(a, b int) int {
|
||||
if a > b {
|
||||
return b
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
```
|
||||
@@ -0,0 +1,78 @@
|
||||
# [949. Largest Time for Given Digits](https://leetcode.com/problems/largest-time-for-given-digits/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given an array of 4 digits, return the largest 24 hour time that can be made.
|
||||
|
||||
The smallest 24 hour time is 00:00, and the largest is 23:59. Starting from 00:00, a time is larger if more time has elapsed since midnight.
|
||||
|
||||
Return the answer as a string of length 5. If no valid time can be made, return an empty string.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: [1,2,3,4]
|
||||
Output: "23:41"
|
||||
```
|
||||
|
||||
**Example 2**:
|
||||
|
||||
```
|
||||
Input: [5,5,5,5]
|
||||
Output: ""
|
||||
```
|
||||
|
||||
**Note**:
|
||||
|
||||
1. `A.length == 4`
|
||||
2. `0 <= A[i] <= 9`
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个由 4 位数字组成的数组,返回可以设置的符合 24 小时制的最大时间。最小的 24 小时制时间是 00:00,而最大的是 23:59。从 00:00 (午夜)开始算起,过得越久,时间越大。以长度为 5 的字符串返回答案。如果不能确定有效时间,则返回空字符串。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给出 4 个数字,要求返回一个字符串,代表由这 4 个数字能组成的最大 24 小时制的时间。
|
||||
- 简单题,这一题直接暴力枚举就可以了。依次检查给出的 4 个数字每个排列组合是否是时间合法的。例如检查 10 * A[i] + A[j] 是不是小于 24, 10 * A[k] + A[l] 是不是小于 60。如果合法且比目前存在的最大时间更大,就更新这个最大时间。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
import "fmt"
|
||||
|
||||
func largestTimeFromDigits(A []int) string {
|
||||
flag, res := false, 0
|
||||
for i := 0; i < 4; i++ {
|
||||
for j := 0; j < 4; j++ {
|
||||
if i == j {
|
||||
continue
|
||||
}
|
||||
for k := 0; k < 4; k++ {
|
||||
if i == k || j == k {
|
||||
continue
|
||||
}
|
||||
l := 6 - i - j - k
|
||||
hour := A[i]*10 + A[j]
|
||||
min := A[k]*10 + A[l]
|
||||
if hour < 24 && min < 60 {
|
||||
if hour*60+min >= res {
|
||||
res = hour*60 + min
|
||||
flag = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if flag {
|
||||
return fmt.Sprintf("%02d:%02d", res/60, res%60)
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
49
website/content/ChapterFour/1037.Valid-Boomerang.md
Normal file
49
website/content/ChapterFour/1037.Valid-Boomerang.md
Normal file
@@ -0,0 +1,49 @@
|
||||
# [1037. Valid Boomerang](https://leetcode.com/problems/valid-boomerang/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
A *boomerang* is a set of 3 points that are all distinct and **not** in a straight line.
|
||||
|
||||
Given a list of three points in the plane, return whether these points are a boomerang.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: [[1,1],[2,3],[3,2]]
|
||||
Output: true
|
||||
```
|
||||
|
||||
**Example 2**:
|
||||
|
||||
```
|
||||
Input: [[1,1],[2,2],[3,3]]
|
||||
Output: false
|
||||
```
|
||||
|
||||
**Note**:
|
||||
|
||||
1. `points.length == 3`
|
||||
2. `points[i].length == 2`
|
||||
3. `0 <= points[i][j] <= 100`
|
||||
|
||||
## 题目大意
|
||||
|
||||
回旋镖定义为一组三个点,这些点各不相同且不在一条直线上。给出平面上三个点组成的列表,判断这些点是否可以构成回旋镖。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 判断给出的 3 组点能否满足回旋镖。
|
||||
- 简单题。判断 3 个点组成的 2 条直线的斜率是否相等。由于斜率的计算是除法,还可能遇到分母为 0 的情况,那么可以转换成乘法,交叉相乘再判断是否相等,就可以省去判断分母为 0 的情况了,代码也简洁成一行了。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
func isBoomerang(points [][]int) bool {
|
||||
return (points[0][0]-points[1][0])*(points[0][1]-points[2][1]) != (points[0][0]-points[2][0])*(points[0][1]-points[1][1])
|
||||
}
|
||||
|
||||
```
|
||||
@@ -0,0 +1,60 @@
|
||||
# [1313. Decompress Run-Length Encoded List](https://leetcode.com/problems/decompress-run-length-encoded-list/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
We are given a list `nums` of integers representing a list compressed with run-length encoding.
|
||||
|
||||
Consider each adjacent pair of elements `[freq, val] = [nums[2*i], nums[2*i+1]]` (with `i >= 0`). For each such pair, there are `freq` elements with value `val` concatenated in a sublist. Concatenate all the sublists from left to right to generate the decompressed list.
|
||||
|
||||
Return the decompressed list.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: nums = [1,2,3,4]
|
||||
Output: [2,4,4,4]
|
||||
Explanation: The first pair [1,2] means we have freq = 1 and val = 2 so we generate the array [2].
|
||||
The second pair [3,4] means we have freq = 3 and val = 4 so we generate [4,4,4].
|
||||
At the end the concatenation [2] + [4,4,4] is [2,4,4,4].
|
||||
```
|
||||
|
||||
**Example 2**:
|
||||
|
||||
```
|
||||
Input: nums = [1,1,2,3]
|
||||
Output: [1,3,3]
|
||||
```
|
||||
|
||||
**Constraints**:
|
||||
|
||||
- `2 <= nums.length <= 100`
|
||||
- `nums.length % 2 == 0`
|
||||
- `1 <= nums[i] <= 100`
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你一个以行程长度编码压缩的整数列表 nums 。考虑每对相邻的两个元素 [freq, val] = [nums[2*i], nums[2*i+1]] (其中 i >= 0 ),每一对都表示解压后子列表中有 freq 个值为 val 的元素,你需要从左到右连接所有子列表以生成解压后的列表。请你返回解压后的列表。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给定一个带编码长度的数组,要求解压这个数组。
|
||||
- 简单题。按照题目要求,下标从 0 开始,奇数位下标为前一个下标对应元素重复次数,那么就把这个元素 append 几次。最终输出解压后的数组即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
func decompressRLElist(nums []int) []int {
|
||||
res := []int{}
|
||||
for i := 0; i < len(nums); i += 2 {
|
||||
for j := 0; j < nums[i]; j++ {
|
||||
res = append(res, nums[i+1])
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
```
|
||||
@@ -0,0 +1,96 @@
|
||||
# [1317. Convert Integer to the Sum of Two No-Zero Integers](https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given an integer `n`. No-Zero integer is a positive integer which **doesn't contain any 0** in its decimal representation.
|
||||
|
||||
Return *a list of two integers* `[A, B]` where:
|
||||
|
||||
- `A` and `B` are No-Zero integers.
|
||||
- `A + B = n`
|
||||
|
||||
It's guarateed that there is at least one valid solution. If there are many valid solutions you can return any of them.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: n = 2
|
||||
Output: [1,1]
|
||||
Explanation: A = 1, B = 1. A + B = n and both A and B don't contain any 0 in their decimal representation.
|
||||
```
|
||||
|
||||
**Example 2**:
|
||||
|
||||
```
|
||||
Input: n = 11
|
||||
Output: [2,9]
|
||||
```
|
||||
|
||||
**Example 3**:
|
||||
|
||||
```
|
||||
Input: n = 10000
|
||||
Output: [1,9999]
|
||||
```
|
||||
|
||||
**Example 4**:
|
||||
|
||||
```
|
||||
Input: n = 69
|
||||
Output: [1,68]
|
||||
```
|
||||
|
||||
**Example 5**:
|
||||
|
||||
```
|
||||
Input: n = 1010
|
||||
Output: [11,999]
|
||||
```
|
||||
|
||||
**Constraints**:
|
||||
|
||||
- `2 <= n <= 10^4`
|
||||
|
||||
## 题目大意
|
||||
|
||||
「无零整数」是十进制表示中 不含任何 0 的正整数。给你一个整数 n,请你返回一个 由两个整数组成的列表 [A, B],满足:
|
||||
|
||||
- A 和 B 都是无零整数
|
||||
- A + B = n
|
||||
|
||||
题目数据保证至少有一个有效的解决方案。如果存在多个有效解决方案,你可以返回其中任意一个。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给定一个整数 n,要求把它分解为 2 个十进制位中不含 0 的正整数且这两个正整数之和为 n。
|
||||
- 简单题。在 [1, n/2] 区间内搜索,只要有一组满足条件的解就 break。题目保证了至少有一组解,并且多组解返回任意一组即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
func getNoZeroIntegers(n int) []int {
|
||||
noZeroPair := []int{}
|
||||
for i := 1; i <= n/2; i++ {
|
||||
if isNoZero(i) && isNoZero(n-i) {
|
||||
noZeroPair = append(noZeroPair, []int{i, n - i}...)
|
||||
break
|
||||
}
|
||||
}
|
||||
return noZeroPair
|
||||
}
|
||||
|
||||
func isNoZero(n int) bool {
|
||||
for n != 0 {
|
||||
if n%10 == 0 {
|
||||
return false
|
||||
}
|
||||
n /= 10
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
```
|
||||
@@ -0,0 +1,98 @@
|
||||
# [1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence](https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given a `sentence` that consists of some words separated by a **single space**, and a `searchWord`.
|
||||
|
||||
You have to check if `searchWord` is a prefix of any word in `sentence`.
|
||||
|
||||
Return *the index of the word* in `sentence` where `searchWord` is a prefix of this word (**1-indexed**).
|
||||
|
||||
If `searchWord` is a prefix of more than one word, return the index of the first word **(minimum index)**. If there is no such word return **-1**.
|
||||
|
||||
A **prefix** of a string `S` is any leading contiguous substring of `S`.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: sentence = "i love eating burger", searchWord = "burg"
|
||||
Output: 4
|
||||
Explanation: "burg" is prefix of "burger" which is the 4th word in the sentence.
|
||||
|
||||
```
|
||||
|
||||
**Example 2**:
|
||||
|
||||
```
|
||||
Input: sentence = "this problem is an easy problem", searchWord = "pro"
|
||||
Output: 2
|
||||
Explanation: "pro" is prefix of "problem" which is the 2nd and the 6th word in the sentence, but we return 2 as it's the minimal index.
|
||||
|
||||
```
|
||||
|
||||
**Example 3**:
|
||||
|
||||
```
|
||||
Input: sentence = "i am tired", searchWord = "you"
|
||||
Output: -1
|
||||
Explanation: "you" is not a prefix of any word in the sentence.
|
||||
|
||||
```
|
||||
|
||||
**Example 4**:
|
||||
|
||||
```
|
||||
Input: sentence = "i use triple pillow", searchWord = "pill"
|
||||
Output: 4
|
||||
|
||||
```
|
||||
|
||||
**Example 5**:
|
||||
|
||||
```
|
||||
Input: sentence = "hello from the other side", searchWord = "they"
|
||||
Output: -1
|
||||
|
||||
```
|
||||
|
||||
**Constraints**:
|
||||
|
||||
- `1 <= sentence.length <= 100`
|
||||
- `1 <= searchWord.length <= 10`
|
||||
- `sentence` consists of lowercase English letters and spaces.
|
||||
- `searchWord` consists of lowercase English letters.
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你一个字符串 sentence 作为句子并指定检索词为 searchWord ,其中句子由若干用 单个空格 分隔的单词组成。请你检查检索词 searchWord 是否为句子 sentence 中任意单词的前缀。
|
||||
|
||||
- 如果 searchWord 是某一个单词的前缀,则返回句子 sentence 中该单词所对应的下标(下标从 1 开始)。
|
||||
- 如果 searchWord 是多个单词的前缀,则返回匹配的第一个单词的下标(最小下标)。
|
||||
- 如果 searchWord 不是任何单词的前缀,则返回 -1 。
|
||||
|
||||
字符串 S 的 「前缀」是 S 的任何前导连续子字符串。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给出 2 个字符串,一个是匹配串,另外一个是句子。在句子里面查找带匹配串前缀的单词,并返回第一个匹配单词的下标。
|
||||
- 简单题。按照题意,扫描一遍句子,一次匹配即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
import "strings"
|
||||
|
||||
func isPrefixOfWord(sentence string, searchWord string) int {
|
||||
for i, v := range strings.Split(sentence, " ") {
|
||||
if strings.HasPrefix(v, searchWord) {
|
||||
return i + 1
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
```
|
||||
@@ -0,0 +1,66 @@
|
||||
# [1464. Maximum Product of Two Elements in an Array](https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given the array of integers `nums`, you will choose two different indices `i` and `j` of that array. Return the maximum value of `(nums[i]-1)*(nums[j]-1)`.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: nums = [3,4,5,2]
|
||||
Output: 12
|
||||
Explanation: If you choose the indices i=1 and j=2 (indexed from 0), you will get the maximum value, that is, (nums[1]-1)*(nums[2]-1) = (4-1)*(5-1) = 3*4 = 12.
|
||||
|
||||
```
|
||||
|
||||
**Example 2**:
|
||||
|
||||
```
|
||||
Input: nums = [1,5,4,5]
|
||||
Output: 16
|
||||
Explanation: Choosing the indices i=1 and j=3 (indexed from 0), you will get the maximum value of (5-1)*(5-1) = 16.
|
||||
|
||||
```
|
||||
|
||||
**Example 3**:
|
||||
|
||||
```
|
||||
Input: nums = [3,7]
|
||||
Output: 12
|
||||
|
||||
```
|
||||
|
||||
**Constraints**:
|
||||
|
||||
- `2 <= nums.length <= 500`
|
||||
- `1 <= nums[i] <= 10^3`
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你一个整数数组 nums,请你选择数组的两个不同下标 i 和 j,使 (nums[i]-1)*(nums[j]-1) 取得最大值。请你计算并返回该式的最大值。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 简单题。循环一次,按照题意动态维护 2 个最大值,从而也使得 `(nums[i]-1)*(nums[j]-1)` 能取到最大值。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
func maxProduct(nums []int) int {
|
||||
max1, max2 := 0, 0
|
||||
for _, num := range nums {
|
||||
if num >= max1 {
|
||||
max2 = max1
|
||||
max1 = num
|
||||
} else if num <= max1 && num >= max2 {
|
||||
max2 = num
|
||||
}
|
||||
}
|
||||
return (max1 - 1) * (max2 - 1)
|
||||
}
|
||||
|
||||
```
|
||||
64
website/content/ChapterFour/1470.Shuffle-the-Array.md
Normal file
64
website/content/ChapterFour/1470.Shuffle-the-Array.md
Normal file
@@ -0,0 +1,64 @@
|
||||
# [1470. Shuffle the Array](https://leetcode.com/problems/shuffle-the-array/)
|
||||
|
||||
## 题目
|
||||
|
||||
Given the array `nums` consisting of `2n` elements in the form `[x1,x2,...,xn,y1,y2,...,yn]`.
|
||||
|
||||
*Return the array in the form* `[x1,y1,x2,y2,...,xn,yn]`.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: nums = [2,5,1,3,4,7], n = 3
|
||||
Output: [2,3,5,4,1,7]
|
||||
Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7].
|
||||
|
||||
```
|
||||
|
||||
**Example 2**:
|
||||
|
||||
```
|
||||
Input: nums = [1,2,3,4,4,3,2,1], n = 4
|
||||
Output: [1,4,2,3,3,2,4,1]
|
||||
|
||||
```
|
||||
|
||||
**Example 3**:
|
||||
|
||||
```
|
||||
Input: nums = [1,1,2,2], n = 2
|
||||
Output: [1,2,1,2]
|
||||
|
||||
```
|
||||
|
||||
**Constraints**:
|
||||
|
||||
- `1 <= n <= 500`
|
||||
- `nums.length == 2n`
|
||||
- `1 <= nums[i] <= 10^3`
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你一个数组 nums ,数组中有 2n 个元素,按 [x1,x2,...,xn,y1,y2,...,yn] 的格式排列。请你将数组按 [x1,y1,x2,y2,...,xn,yn] 格式重新排列,返回重排后的数组。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给定一个 2n 的数组,把后 n 个元素插空放到前 n 个元素里面。输出最终完成的数组。
|
||||
- 简单题,按照题意插空即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
func shuffle(nums []int, n int) []int {
|
||||
result := make([]int, 0)
|
||||
for i := 0; i < n; i++ {
|
||||
result = append(result, nums[i])
|
||||
result = append(result, nums[n+i])
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
```
|
||||
Reference in New Issue
Block a user