25.03.2013 Views

Cracking the Coding Interview - Fooo

Cracking the Coding Interview - Fooo

Cracking the Coding Interview - Fooo

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Solutions 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!