Skip to content

Commit 37fde65

Browse files
committed
Add solution 1662、1663、1664、1665
1 parent 285f7a1 commit 37fde65

17 files changed

+1204
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package leetcode
2+
3+
func arrayStringsAreEqual(word1 []string, word2 []string) bool {
4+
str1, str2 := "", ""
5+
for i := 0; i < len(word1); i++ {
6+
str1 += word1[i]
7+
}
8+
for i := 0; i < len(word2); i++ {
9+
str2 += word2[i]
10+
}
11+
return str1 == str2
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1662 struct {
9+
para1662
10+
ans1662
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1662 struct {
16+
word1 []string
17+
word2 []string
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans1662 struct {
23+
one bool
24+
}
25+
26+
func Test_Problem1662(t *testing.T) {
27+
28+
qs := []question1662{
29+
30+
{
31+
para1662{[]string{"ab", "c"}, []string{"a", "bc"}},
32+
ans1662{true},
33+
},
34+
35+
{
36+
para1662{[]string{"a", "cb"}, []string{"ab", "c"}},
37+
ans1662{false},
38+
},
39+
40+
{
41+
para1662{[]string{"abc", "d", "defg"}, []string{"abcddefg"}},
42+
ans1662{true},
43+
},
44+
}
45+
46+
fmt.Printf("------------------------Leetcode Problem 1662------------------------\n")
47+
48+
for _, q := range qs {
49+
_, p := q.ans1662, q.para1662
50+
fmt.Printf("【input】:%v 【output】:%v \n", p, arrayStringsAreEqual(p.word1, p.word2))
51+
}
52+
fmt.Printf("\n\n\n")
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# [1662. Check If Two String Arrays are Equivalent](https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/)
2+
3+
4+
## 题目
5+
6+
Given two string arrays `word1` and `word2`, return **`true` *if the two arrays **represent** the same string, and* `false` *otherwise.*
7+
8+
A string is **represented** by an array if the array elements concatenated **in order** forms the string.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: word1 = ["ab", "c"], word2 = ["a", "bc"]
14+
Output: true
15+
Explanation:
16+
word1 represents string "ab" + "c" -> "abc"
17+
word2 represents string "a" + "bc" -> "abc"
18+
The strings are the same, so return true.
19+
```
20+
21+
**Example 2:**
22+
23+
```
24+
Input: word1 = ["a", "cb"], word2 = ["ab", "c"]
25+
Output: false
26+
```
27+
28+
**Example 3:**
29+
30+
```
31+
Input: word1 = ["abc", "d", "defg"], word2 = ["abcddefg"]
32+
Output: true
33+
```
34+
35+
**Constraints:**
36+
37+
- `1 <= word1.length, word2.length <= 103`
38+
- `1 <= word1[i].length, word2[i].length <= 103`
39+
- `1 <= sum(word1[i].length), sum(word2[i].length) <= 103`
40+
- `word1[i]` and `word2[i]` consist of lowercase letters.
41+
42+
## 题目大意
43+
44+
给你两个字符串数组 word1 和 word2 。如果两个数组表示的字符串相同,返回 true ;否则,返回 false 。数组表示的字符串 是由数组中的所有元素 按顺序 连接形成的字符串。
45+
46+
## 解题思路
47+
48+
- 简单题,依次拼接 2 个数组内的字符串,然后比较 str1 和 str2 是否相同即可。
49+
50+
## 代码
51+
52+
```go
53+
package leetcode
54+
55+
func arrayStringsAreEqual(word1 []string, word2 []string) bool {
56+
str1, str2 := "", ""
57+
for i := 0; i < len(word1); i++ {
58+
str1 += word1[i]
59+
}
60+
for i := 0; i < len(word2); i++ {
61+
str2 += word2[i]
62+
}
63+
return str1 == str2
64+
}
65+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package leetcode
2+
3+
// 解法一 贪心
4+
func getSmallestString(n int, k int) string {
5+
res := make([]rune, n)
6+
for i := n - 1; i >= 0; i-- {
7+
diff := k - i
8+
if diff >= 26 {
9+
// Need to add z
10+
res[i] = 'z'
11+
k = k - 26
12+
} else {
13+
res[i] = rune('a' + diff - 1)
14+
k = k - diff
15+
}
16+
}
17+
return string(res)
18+
}
19+
20+
// 解法二 DFS
21+
func getSmallestString1(n int, k int) string {
22+
if n == 0 {
23+
return ""
24+
}
25+
res, c := "", []byte{}
26+
findSmallestString(0, n, k, 0, c, &res)
27+
return res
28+
}
29+
30+
func findSmallestString(value int, length, k, index int, str []byte, res *string) {
31+
if len(str) == length && value == k {
32+
tmp := string(str)
33+
if (*res) == "" {
34+
*res = tmp
35+
}
36+
if tmp < *res && *res != "" {
37+
*res = tmp
38+
}
39+
return
40+
}
41+
if len(str) >= index && (*res) != "" && str[index-1] > (*res)[index-1] {
42+
return
43+
}
44+
for j := 0; j < 26; j++ {
45+
if k-value > (length-len(str))*26 || value > k {
46+
return
47+
}
48+
str = append(str, byte(int('a')+j))
49+
value += j + 1
50+
findSmallestString(value, length, k, index+1, str, res)
51+
str = str[:len(str)-1]
52+
value -= j + 1
53+
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1663 struct {
9+
para1663
10+
ans1663
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1663 struct {
16+
n int
17+
k int
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans1663 struct {
23+
one string
24+
}
25+
26+
func Test_Problem1663(t *testing.T) {
27+
28+
qs := []question1663{
29+
30+
{
31+
para1663{3, 27},
32+
ans1663{"aay"},
33+
},
34+
35+
{
36+
para1663{5, 73},
37+
ans1663{"aaszz"},
38+
},
39+
40+
{
41+
para1663{24, 552},
42+
ans1663{"aaszz"},
43+
},
44+
}
45+
46+
fmt.Printf("------------------------Leetcode Problem 1663------------------------\n")
47+
48+
for _, q := range qs {
49+
_, p := q.ans1663, q.para1663
50+
fmt.Printf("【input】:%v 【output】:%v \n", p, getSmallestString(p.n, p.k))
51+
}
52+
fmt.Printf("\n\n\n")
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# [1663. Smallest String With A Given Numeric Value](https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/)
2+
3+
## 题目
4+
5+
The **numeric value** of a **lowercase character** is defined as its position `(1-indexed)` in the alphabet, so the numeric value of `a` is `1`, the numeric value of `b` is `2`, the numeric value of `c` is `3`, and so on.
6+
7+
The **numeric value** of a **string** consisting of lowercase characters is defined as the sum of its characters' numeric values. For example, the numeric value of the string `"abe"` is equal to `1 + 2 + 5 = 8`.
8+
9+
You are given two integers `n` and `k`. Return *the **lexicographically smallest string** with **length** equal to `n` and **numeric value** equal to `k`.*
10+
11+
Note that a string `x` is lexicographically smaller than string `y` if `x` comes before `y` in dictionary order, that is, either `x` is a prefix of `y`, or if `i` is the first position such that `x[i] != y[i]`, then `x[i]` comes before `y[i]` in alphabetic order.
12+
13+
**Example 1:**
14+
15+
```
16+
Input: n = 3, k = 27
17+
Output: "aay"
18+
Explanation: The numeric value of the string is 1 + 1 + 25 = 27, and it is the smallest string with such a value and length equal to 3.
19+
```
20+
21+
**Example 2:**
22+
23+
```
24+
Input: n = 5, k = 73
25+
Output: "aaszz"
26+
```
27+
28+
**Constraints:**
29+
30+
- `1 <= n <= 105`
31+
- `n <= k <= 26 * n`
32+
33+
## 题目大意
34+
35+
小写字符 的 数值 是它在字母表中的位置(从 1 开始),因此 a 的数值为 1 ,b 的数值为 2 ,c 的数值为 3 ,以此类推。字符串由若干小写字符组成,字符串的数值 为各字符的数值之和。例如,字符串 "abe" 的数值等于 1 + 2 + 5 = 8 。给你两个整数 n 和 k 。返回 长度 等于 n 且 数值 等于 k 的 字典序最小 的字符串。注意,如果字符串 x 在字典排序中位于 y 之前,就认为 x 字典序比 y 小,有以下两种情况:
36+
37+
- x 是 y 的一个前缀;
38+
- 如果 i 是 x[i] != y[i] 的第一个位置,且 x[i] 在字母表中的位置比 y[i] 靠前。
39+
40+
## 解题思路
41+
42+
- 给出 n 和 k,要求找到字符串长度为 n,字母在字母表内位置总和为 k 的最小字典序字符串。
43+
- 这一题笔者读完题,比赛的时候直接用 DFS 撸了一版。赛后看了时间复杂度马马虎虎,感觉还有优化的空间。DFS 会遍历出所有的解,实际上这一题只要求最小字典序,所以 DFS 剪枝的时候要加上判断字典序的判断,如果新添加进来的字母比已经保存的字符串的相应位置上的字母字典序大,那么就直接 return,这个答案一定不会是最小字典序。代码见解法二
44+
- 想到这里,其实 DFS 不必要,直接用 for 循环就可找到最小字典序的字符串。代码见解法一。
45+
46+
## 代码
47+
48+
```go
49+
package leetcode
50+
51+
// 解法一 贪心
52+
func getSmallestString(n int, k int) string {
53+
res := make([]rune, n)
54+
for i := n - 1; i >= 0; i-- {
55+
diff := k - i
56+
if diff >= 26 {
57+
// Need to add z
58+
res[i] = 'z'
59+
k = k - 26
60+
} else {
61+
res[i] = rune('a' + diff - 1)
62+
k = k - diff
63+
}
64+
}
65+
return string(res)
66+
}
67+
68+
// 解法二 DFS
69+
func getSmallestString1(n int, k int) string {
70+
if n == 0 {
71+
return ""
72+
}
73+
res, c := "", []byte{}
74+
findSmallestString(0, n, k, 0, c, &res)
75+
return res
76+
}
77+
78+
func findSmallestString(value int, length, k, index int, str []byte, res *string) {
79+
if len(str) == length && value == k {
80+
tmp := string(str)
81+
if (*res) == "" {
82+
*res = tmp
83+
}
84+
if tmp < *res && *res != "" {
85+
*res = tmp
86+
}
87+
return
88+
}
89+
if len(str) >= index && (*res) != "" && str[index-1] > (*res)[index-1] {
90+
return
91+
}
92+
for j := 0; j < 26; j++ {
93+
if k-value > (length-len(str))*26 || value > k {
94+
return
95+
}
96+
str = append(str, byte(int('a')+j))
97+
value += j + 1
98+
findSmallestString(value, length, k, index+1, str, res)
99+
str = str[:len(str)-1]
100+
value -= j + 1
101+
102+
}
103+
}
104+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package leetcode
2+
3+
// 解法一 超简洁写法
4+
func waysToMakeFair(nums []int) int {
5+
sum, res := [2]int{}, 0
6+
for i := 0; i < len(nums); i++ {
7+
sum[i%2] += nums[i]
8+
}
9+
for i := 0; i < len(nums); i++ {
10+
sum[i%2] -= nums[i]
11+
if sum[i%2] == sum[1-(i%2)] {
12+
res++
13+
}
14+
sum[1-(i%2)] += nums[i]
15+
}
16+
return res
17+
}
18+
19+
// 解法二 前缀和,后缀和
20+
func waysToMakeFair1(nums []int) int {
21+
evenPrefix, oddPrefix, evenSuffix, oddSuffix, res := 0, 0, 0, 0, 0
22+
for i := 0; i < len(nums); i++ {
23+
if i%2 == 0 {
24+
evenSuffix += nums[i]
25+
} else {
26+
oddSuffix += nums[i]
27+
}
28+
}
29+
for i := 0; i < len(nums); i++ {
30+
if i%2 == 0 {
31+
evenSuffix -= nums[i]
32+
} else {
33+
oddSuffix -= nums[i]
34+
}
35+
if (evenPrefix + oddSuffix) == (oddPrefix + evenSuffix) {
36+
res++
37+
}
38+
if i%2 == 0 {
39+
evenPrefix += nums[i]
40+
} else {
41+
oddPrefix += nums[i]
42+
}
43+
}
44+
return res
45+
}

0 commit comments

Comments
 (0)