diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 084. \345\220\253\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 084. \345\220\253\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 998bcffb292d6..9cf8d398b21dd 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 084. \345\220\253\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 084. \345\220\253\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" @@ -222,6 +222,40 @@ public class Solution { } ``` +#### Swift + +```swift +class Solution { + func permuteUnique(_ nums: [Int]) -> [[Int]] { + var res = [[Int]]() + var path = [Int]() + var used = [Bool](repeating: false, count: nums.count) + let sortedNums = nums.sorted() + dfs(0, sortedNums.count, sortedNums, &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.. 0 && nums[i] == nums[i - 1] && !used[i - 1]) { + continue + } + path.append(nums[i]) + used[i] = true + dfs(u + 1, n, nums, &used, &path, &res) + used[i] = false + path.removeLast() + } + } +} +``` + diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 084. \345\220\253\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 084. \345\220\253\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..4e9d6b55b69d8 --- /dev/null +++ "b/lcof2/\345\211\221\346\214\207 Offer II 084. \345\220\253\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,29 @@ +class Solution { + func permuteUnique(_ nums: [Int]) -> [[Int]] { + var res = [[Int]]() + var path = [Int]() + var used = [Bool](repeating: false, count: nums.count) + let sortedNums = nums.sorted() + dfs(0, sortedNums.count, sortedNums, &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.. 0 && nums[i] == nums[i - 1] && !used[i - 1]) { + continue + } + path.append(nums[i]) + used[i] = true + dfs(u + 1, n, nums, &used, &path, &res) + used[i] = false + path.removeLast() + } + } +} \ No newline at end of file