27.10.2014 Views

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Solutions</strong> to Chapter 20 | Hard<br />

32 matrix[i][j];<br />

33 }<br />

34 }<br />

35 }<br />

36 return sumMatrix;<br />

37 }<br />

38<br />

39 private static int computeSum(int[][] sumMatrix, int i1, int i2,<br />

40 int j1, int j2) {<br />

41 if (i1 == 0 && j1 == 0) { // starts at row 0, column 0<br />

42 return sumMatrix[i2][j2];<br />

43 } else if (i1 == 0) { // start at row 0<br />

44 return sumMatrix[i2][j2] - sumMatrix[i2][j1 - 1];<br />

45 } else if (j1 == 0) { // start at column 0<br />

46 return sumMatrix[i2][j2] - sumMatrix[i1 - 1][j2];<br />

47 } else {<br />

48 return sumMatrix[i2][j2] - sumMatrix[i2][j1 - 1]<br />

49 - sumMatrix[i1 - 1][j2] + sumMatrix[i1 - 1][j1 - 1];<br />

50 }<br />

51 }<br />

2 9 7<br />

<strong>Cracking</strong> <strong>the</strong> <strong>Coding</strong> <strong>Interview</strong> | Additional Review Problems

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!