为什么我的 Go 解决方案给出的结果与C++不同
Why does my Go solution give a different result from C++?
两个具有完全相同逻辑的代码给出了不同的解决方案。
Go 中是否存在错误,还是我在代码中犯了一些愚蠢的错误?
这是一个leetcode问题LeetCode - Permutations II。
下面的C++代码是由某人编写的已接受解决方案。
C++代码
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<vector<int>> permuteUnique(vector<int> &num) {
sort(num.begin(), num.end());
vector<vector<int>>res;
helper(num, 0, num.size(), res);
return res;
}
void helper(vector<int> num, int start, int j, vector<vector<int> > &res) {
if (start == j-1) {
res.push_back(num);
return;
}
for (int i = start; i < j; i++) {
if (start == i || num[start] != num[i]) {
swap(num[start], num[i]);
helper(num, start + 1, j, res);
}
}
}
};
int main() {
Solution s;
vector<int> nums({2,1,2});
vector<vector<int>> res = s.permuteUnique(nums);
for (int i = 0; i < res.size(); i++) {
for (int j = 0; j < res[i].size(); j++) {
cout << " " << res[i][j];
}
cout << endl;
}
}
我将上面的C++代码翻译成下面的golang代码:
戈朗代码
package main
func permuteUnique(nums []int) [][]int {
qsort(nums, 0, len(nums)-1)
res := make([][]int, 0, len(nums))
helper(&res, nums, 0)
return res
}
func helper(res *[][]int, nums []int, start int) {
if start == len(nums)-1 {
copied := make([]int, len(nums))
copy(copied, nums)
*res = append(*res, copied)
return
}
for i := start; i < len(nums); i++ {
if start == i || nums[start] != nums[i] {
nums[i], nums[start] = nums[start], nums[i]
helper(res, nums, start+1)
}
}
}
func main() {
nums := []int{2,1,2}
res := permuteUnique(nums)
for i := 0; i < len(res); i++ {
for j := 0; j < len(res[0]); j++ {
print(" ", res[i][j]);
}
println()
}
}
func qsort(nums []int, low, high int) {
if low >= high {
return
}
i, j, pivot := low, high, nums[low]
for i < j {
for i < j && nums[j] >= pivot {
j--
}
nums[i] = nums[j]
for i < j && nums[i] <= pivot {
i++
}
nums[j] = nums[i]
}
nums[i] = pivot
qsort(nums, low, i-1)
qsort(nums, i+1, high)
}
输出
上面的两个代码都能够立即运行。以下是我的输出:
fondoger@localhost:Desktop$ g++ test.cpp -o app && ./app
1 2 2
2 1 2
2 2 1
fondoger@localhost:Desktop$ go run test.go
1 2 2
2 1 2
2 2 1
1 2 2
我尝试使用Goland IDE和Clion IDE进行了几个小时的调试,但我无法找出真相。
试试这个:
func helper(res *[][]int, nums []int, start int) {
copied := make([]int, len(nums))
copy(copied, nums)
nums = copied;
if start == len(nums)-1 {
*res = append(*res, nums)
return
}
for i := start; i < len(nums); i++ {
if start == i || nums[start] != nums[i] {
nums[i], nums[start] = nums[start], nums[i]
helper(res, nums, start+1)
}
}
}
从语言移植到语言时,您需要确保"移植"隐式功能。C++解决方案中,调用helper
函数语言将为每次迭代创建nums
数组的副本。您没有克隆此行为,并得到了不同的结果。我在功能开始时添加了应对num
helper
并且效果很好。
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- valgrind-hellgrind与泄漏检查的结果不同
- 用C++20 fmt限制结果的总大小
- 如何返回一个类的两个对象相加的结果
- 使用QProcess执行命令,并将结果存储在QStringList中
- 如果我std::dynamic_pointer_cast并且底层dynamic_cast的结果为null,那么返回的sh
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 序列化,没有库的整数,得到奇怪的结果
- 使用取消引用的指针的多态性会产生意外的结果.为什么?
- 在更改for循环的第三部分后,未使用for循环结果
- 使用++运算符会导致意外的结果
- 为什么在逗号分隔符上下文中将预增量的结果强制转换为void
- C++Brute Force攻击函数不会返回结果
- 你好。。。id_public变量不应该给出结果为 81 和 86 吗?为什么它为两个派生类占用不同的内存位置?
- 算术运算的结果类似于:C浮点变量中的1/3
- ";结果类型必须是可从输入范围的值类型""构造的;创建std::vector时
- 密码登录程序将永远循环并显示不正确的结果
- 如何让C++'tally up'结果并制定计划?
- 为什么这个程序的结果是3 "born"?和 4 死
- 尝试将字符串/字符转换为整数会产生意外结果