英文:
How to add a special condition for the last element of an array
问题
根据任务描述,输出应该是一个新的数组,其中包含数组"days"中所有大于它们前一个和后一个元素(递增1)的元素。例如,给定输入为:
[]int{3, 2, 4, 3, 7, 9}
输出应该是:
[]int{3, 4, 9}
起初,我尝试编写以下条件:
if days[i] > days[i-1] && days[i] > days[i+1] {
arr = append(arr, days[i])
}
但是出现了错误:"index out of range [-1]"
然后,我修改了条件,并为最后一个元素添加了特殊条件:
package main
import (
"fmt"
)
func chaos(days []int) []int {
var arr []int
for i := 0; i < len(days)-1; i++ {
if days[i] > days[i+1] {
arr = append(arr, days[i])
}
if days[len(days)-1] > days[len(days)-2] {
arr = append(arr, days[len(days)-1])
}
}
return arr
}
func main() {
fmt.Println(chaos([]int{3, 2, 4, 3, 7, 9}))
}
但是输出结果是"[3 9 9 4 9 9 9]"
如何正确指定最后一个元素的条件,并且为什么我的结果是错误的?
英文:
According to the task description, the output should be a new array with all elements of array days that are larger than their previous and next elements (in increments of 1). For example, we have as input
[ ]int{3, 2, 4, 3, 7, 9}
and the output should be:
[ ]int{3, 4, 9}
At first I tried to write the next condition:
if days[i] > days[i-1] && days[i] > days[i+1] {
arr = append(arr, days[i])
}
but an error occured: index out of range [-1]
Then I changed the condition and added a special condition for the last element:
package main
import (
"fmt"
)
func chaos(days []int) []int {
var arr []int
for i := 0; i < len(days)-1; i++ {
if days[i] > days[i+1] {
arr = append(arr, days[i])
}
if days[len(days)-1] > days[len(days)-2] {
arr = append(arr, days[len(days)-1])
}
}
return arr
}
func main() {
fmt.Println(chaos([]int{3, 2, 4, 3, 7, 9}))
}
But the output is [3 9 9 4 9 9 9]
How to correctly specify the condition for the last element and why the result in my case is incorrect?
答案1
得分: 1
你可以在切片的开头和结尾添加math.MinInt64
(可以视为负无穷大),以便轻松处理两个边界情况。然后从索引1
迭代到索引n-2
(排除我们添加的0
和n-1
条目)。
我会尝试以下方式。
package main
import (
"fmt"
"math"
)
func chaos(days []int) []int {
var arr []int
// 在开头添加负无穷大
days = append([]int{math.MinInt64}, days...)
// 在结尾添加负无穷大
days = append(days, math.MinInt64)
for i := 1; i <= len(days)-2; i++ {
if (days[i] > days[i+1]) && (days[i] > days[i-1]) {
arr = append(arr, days[i])
}
}
return arr
}
func main() {
fmt.Println(chaos([]int{3, 2, 4, 3, 7, 9}))
}
英文:
You can add math.MinInt64
(can be considered as negative infinity) to the start and end for the slice to handle the two edge cases easily. Then iterate starting from index 1
to index n-2
(by excluding 0
and n-1
which are the entries we added).
I would try like below.
package main
import (
"fmt"
"math"
)
func chaos(days []int) []int {
var arr []int
// Add negative infinity to the start
days = append([]int{math.MinInt64}, days...)
// Add negative infinity to the end
days = append(days, math.MinInt64)
for i := 1; i <= len(days)-2; i++ {
if (days[i] > days[i+1]) && (days[i] > days[i-1]) {
arr = append(arr, days[i])
}
}
return arr
}
func main() {
fmt.Println(chaos([]int{3, 2, 4, 3, 7, 9}))
}
答案2
得分: 1
9
被重复填充的原因是因为你在for
循环内部使用了下面的if
条件。将这个if
放在for
循环之外,然后你将得到期望的输出:
if days[len(days)-1] > days[len(days)-2] {
arr = append(arr, days[len(days)-1])
}
然而,除此之外,你还只是将第i
个元素与下一个元素进行比较。根据问题的要求,你还应该将其与前一个元素进行比较。以下是可能对你的代码进行的更改,以使其正常工作:
package main
import (
"fmt"
)
func chaos(days []int) []int {
var arr []int
fmt.Println(len(days))
if days[0] > days[1] {
arr = append(arr, days[0])
}
for i := 1; i < len(days)-1; i++ {
if (days[i] > days[i+1]) && (days[i] > days[i-1]) {
arr = append(arr, days[i])
}
}
if days[len(days)-1] > days[len(days)-2] {
arr = append(arr, days[len(days)-1])
}
return arr
}
func main() {
fmt.Println(chaos([]int{3, 2, 4, 3, 7, 9}))
}
这里是一个示例链接。
英文:
The reason 9
is populated multiple times is because you're using below if
condition inside for
loop. Put this if
outside of your for
loop, then you will get the desired output:
if days[len(days)-1] > days[len(days)-2] {
arr = append(arr, days[len(days)-1])
}
However, apart from this also you're comparing i'th
element with the next element only. As per the question, you should also compare it with previous element as well. Below are the possible changes which could be made in your code to work properly:
package main
import (
"fmt"
)
func chaos(days []int) []int {
var arr []int
fmt.Println(len(days))
if days[0] > days[1] {
arr = append(arr, days[0])
}
for i := 1; i < len(days)-1; i++ {
if (days[i] > days[i+1]) && (days[i] > days[i-1]) {
arr = append(arr, days[i])
}
}
if days[len(days)-1] > days[len(days)-2] {
arr = append(arr, days[len(days)-1])
}
return arr
}
func main() {
fmt.Println(chaos([]int{3, 2, 4, 3, 7, 9}))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论