Advertisement
HXXXXJ

251. Flatten 2D Vector

Mar 18th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.64 KB | None | 0 0
  1. class Vector2D {
  2.     var x : Int
  3.     var y : Int
  4.     let map : [[Int]]
  5.     init(_ v: [[Int]]) {
  6.         map = v
  7.         x = 0
  8.         y = 0
  9.     }
  10.    
  11.     func next() -> Int {
  12.         hasNext()
  13.         let n = map[x][y]
  14.         y += 1
  15.         return n
  16.     }
  17.    
  18.     func hasNext() -> Bool {
  19.         if map.count == 0 {return false}
  20.         if y >= map[x].count{
  21.             x += 1
  22.             //find next available row
  23.             while x < map.count && map[x].count == 0 {
  24.                 x += 1
  25.             }
  26.            
  27.             if x >= map.count {return false}
  28.             y = 0
  29.         }
  30.         return true
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement