Sunday, July 19, 2015

Leetcode 37: Sudoku Solver

July 19, 2015

Problem statement:
Write a program to solve a Sudoku puzzle by filling the empty cells.
Empty cells are indicated by the character '.'.
You may assume that there will be only one unique solution.

Solution 1: 

Great blog to read:

And then, start to implement the solution using C# code:

Solution 2: 

Read the blog,

and then, implement the solution using c# code:

Solution 3: (good workout on C# KeyValuePair class)

and then, convert C++ code to C# code from the blog:

Excellent code in C++, using class for node on the board. Learn a few things, fun to play with the code

C# code:

solution 4:
source code from the blog:

Solution 5:
read the blog: (Good coding! practice more based on this blog)

and convert the C++ code to C# code, (great workout on C# LinkedList for blank nodes)

Solution 6:
read the blog:

and convert Java code to C# code, great workout on C# and logic checking "return false"

Solution 7:
C# code:

Solution 8:
Thanks for the blog's highlight line of code on back tracking; finally, I got it! My logic thinking has flaws on back tracking; extra backtracking is not a good. Minimize the back tracking, only do it when "return false". It makes sense to do that.

C# code:

Solution 10:
blog: (Excellent implementation! no extra line or number in the code! Best for memorization! Go through other solutions later. )

C# code:

算法理解了, 代码可以记住了; 开始看不同的题解, 看看高手的代码; 从不同的题解中, 模仿模仿! 像打网球, 多接触不同的打法, 开阔眼界; 接着看这道题的题解. 试着从不同角度看一个问题, 多练习改代码; 看自己能不能有自己的看法, 去尝试一点更改, 玩一点花样; 增加练习C#编程的机会.

Also, the code written has been work on readability, learned through my favorite book reading:

Those favorite rules I like to learn, pick up and follow:
Big fan of DRY (Do not repeat yourself) principle, do one thing a time, break giant expression, using explaining variable or summary variable, and abstract the thing to a function, extract a subproblem to a function. The code is also modified to fit into short memory, less mental baggage to read through. 

Read solutions:

BFS, using queue - try to convert it to C#

No comments:

Post a Comment