猿问

查找列表中的最小数字

我用 Go 编写了一个程序,它可以找到列表中的最小数字并且它可以工作。但是,我真的不明白其中的逻辑。你能解释一下它是如何工作的吗?


package main


import "fmt"


func main() {

    x := []int{

        48, 96, 86, 68,

        57, 82, 63, 70,

        37, 34, 83, 27,

        19, 97, 9, 17,

    }


    for i, num := range x {

        if num < i {

            fmt.Println(num)

        }

    }

}

游乐场:https://play.golang.org/p/Awuw2Th1g2V


输出:


9

我教科书中的解决方案不同,我理解那里的逻辑。


慕神8447489
浏览 126回答 4
4回答

尚方宝剑之说

要找到列表中的最小数字,您需要遍历列表并存储您当前找到的最小数字。将这个“迄今为止最小”的数字与列表中的其他数字进行比较,如果您发现一个较小的数字,请用它替换您的最小数字。在迭代结束时,您将知道列表中的最小数字。smallest := x[0]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // set the smallest number to the first element of the listfor _, num := range x[1:] { // iterate over the rest of the list&nbsp; &nbsp; if num < smallest {&nbsp; &nbsp; &nbsp;// if num is smaller than the current smallest number&nbsp; &nbsp; &nbsp; &nbsp; smallest = num&nbsp; &nbsp; &nbsp; // set smallest to num&nbsp; &nbsp; }}fmt.Println(smallest)

阿晨1998

您提供的示例程序纯属巧合。如果正确的值 9 是切片中的第一个,则根本不会有输出。有多种方法可以达到识别最小int的目的(还有更多的方法):func smallestOfCopyWithSort(in []int) int {&nbsp; // Make a copy, so we do not have to modify the original slice.&nbsp; // Note: Do NOT use this approach, it is here only for completeness.&nbsp; copy := append([]int(nil), in...)&nbsp; sort.Ints(copy)&nbsp; return (copy[0])}func smallestWithSort(in []int) int {&nbsp; // Sort the slice.&nbsp; // Note that it will be modified and you&nbsp; // need to make sure that it will always&nbsp; // be sorted, even when you add new values.&nbsp; sort.Ints(in)&nbsp; return (in[0])}func smallestWithMattsApproach(in []int) int {&nbsp; smallest := in[0]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // set the smallest number to the first element of the list&nbsp; for _, num := range in[1:] { // iterate over the rest of the list&nbsp; &nbsp; if num < smallest { // if num is smaller than the current smallest number&nbsp; &nbsp; &nbsp; smallest = num // set smallest to num&nbsp; &nbsp; }&nbsp; }&nbsp; return smallest}@Matt 的方法可能是最好的方法,因为它非常快,无需修改原始切片。这实际上取决于您想要实现的目标。这里有一些基准$ go test -test.benchmem -bench=. -test.cpu 1,2,4 -test.benchtime=10sgoos: darwingoarch: amd64pkg: <redacted>BenchmarkSortWithCopy&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 5000000&nbsp; &nbsp; &nbsp; &nbsp;345 ns/op&nbsp; &nbsp; &nbsp; &nbsp;160 B/op&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2 allocs/opBenchmarkSortWithCopy-2&nbsp; &nbsp; &nbsp; &nbsp; 5000000&nbsp; &nbsp; &nbsp; &nbsp;354 ns/op&nbsp; &nbsp; &nbsp; &nbsp;160 B/op&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2 allocs/opBenchmarkSortWithCopy-4&nbsp; &nbsp; &nbsp; &nbsp; 5000000&nbsp; &nbsp; &nbsp; &nbsp;352 ns/op&nbsp; &nbsp; &nbsp; &nbsp;160 B/op&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2 allocs/opBenchmarkMattsApproach&nbsp; &nbsp; &nbsp; &nbsp;100000000&nbsp; &nbsp; &nbsp; 15.1 ns/op&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 B/op&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 allocs/opBenchmarkMattsApproach-2&nbsp; &nbsp; &nbsp;100000000&nbsp; &nbsp; &nbsp; 15.1 ns/op&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 B/op&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 allocs/opBenchmarkMattsApproach-4&nbsp; &nbsp; &nbsp;100000000&nbsp; &nbsp; &nbsp; 15.2 ns/op&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 B/op&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 allocs/opBenchmarkSort&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2000000000&nbsp; &nbsp; &nbsp; 0.00 ns/op&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 B/op&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 allocs/opBenchmarkSort-2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2000000000&nbsp; &nbsp; &nbsp; 0.00 ns/op&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 B/op&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 allocs/opBenchmarkSort-4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2000000000&nbsp; &nbsp; &nbsp; 0.00 ns/op&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 B/op&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 allocs/op毫不奇怪,smallestOfCopyWithSort如果多次调用,它比其他方法慢几个数量级。Matts 的方法非常快,不会复制或修改任何内容。但是,如果您需要多次访问最小数量的切片,则对切片进行排序(升序)并简单地访问第一个成员会更高效。这样做的原因是切片将被修改为排序顺序。但是,这种方法有一个警告:您要么在向切片添加值时非常小心,要么在每次修改它时都使用它,这可能会抵消性能优势,具体取决于您的读取和写入比率/从切片。就个人而言,我发现smallestWithSort我最常使用的解决方案,因为我正在使用的切片通常不会改变。结论如果您只需要访问最小的数字一次或者切片值的顺序很重要,请使用 Matt 的方法。如果顺序无关紧要并且您需要多次访问最小的数字,您可能应该使用smallestWithSort,同时牢记约束条件。

qq_笑_17

在for i, num := range x {&nbsp; &nbsp; &nbsp; &nbsp; if num < i {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(num)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }这里,i代表索引,num代表价值。因此,您的if条件表示值小于索引然后打印该值。因为,9 值是 9,索引是 14。所以它打印 9,这不是你想要的。

富国沪深

返回python中列表的最小数量def find_smallest_number(input_list):&nbsp; &nbsp; d=[]&nbsp; &nbsp; for num in input_list:&nbsp; &nbsp; &nbsp; &nbsp; for i in numbers:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if num<i:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if d==[]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d.append(num)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for j in d:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if j>num:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d.remove(j)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d.append(num)&nbsp; &nbsp; return d
随时随地看视频慕课网APP

相关分类

Go
我要回答