Guest User

Untitled

a guest
Jul 18th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. class Solution {
  2. func permute(_ nums: [Int]) -> [[Int]] {
  3. for num in nums {
  4. let index = nums.index(of: num)!
  5. var newNums = nums
  6. newNums.remove(at: index)
  7. dfs(a: [num], b: newNums)
  8. }
  9. return [[1]]
  10. }
  11.  
  12. func dfs(a: [Int], b:[Int]) {
  13. if b.count == 1 {
  14. print(a+b)
  15. }else {
  16.  
  17.  
  18. for index in 0 ..< b.count {
  19. var tmpA = a
  20. var tmpB = b
  21.  
  22. let bElement = b[index]
  23. tmpA.append(bElement)
  24. tmpB.remove(at: index)
  25.  
  26. dfs(a: tmpA, b: tmpB)
  27. }
  28.  
  29.  
  30.  
  31. /*
  32. var firstNewA = a
  33. var firstNewB = b
  34. let b0 = firstNewB[0]
  35. firstNewA.append(b0)
  36. firstNewB.remove(at: 0)
  37. dfs(a: firstNewA, b: firstNewB)
  38.  
  39. var secondNewA = a
  40. var secondNewB = b
  41. let b1 = secondNewB[1]
  42. secondNewA.append(b1)
  43. secondNewB.remove(at: 1)
  44. dfs(a: secondNewA, b: secondNewB)
  45. */
  46. }
  47. }
  48. }
  49.  
  50. let sol = Solution()
  51. sol.permute([1,2,3,4])
Add Comment
Please, Sign In to add comment