让我们说我有一个变量num.
在我评估if语句之前,我需要先检查是否已设置此变量.通常我会用两种-1,0或math.MaxInt32/math.MinInt32但对于这个任务,我需要检查,如果我的计划已经将其值设置.
我试过nums := nil,在我的if陈述中,我有类似的东西if nums == nil || ....
但是在创建nums变量时,我很惊讶地得到了错误cannot convert nil to type int.我怎么能实现我想做的事情?
为了说清楚:这是一个leetcode 问题而且没有输入的控制权,他们把各种数字放在里面包括math.MaxInt32,math.MinInt32
完整代码:
func thirdMax(nums []int) int {
dict := make(map[int]int, 0)
varmaxOne := math.MinInt32
maxTwo := math.MinInt32
maxThree := math.MinInt32
for _, value := range nums {
if value >= maxOne && dict[value] < 1 {
maxThree = maxTwo
maxTwo = maxOne
maxOne = value
}else if value >= maxTwo && dict[value] < 1{
maxThree = maxTwo
maxTwo = value
} else if value >= maxThree && dict[value] < 1 {
maxThree = value
}
dict[value] += 1
}
if maxThree == math.MinInt32 {
return maxTwo
}
return maxThree
}
func max(a int, b int) int {
if a > b {
return a
}
return b
}
Run Code Online (Sandbox Code Playgroud)
测试
func TestThirdMax(t *testing.T) {
//array := []int{2, 3, -1, -9, 11, 4, 3, 0, -100}
//array := []int{2, 2, 3, 1}
array := []int{1,2,-2147483648}
//array := []int{3,2,1}
result := thirdMax(array)
if result != 2 {
t.Errorf("Expected 2, but it was %d instead.", array)
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码使用math.MinInt32,但在给定输入时失败.我的想法是nil反过来并对if声明进行检查,但如上所述失败.
正如评论所示,Go中所有int类型的零值是0.如果0是数字的可能值,并且您需要检查它是否已分配,那么您可能希望使用附加了某些元数据的Struct类型,而不是基本类型.
像往常一样,这些问题在标准库中有先例.
看一下database/sql包的Null类型. 这些类型也可以通过实现来证明Scanner,但是您可以在需要检查原始值是否应该真正等于Null而不是它的零值时使用相同的想法.
如评论中所述,您无法检查是否int已设置.
并使用math.MinInt32作为信号值如果你在输入会失败.
因此,如果您想以这种方式实现它,唯一的选择是使用指针(*int).
使用指针,您可以检查它们是否已设置,因为它们nil以您期望的值开头.
但是,当您需要使用指针和引用时,逻辑会变得更加复杂.
请参阅下面的可能实现,尝试使用您发布的相同逻辑,仅更改为使用指针.
另一种选择可能是对数组进行排序并使用已排序的元素来查找第三个元素.
游乐场链接:https://play.golang.org/p/ro_NIrDEo_s
func thirdMax(nums []int) int {
dict := make(map[int]int, 0)
// these will start with "nil values"
var maxOne *int
var maxTwo *int
var maxThree *int
for _, value := range nums {
if maxOne == nil || value >= *maxOne && dict[value] < 1 {
maxThree = maxTwo
maxTwo = maxOne
maxOne = new(int)
*maxOne = value
}else if maxTwo == nil || value >= *maxTwo && dict[value] < 1{
maxThree = maxTwo
maxTwo = new(int)
*maxTwo = value
} else if maxThree == nil || value >= *maxThree && dict[value] < 1 {
maxThree = new(int)
*maxThree = value
}
dict[value] += 1
}
if maxOne == nil {
panic("does not work with empty input array!")
}
if maxTwo == nil {
return *maxOne
}
if maxThree == nil {
return *maxTwo
}
return *maxThree
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2272 次 |
| 最近记录: |