Permalink
Browse files

Merge remote branch 'siobhan/master'

  • Loading branch information...
2 parents d1a3680 + 313bd79 commit 6bfab82756beff342cf8f76ed4e5b57aea7fa939 Barak A. Pearlmutter committed Apr 6, 2011
Showing with 63 additions and 0 deletions.
  1. +40 −0 print_nxn_array_in_spiral_order
  2. +23 −0 reverseString
@@ -0,0 +1,40 @@
+
+Print n x n array in spiral order
+
+E.g. n=5, 5x5
+
+1 2 3 4 5 (0,0) (0,1) (0,2) (0,3) (0,4) * > - - v
+6 7 8 9 10 (1,0) (1,1) (1,2) (1,3) (1,4) > - - v |
+11 12 13 14 15 (2,0) (2,1) (2,2) (2,3) (2,4) | > * | |
+16 17 18 19 20 (3,0) (3,1) (3,2) (3,3) (3,4) | ^ - < |
+21 22 23 24 25 (4,0) (4,1) (4,2) (4,3) (4,4) ^ - - - <
+
+
+O(n) * O(4k)
+= O(n)
+
+int[][] array = {1,2,3,4,5,6,7,8,...,n};
+j=0;
+for(i=n-1; i>0; i--){ //O(n)
+ for(k=j; k<n; k++){ //O(k)
+ write(array[j][k]); //prints top line (eg i=4, j=0 -> 1 2 3 4 5)
+ }
+
+ j++;
+
+ for(k=j; k<n; k++){ //O(k)
+ write(array[k][i]); //prints right side (eg i=4, j=1 -> 10, 15, 20, 25)
+ }
+
+ check = n-i-1;
+ for(k=i-1; k>=check; k--){ //O(k)
+ write(array[i][k]); //prints bottom line (eg i=4; j=1 -> 24, 23, 22, 21)
+ }
+
+ for(k=i-j; k>=j; k--){ //O(k)
+ write(array[k][check]); //prints left line (eg i=4; j=1 -> 16, 11, 6)
+ }
+}
+
+
+
View
@@ -0,0 +1,23 @@
+
+How to reverse a string
+
+Eg. s = quick brown fox
+ r = fox brown quick
+
+O(n) * O(n)
+= O(n^2)
+
+
+s = "quick brown fox";
+r="";
+i=0;
+while(i<s.length()){ //O(n)
+ substring = "";
+ while(s.charAt(i)!=' '){ //O(n)
+ substring = substring + s.charAt(i);
+ i++; //move onto the next character
+ }
+ r = substring + r;
+ i++; //skip past the space
+}
+System.out.println(s + " -> " + r);

0 comments on commit 6bfab82

Please sign in to comment.