SHARE
TWEET

251. Flatten 2D Vector

HXXXXJ Mar 18th, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top