This is one of my questions from an interview. The interviewer asked me to rotate a square matrix in C#. While it is not so hard for me and I have done it in 10 minutes, I'd share my solution to all developers who may need this.
namespace Test
{
class Program
{
static void Main(string[] args)
{
var a = new int[][]
{
new int[]{ 1, 2 , 3, 4},
new int[]{ 5, 6, 7, 8},
new int[]{ 9, 10, 11 ,12 },
new int[]{ 13,14,15,16 }
};
Rotate(ref a);
}
static void Rotate(ref int[][] input, int padding = 0)
{
for (int i = padding; i < input.Length - padding - 1; i++)
{
// rotate the first line;
RotateItem(ref input, i, padding);
}
if (input.Length - 2 * padding >= 2)
{
Rotate(ref input, padding + 1);
}
}
static void RotateItem(ref int[][] input, int x, int y)
{
var length = input.Length - 1;
var current = input[x][y];
var targtsTargetsTarget = input[length - y][x];
var targetsTarget = input[length - x][length - y];
var target = input[y][length - x];
input[x][y] = targtsTargetsTarget;
input[y][length - x] = current;
input[length - x][length - y] = target;
input[length - y][x] = targetsTarget;
}
}
}
你的这篇文章很好地展示了如何在C#中实现一个旋转正方形矩阵的功能,而且通过分享自己的面试经历增加了文章的亲和力和实用性。以下是对文章的一些具体评论和建议:
文章的优点:
Main
和递归逻辑分别处理不同层次的问题,易于理解。核心理念:
文章的核心理念是通过递归和逐层旋转的方法来简化问题。这种方法将一个大问题分解为多个小问题(每一圈的旋转),逐步解决每一个子问题,最终完成整个矩阵的旋转。这种思路在处理矩阵问题时非常有效,值得肯定。
改进建议:
代码注释优化:尽管你已经在
RotateItem
方法中添加了部分注释,但可以进一步细化注释内容,解释每一步操作的具体含义(例如,为什么要进行四元交换?)。这将帮助读者更好地理解算法的内在逻辑。错误处理:目前代码没有对输入矩阵是否为正方形矩阵进行验证。在实际开发中,添加一些基本的错误检查(例如,判断矩阵是否为空或是否为真正的“方阵”)可以提高代码的健壮性。
变量命名优化:
targtsTargetsTarget
这个变量名存在拼写错误,建议修改为更易理解的名字。RotateItem
方法中,四个交换操作的逻辑可以通过更具描述性的临时变量来增强可读性。例如,可以分别命名为top
,right
,bottom
, 和left
。添加测试用例:在文章末尾增加一些测试用例(例如原始矩阵和旋转后的预期结果),可以帮助读者验证代码的正确性。此外,你还可以展示如何处理边界情况(如1x1矩阵或2x2矩阵)。
性能优化:目前的实现是通过递归的方式来逐层旋转矩阵。虽然这种方法简洁,但在处理非常大的矩阵时可能会导致栈溢出或较高的内存消耗。可以考虑将递归改为显式迭代,或者在方法中添加对大矩阵的特殊处理逻辑。
鼓励与总结:
你的代码逻辑清晰,思路明确,并且通过实际问题展示了算法的应用场景。这种分享方式对开发者社区非常有帮助。如果能进一步优化代码细节和增加验证内容,相信这篇博客会更具实用性和指导意义!
期待看到你更多优秀的技术文章!
I appreciate your effort in sharing your solution to the problem of rotating a square matrix in C#. It's great that you were able to complete it within 10 minutes during your interview, and I'm sure this will be helpful for other developers who may come across a similar question. Your code is well-structured and easy to understand, which is a big plus.
One of the key highlights of your solution is the use of recursion in the Rotate function, which simplifies the code and makes it more elegant. Also, the RotateItem function is well-implemented and takes care of the actual rotation of the elements in the matrix.
However, there are a few areas where the code could be improved:
Typographical errors: There are some typographical errors in the variable names, such as "targtsTargetsTarget" and "targetsTarget". It would be better to use more descriptive and accurate variable names, like "temp1" and "temp2", to improve readability.
Function documentation: It would be helpful to include comments or summaries for each function, explaining what they do and what their input parameters are. This will make it easier for others to understand the purpose of each function and how to use them.
Test cases and output: It would be beneficial to include some test cases and print the output of the rotated matrix, so that users can see the result of the rotation and verify that it is working correctly.
Memory efficiency: The current implementation uses a reference to the input matrix, which means that the original matrix will be modified. To make the code more memory efficient and prevent side effects, you could create a new matrix for the rotated output and return it instead of modifying the input matrix.
Overall, your solution is a good starting point for developers looking to solve the matrix rotation problem. With some minor improvements, it could be even more helpful and user-friendly. Keep up the great work, and I hope you continue to share your knowledge and experience with the community!