Advertisement
HXXXXJ

House 链接问题

Apr 12th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.64 KB | None | 0 0
  1. struct House{
  2.     let id : Int
  3.     let left : Int
  4.     let right : Int
  5.     init(_ id: Int, _ left: Int, _ right: Int) {
  6.         self.id = id
  7.         self.left = left
  8.         self.right = right
  9.     }
  10. }
  11.  
  12. func getBlock(_ arr: [House]) -> Int{
  13.     var visited = Set<Int>()
  14.     var count = arr.count
  15.     for house in arr{
  16.         if visited.contains(house.left) { count -= 1}
  17.         if visited.contains(house.right) { count -= 1}
  18.         visited.insert(house.id)
  19.     }
  20.     return count
  21. }
  22.  
  23. let h3 = House(3, 2,4)
  24. //let h2 = House(2, 1,3)
  25. let h1 = House(1, 0,2)
  26. let h7 = House(7, 6,8)
  27.  
  28. let arr = [h1,h3,h7]
  29.  
  30. print(getBlock(arr))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement