两个已排序数组的第k个最大值

huangapple go评论75阅读模式
英文:

k'th highest of two sorted arrays

问题

我是你的中文翻译助手,以下是你要翻译的内容:

我刚开始学习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
}
英文:

I'm new to Go and I'm running into what I'm sure are beginner errors. I'm getting an error invalid identifier character U+2013 seemingly everywhere I'm subtracting. Am I some how not using the arithmetic operator properly? I'm also getting the errors unexpected name, expecting semicolon or newline or } and non-declaration statement outside function body but I'm not too sure what's causing it. It mainly seems like the issue is within my if statements above the for loop. I thought it was because I'm reassigning already declared variables, but that doesn't seem to be the case. Any help would be much appreciated and if anyone has some tips in general for debugging Go that would be great too.

package main
import &quot;fmt&quot;
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(&quot;%v&quot;, result)
}
func kthSmallest(a []int, b []int, k int) int {
if k &gt;= len(a) + len(b) {
fmt.Printf(&quot;The range is not large enough&quot;)
return 0
}
var lowA, lowB, highA, highB int = 0, 0, k - 1, k - 1
if len(a) &lt; k - 1 {
highA = len(a) - 1;
}
if len(b) &lt; k - 1 {
highB = len(b) - 1;
}
if highA + highB &lt; k {
return 0;
}
var midA, midB int = 0, 0
var result int = 0
for k &gt; 0 {
midA = lowA + (highA - lowA)/2;
midB = lowB + (highB - lowB)/2;
if a[midA] &gt;= b[midB] {
k = k - (midB - lowB + 1);
result = b[midB];
highA = midA - 1;
lowB = midB + 1;
} else if a[midA] &lt; b[midB] {
k = k - (midA - lowA + 1);
result = a[midA];
highB = midB - 1;
lowA = midA + 1;
}
}
return result
}

答案1

得分: 3

U+2013(或'–')是一个对于喜欢排版的人来说比较花哨的破折号版本,如果你在键盘上输入一个破折号,你会得到正确的字符,但如果你从网站、文字处理软件或电子邮件中复制粘贴的话,它可能会被错误的破折号“帮助性地”替换掉,而Go语言是无法理解这种替换的。双引号字符也经常出现这种情况。

你可以在文本编辑器或集成开发环境中进行搜索和替换,将花哨的破折号替换回来。

英文:

U+2013 (or '–') is a fancy version of '-' (or U+2d) for people who are big into typography. If you type a dash on your keyboard, you'll get the right character, if you copied something in from a web site, a word processor, or an email, it might have been 'helpfully' replaced along with way with the wrong dash, which Go doesn't understand. This also happens a lot with the double-quote character.

You should be able to search and replace the fancy dash in your text editor or IDE.

huangapple
  • 本文由 发表于 2017年9月10日 11:39:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/46137083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定