如何在不使用for语句的情况下,在Golang中反转数组或切片。

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

how to reverse array | slice in golang without using for statement

问题

我正在寻找一种在Golang中反转[][]string|int的方法,而不使用任何for语句。
我找不到任何解决方案。我找到了这篇帖子How do I reverse a slice in go?,但每个解决方案都使用了for语句。我知道处理这个问题的最佳方法是使用for,但我正在寻找其他不使用for的解决方案。

list := [][]int{
    {2,3,4}, 
    {6,8,1},
}

结果应该是这样的:[[2 3 4] [6 8 1]].

但我需要像这样的结果:[[6 8 1] [2 3 4]].

在不使用for语句的情况下,我该怎么做?有什么想法吗?

英文:

I'm looking for a way to reverse [][]string|int in Golang without using any for statement.
I couldn't find any solution. I found this post How do I reverse a slice in go?, but every solution is used for statement. I know the best way to handle this problem is using for, but I'm looking for other solutions without having for

 list := [][]int{
    		{2,3,4}, 
            {6,8,1},
    	}

the result is like this: [[2 3 4] [6 8 1]].

but I need something like this: [[6 8 1] [2 3 4]].

What should I do without using the for statement? Is there any idea?

答案1

得分: 1

在这里使用for循环是最简单的解决方案。

PS:你可以尝试使用硬编码的解决方案,像这样:

list2 := [][]int{list[1], list[0]}

但这更像是一个hack而不是解决方案,
所以最好使用基于for循环的简洁而优雅的编程解决方案。

英文:

Using for loop here - is the simplest solution.

PS: You can try to use hardcoded solution like this one:

list2 := [][]int{list[1], list[0]}

but it's rather hack than solution,
<br>so better to use nice and concise programmatic solution base on for loop.

答案2

得分: 1

不使用for语句,你可以考虑使用递归调用或者goto语句来实现相同的功能。

递归调用的示例代码如下:

package main

import (
	"fmt"
)

func main() {
	list := [][]int{
		{2, 3, 4},
		{6, 8, 1},
	}
	reverse(list)
	fmt.Println(list)
}

func reverse(d [][]int) {
	j := len(d) - 1
	i := 0
	var h func()
	h = func() {
		if i < j {
			d[i], d[j] = d[j], d[i]
			i, j = i+1, j-1
			h()
		}
	}
	h()
}

使用goto语句的示例代码如下:

package main

import (
	"fmt"
)

func main() {
	list := [][]int{
		{2, 3, 4},
		{6, 8, 1},
	}
	reverse(list)
	fmt.Println(list)
}

func reverse(d [][]int) {
	j := len(d) - 1
	i := 0
START:
	if i < j {
		d[i], d[j] = d[j], d[i]
		i, j = i+1, j-1
		goto START
	}

}

如果你已经阅读到这里,可以参考这个常规解决方案:https://stackoverflow.com/a/28058324/4466350

英文:

> What should I do without using the for statement? Is there any idea?

recursive calls

package main

import (
	&quot;fmt&quot;
)

func main() {
	list := [][]int{
		{2, 3, 4},
		{6, 8, 1},
	}
	reverse(list)
	fmt.Println(list)
}

func reverse(d [][]int) {
	j := len(d) - 1
	i := 0
	var h func()
	h = func() {
		if i&lt;j {
			d[i], d[j] = d[j], d[i]
			i, j = i+1, j-1
			h()
		}
	}
	h()
}

goto statements

package main

import (
	&quot;fmt&quot;
)

func main() {
	list := [][]int{
		{2, 3, 4},
		{6, 8, 1},
	}
	reverse(list)
	fmt.Println(list)
}

func reverse(d [][]int) {
	j := len(d) - 1
	i := 0
START:
	if i &lt; j {
		d[i], d[j] = d[j], d[i]
		i, j = i+1, j-1
		goto START
	}

}

if you read that far, read about a regular solution https://stackoverflow.com/a/28058324/4466350

huangapple
  • 本文由 发表于 2021年7月1日 04:06:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/68201017.html
匿名

发表评论

匿名网友

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

确定