diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 083. \346\262\241\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 083. \346\262\241\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227/README.md" index 2114fa3d9b8e6..319bba66e05ce 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 083. \346\262\241\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 083. \346\262\241\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227/README.md" @@ -247,6 +247,38 @@ public class Solution { } ``` +#### Swift + +```swift +class Solution { + func permute(_ nums: [Int]) -> [[Int]] { + var res = [[Int]]() + var path = [Int]() + var used = [Bool](repeating: false, count: nums.count) + dfs(0, nums.count, nums, &used, &path, &res) + return res + } + + private func dfs( + _ u: Int, _ n: Int, _ nums: [Int], _ used: inout [Bool], _ path: inout [Int], _ res: inout [[Int]] + ) { + if u == n { + res.append(path) + return + } + for i in 0.. diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 083. \346\262\241\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227/Solution.swift" "b/lcof2/\345\211\221\346\214\207 Offer II 083. \346\262\241\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227/Solution.swift" new file mode 100644 index 0000000000000..6f72730e15f81 --- /dev/null +++ "b/lcof2/\345\211\221\346\214\207 Offer II 083. \346\262\241\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227/Solution.swift" @@ -0,0 +1,27 @@ +class Solution { + func permute(_ nums: [Int]) -> [[Int]] { + var res = [[Int]]() + var path = [Int]() + var used = [Bool](repeating: false, count: nums.count) + dfs(0, nums.count, nums, &used, &path, &res) + return res + } + + private func dfs( + _ u: Int, _ n: Int, _ nums: [Int], _ used: inout [Bool], _ path: inout [Int], _ res: inout [[Int]] + ) { + if u == n { + res.append(path) + return + } + for i in 0..