我是Go的新手,我遇到了我确定的初学者错误。我收到的错误invalid identifier character U+2013
似乎在我减去的每个地方。我有些人如何不正确使用算术运算符?我也收到错误unexpected name, expecting semicolon or newline or }
和non-declaration statement outside function body
,但我不太确定导致错误的原因。它似乎主要是问题在for循环之上的if语句中。我以为是因为我重新分配已经声明的变量,但事实并非如此。任何帮助都将非常感激,如果有人有一些调试Go的一般提示,那也很棒。
package main
import "fmt"
func main() {
a := []int{
13, 14, 15, 16, 17,
}
b := []int{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
}
k := 8
result := kthSmallest(a, b, k)
fmt.Printf("%v", result)
}
func kthSmallest(a []int, b []int, k int) int {
if k >= len(a) + len(b) {
fmt.Printf("The range is not large enough")
return 0
}
var lowA, lowB, highA, highB int = 0, 0, k - 1, k - 1
if len(a) < k - 1 {
highA = len(a) - 1;
}
if len(b) < k - 1 {
highB = len(b) - 1;
}
if highA + highB < k {
return 0;
}
var midA, midB int = 0, 0
var result int = 0
for k > 0 {
midA = lowA + (highA - lowA)/2;
midB = lowB + (highB - lowB)/2;
if a[midA] >= b[midB] {
k = k - (midB - lowB + 1);
result = b[midB];
highA = midA - 1;
lowB = midB + 1;
} else if a[midA] < b[midB] {
k = k - (midA - lowA + 1);
result = a[midA];
highB = midB - 1;
lowA = midA + 1;
}
}
return result
}