C# solution: most logic in hasNext();


  • 0
    B
    public class Vector2D 
    {
    	private int curRow;
    	private int curCol;
    	
    	private IList<IList<int>> list2d;
    	
        public Vector2D(IList<IList<int>> vec2d) 
    	{
            list2d = vec2d;
    		curRow = 0;
    		curCol = 0;
        }
    
        public bool HasNext() 
    	{
            while (curRow < list2d.Count && curCol >= list2d[curRow].Count)
    		{
    			curCol = 0;
    			curRow++;
    		}
    
    		if (curRow < list2d.Count) return true;
    		
    		return false;
        }
    
        public int Next() 
    	{
            var result = list2d[curRow][curCol];
    		curCol++;
    		return result;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.