diff --git a/leetcode/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence/1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence.go b/leetcode/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence/1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence.go new file mode 100644 index 00000000..c5f13249 --- /dev/null +++ b/leetcode/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence/1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence.go @@ -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 +} diff --git a/leetcode/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence/1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence_test.go b/leetcode/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence/1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence_test.go new file mode 100644 index 00000000..22607f7c --- /dev/null +++ b/leetcode/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence/1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence_test.go @@ -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") +} diff --git a/leetcode/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence/README.md b/leetcode/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence/README.md new file mode 100644 index 00000000..c8081e82 --- /dev/null +++ b/leetcode/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence/README.md @@ -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 +} + +``` \ No newline at end of file diff --git a/leetcode/1464.Maximum-Product-of-Two-Elements-in-an-Array/1464. Maximum Product of Two Elements in an Array.go b/leetcode/1464.Maximum-Product-of-Two-Elements-in-an-Array/1464. Maximum Product of Two Elements in an Array.go new file mode 100644 index 00000000..a88bccb1 --- /dev/null +++ b/leetcode/1464.Maximum-Product-of-Two-Elements-in-an-Array/1464. Maximum Product of Two Elements in an Array.go @@ -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) +} diff --git a/leetcode/1464.Maximum-Product-of-Two-Elements-in-an-Array/1464. Maximum Product of Two Elements in an Array_test.go b/leetcode/1464.Maximum-Product-of-Two-Elements-in-an-Array/1464. Maximum Product of Two Elements in an Array_test.go new file mode 100644 index 00000000..06c9821f --- /dev/null +++ b/leetcode/1464.Maximum-Product-of-Two-Elements-in-an-Array/1464. Maximum Product of Two Elements in an Array_test.go @@ -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") +} diff --git a/leetcode/1464.Maximum-Product-of-Two-Elements-in-an-Array/README.md b/leetcode/1464.Maximum-Product-of-Two-Elements-in-an-Array/README.md new file mode 100644 index 00000000..04dbf134 --- /dev/null +++ b/leetcode/1464.Maximum-Product-of-Two-Elements-in-an-Array/README.md @@ -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) +} + +``` \ No newline at end of file diff --git a/leetcode/1470.Shuffle-the-Array/1470. Shuffle the Array.go b/leetcode/1470.Shuffle-the-Array/1470. Shuffle the Array.go new file mode 100644 index 00000000..fef82041 --- /dev/null +++ b/leetcode/1470.Shuffle-the-Array/1470. Shuffle the Array.go @@ -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 +} diff --git a/leetcode/1470.Shuffle-the-Array/1470. Shuffle the Array_test.go b/leetcode/1470.Shuffle-the-Array/1470. Shuffle the Array_test.go new file mode 100644 index 00000000..212ecef9 --- /dev/null +++ b/leetcode/1470.Shuffle-the-Array/1470. Shuffle the Array_test.go @@ -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") +} diff --git a/leetcode/1470.Shuffle-the-Array/README.md b/leetcode/1470.Shuffle-the-Array/README.md new file mode 100644 index 00000000..961d200c --- /dev/null +++ b/leetcode/1470.Shuffle-the-Array/README.md @@ -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 +} + +``` \ No newline at end of file diff --git a/website/content/ChapterFour/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence.md b/website/content/ChapterFour/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence.md new file mode 100644 index 00000000..c8081e82 --- /dev/null +++ b/website/content/ChapterFour/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence.md @@ -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 +} + +``` \ No newline at end of file diff --git a/website/content/ChapterFour/1464.Maximum-Product-of-Two-Elements-in-an-Array.md b/website/content/ChapterFour/1464.Maximum-Product-of-Two-Elements-in-an-Array.md new file mode 100644 index 00000000..04dbf134 --- /dev/null +++ b/website/content/ChapterFour/1464.Maximum-Product-of-Two-Elements-in-an-Array.md @@ -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) +} + +``` \ No newline at end of file diff --git a/website/content/ChapterFour/1470.Shuffle-the-Array.md b/website/content/ChapterFour/1470.Shuffle-the-Array.md new file mode 100644 index 00000000..961d200c --- /dev/null +++ b/website/content/ChapterFour/1470.Shuffle-the-Array.md @@ -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 +} + +``` \ No newline at end of file