Cs 307 Spring 2004 Midterm 2 1 A. 4 B. 5 4 3 2 1 0 -1 C. 7 5 3 1 -1 1 3 5 7 D. 37 E. O(N) F. 3N^2 + 4N + 3 G. O(N^2) H. O(N) I. O(NlogN) J. O(N^4) K. 8 sec L. 800 / 19 sec M. 4 3 2 1 0 N. 8 6 4 O. 11 8 2 20 2. soln 1: Object temp; Iterator it = items.iterator(); while( it.hasNext() ) { temp = it.next(); if( target.equals( temp ) ) it.remove(); } soln 2: Object temp; int i = 0; while( i < items.size() ) { temp = items.get(i); if( target.equals(temp)) items.remove(i); else i++; } soln 3: while( items.indexOf(target) != -1 ) { items.remove( items.indexOf( target ) ); } soln 4: (Gacky) Object temp; for(int i = 0; i < items.size(); i++) { temp = items.get(i); if(target.equals(temp)) { items.remove(i); i--; } } 3. Object result; if(iMySize == 1) { result = myTail.getData(); myHead = null; myTail = null; } else { Node temp = myHead; for(int i = 0; i < iMySize - 2; i++) temp = temp.getNext(); result = myTail.getData(); myTail = temp; myTail.setNext(null); } iMySize--; return result; 4. land[row][col] = 4; helper(land, row, col); private void helper(int[][] land, int row, int col) { //base cases: out of bounds, clear, or burnt out -> do nothing if( row >= 0 && row < land.length && col >= 0 && col < land[0].length && land[row][col] != 0 && land[row][col] != -1 ) { double rand = Math.random(); boolean burns = (land[row][col] == 4) || (land[row][col] == 1 && rand < 0.25) || ( (land[row][col] == 2 || land[row][col] == 3) && rand < 0.5) ); if( burns ) { land[row][col] = -1; helper(land, row - 1, col); helper(land, row, col + 1); helper(land, row + 1, col); helper(land, row, col - 1); } } }