Dec. 6, 2016
Problem statement:
https://www.hackerrank.com/challenges/matrix-rotation-algo
Study code:
https://gist.github.com/jianminchen/fd5e7e7e4a031971e56dc30a949e5421
Code review:
Julia took 20 minutes to go over the code, and then understood the code.
A few changes:
/*
* Code review:
* a -> matrix - function argument
* amount -> steps
* k -> step, for loop statement
* buffer -> prev
* buffer1 -> cur
*
* Good ideas to study:
* 1. height, width variables are declared
* 2. go through the ring: start from second row, first column, anti-clockwise, one-by-one
* if/else if statement - 4 lines - very easy to follow
*/
Study do while loop, and also if/else if to do anti-clockwise rotation starting from left-upper corner.
https://gist.github.com/jianminchen/91719129b81eefb19f662615b4e3ab95
Great tip Julia likes to share:
Instead of using four for loop to go through the ring, use if/else if to make it one loop - using do/ while. Easy to write, only 4 lines.
Compare two solutions:
Current one in the blog: smart choice, using one loop - do while - 4 if/else (Excellent!)
This solution can be memorized, and cut the time to write in less than 10 minutes.
https://gist.github.com/jianminchen/91719129b81eefb19f662615b4e3ab95
Previous one: kind of verbose, dumb, 4 for loops.
https://gist.github.com/jianminchen/e943a9e7b86d24e9f36c04f1f3502941
No comments:
Post a Comment