为什么我的Go解决方案给出的结果与C ++不同?

时间:2019-06-18 12:26:07

标签: c++ go

两个逻辑完全相同的代码给出了不同的解决方案。

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代码:

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调试了几个小时,但我找不到真相。

1 个答案:

答案 0 :(得分:4)

尝试一下:

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的方法,效果很好。