go

/**
 * 
 * @param num int整型一维数组 
 * @return int整型二维数组
*/
func permuteUnique( num []int ) [][]int {
    // write code here
    if len(num) == 0 {
        return nil
    }

    sort.Ints(num)

    ret := make([][]int, 0)
    m := make([]bool, len(num))
    path := make([]int, 0)

    var do func(path []int)
    do = func(path []int) {
        if len(path) == len(num) {
            tmp := make([]int, len(num))
            copy(tmp, path)
            ret = append(ret, tmp)
            return
        }

        for i:=0; i < len(num); i++{
//          关键是这里
//           m[i] 为true 说明该值已经使用过了
//          i>0 && num[i] == num[i-1] && !m[i-1]
//             说明 i-1 值与当前相等, 并且已经在其他路径使用过了
            if m[i] || i>0 && num[i] == num[i-1] && !m[i-1] {
                continue
            }

            m[i] = true
            path = append(path, num[i])

            do(path)

            m[i] = false
            path = path[:len(path)-1]
        }
    }

    do(path)

    return ret
}