创建所有可能的数组排序案例

时间:2016-04-17 04:36:09

标签: ios swift

我有这个需要逻辑,但我似乎完全失去了这个,我需要从给定的单个数组创建一个所有可能组合的数组

伪:

input: let array = [1,2,3,4,5]

output:
[1,2,3,4,5],
[2,1,3,4,5],
[3,1,2,4,5],..

非常感谢,如果有人可以建议这样做的逻辑

2 个答案:

答案 0 :(得分:1)

见vacawama的this answer是如何做出的,以及Martin R的答案,看看性能比较。答案代码:

func permute(list: [String], minStringLen: Int = 2) -> Set<String> {
    func permute(fromList: [String], toList: [String], minStringLen: Int, inout set: Set<String>) {
        if toList.count >= minStringLen {
            set.insert(toList.joinWithSeparator(""))
        }
        if !fromList.isEmpty {
            for (index, item) in fromList.enumerate() {
                var newFrom = fromList
                newFrom.removeAtIndex(index)
                permute(newFrom, toList: toList + [item], minStringLen: minStringLen, set: &set)
            }
        }
    }

    var set = Set<String>()
    permute(list, toList:[], minStringLen: minStringLen, set: &set)
    return set
}

permute(["A", "B", "C"])
// {"BA", "AC", "ABC", "AB", "BCA", "CB", "BC", "CAB", "ACB", "CA", "CBA", "BAC"}

permute(["A", "A", "B"])
// {"BA", "BAA", "AAB", "AB", "ABA", "AA"}

permute(["A", "A", "B"], minStringLen: 1)
// {"BA", "A", "BAA", "AB", "AA", "B", "AAB", "ABA"}

permute(["A", "A", "B"], minStringLen: 3)
// {"ABA", "BAA", "AAB"}

答案 1 :(得分:1)

下面列出的代码基本上写在卫生巾的背面,因此虽然它可能没有以任何方式进行优化,但它肯定会产生正确的结果。 基本思想是迭代数组和每个数字:

  1. 创建一个新数组,不包含当前数字。
  2. 全部生成 新数组的可能排列(递归)
  3. 前置 每个生成的排列的当前数字。
  4. 当要生成排列的数组只包含一个元素时,递归终止。

    因此输入[1,2,3]: currentNumber == 1,生成排列为[2,3]的子数组。 递归地生成[2,3]的所有可能的排列给出[2,3]和[3,2]。 将currentNumber(1)加到两者上给出[1,2,3]和[1,3,2]。 当currentNumber等于2和3时也是如此。

    代码:

    func allAvailablePermutationsForArray(array: [Int]) -> [[Int]]
    {
        if (array.count == 1)
        {
            return [array]
        }
    
        var ret = [[Int]]()
        for number in array
        {
            var leftOver = array
            let index = leftOver.indexOf(number)
            leftOver.removeAtIndex(index!)
            let leftOverPermutations = allAvailablePermutationsForArray(leftOver)
            for var permutationsArray in leftOverPermutations
            {
                permutationsArray.insert(number, atIndex: 0)
                ret.append(permutationsArray)
            }
        }
    
        return ret
    }
    

    以下列方式运行:

    let array = [1, 2, 3, 4]
    let res = allAvailablePermutationsForArray(array)
    
    for (index, permuts) in res.enumerate()
    {
        print("\(index + 1):\t\(permuts)")
    }
    

    输出是:

    1:  [1, 2, 3, 4]
    2:  [1, 2, 4, 3]
    3:  [1, 3, 2, 4]
    4:  [1, 3, 4, 2]
    5:  [1, 4, 2, 3]
    6:  [1, 4, 3, 2]
    7:  [2, 1, 3, 4]
    8:  [2, 1, 4, 3]
    9:  [2, 3, 1, 4]
    10: [2, 3, 4, 1]
    11: [2, 4, 1, 3]
    12: [2, 4, 3, 1]
    13: [3, 1, 2, 4]
    14: [3, 1, 4, 2]
    15: [3, 2, 1, 4]
    16: [3, 2, 4, 1]
    17: [3, 4, 1, 2]
    18: [3, 4, 2, 1]
    19: [4, 1, 2, 3]
    20: [4, 1, 3, 2]
    21: [4, 2, 1, 3]
    22: [4, 2, 3, 1]
    23: [4, 3, 1, 2]
    24: [4, 3, 2, 1]