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

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

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

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

23 }<br />

24 size--;<br />

25 }<br />

26 }<br />

27 col++;<br />

28 }<br />

29 return sq;<br />

30 }<br />

31<br />

32 private static boolean isSquare(int[][] matrix, int row, int col,<br />

33 int size) {<br />

34 // Check top <strong>and</strong> bottom border.<br />

35 for (int j = 0; j < size; j++){<br />

36 if (matrix[row][col+j] == 1) {<br />

37 return false;<br />

38 }<br />

39 if (matrix[row+size-1][col+j] == 1){<br />

40 return false;<br />

41 }<br />

42 }<br />

43<br />

44 // Check left <strong>and</strong> right border.<br />

45 for (int i = 1; i < size - 1; i++){<br />

46 if (matrix[row+i][col] == 1){<br />

47 return false;<br />

48 }<br />

49 if (matrix[row+i][col+size-1] == 1){<br />

50 return false;<br />

51 }<br />

52 }<br />

53 return true;<br />

54 }<br />

55<br />

56 public class Subsquare {<br />

57 public int row, column, size;<br />

58 public Subsquare(int r, int c, int sz) {<br />

59 row = r;<br />

60 column = c;<br />

61 size = sz;<br />

62 }<br />

63 }<br />

CareerCup.com<br />

2 9 4

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

Saved successfully!

Ooh no, something went wrong!