Generate n x n Matrix in Spiral Order with LeetCode #59

🧮 𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝐃𝐚𝐢𝐥𝐲 𝐂𝐡𝐚𝐥𝐥𝐞𝐧𝐠𝐞 #𝟓𝟗 – 𝐒𝐩𝐢𝐫𝐚𝐥 𝐌𝐚𝐭𝐫𝐢𝐱 𝐈𝐈 ⏺ 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐃𝐞𝐟𝐢𝐧𝐢𝐭𝐢𝐨𝐧 (𝐟𝐫𝐨𝐦 𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞): Given an integer n, generate an n × n matrix filled with numbers 1 to n² in spiral order. 📖 𝐄𝐱𝐚𝐦𝐩𝐥𝐞: n = 3 → [[1,2,3],[8,9,4],[7,6,5]] 💡𝐄𝐚𝐬𝐲 𝐰𝐚𝐲 𝐭𝐨 𝐮𝐧𝐝𝐞𝐫𝐬𝐭𝐚𝐧𝐝: Fill the matrix like a spiral path: ➡️ top row → ⬇️ right column → ⬅️ bottom row → ⬆️ left column, then shrink the boundaries and repeat. ✨ 𝐊𝐞𝐲 𝐈𝐝𝐞𝐚: Use 4 boundaries (top, bottom, left, right) and keep moving them inward after each direction. 📝 𝐂# 𝐒𝐨𝐥𝐮𝐭𝐢𝐨𝐧: The complete code with comments is shown in the attached image 👇 📌 Tomorrow: 𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 #𝟔𝟎 – 𝐏𝐞𝐫𝐦𝐮𝐭𝐚𝐭𝐢𝐨𝐧 𝐒𝐞𝐪𝐮𝐞𝐧𝐜𝐞 (C#). Stay tuned! #LeetCode #CSharp #CodingChallenge #ProblemSolving #Algorithms #Matrix

  • graphical user interface, text, application, email

To view or add a comment, sign in

Explore content categories