diff --git a/cpp/2204/220413.cpp b/cpp/2204/220413.cpp new file mode 100644 index 0000000..8e886cd --- /dev/null +++ b/cpp/2204/220413.cpp @@ -0,0 +1,51 @@ +#include +#include + +/** + * 59. Spiral Matrix II + * Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order. + */ + +class Solution { +private: + inline static const int dX[] = {0, 1, 0, -1}; + inline static const int dY[] = {1, 0, -1, 0}; +public: + static std::vector> generateMatrix(int n) { + int* buf = new int[n * n]{}; + int direction = 0; + + auto GetNext = [&](int& x, int& y) { + int nx = x + dX[direction], ny = y + dY[direction]; + if (nx < 0 || ny < 0 || nx >= n || ny >= n || buf[nx * n + ny]) { + direction = (1 + direction) % 4; + nx = x + dX[direction], ny = y + dY[direction]; + } + x = nx; + y = ny; + }; + + int x = 0, y = 0; + for (int i = 1; i <= n * n; ++i) { + buf[x * n + y] = i; + GetNext(x, y); + } + + std::vector> ret; + ret.reserve(n); + for (int i = 0; i < n * n; i += n) + ret.emplace_back(buf + i, buf + i + n); + return ret; + } +}; + +int main() { + auto x = Solution::generateMatrix(4); + for (const auto& i : x) { + for (const auto& j : i) { + std::cout << j << ' '; + } + std::cout << '\n'; + } + return 0; +} diff --git a/cpp/2204/CMakeLists.txt b/cpp/2204/CMakeLists.txt index 65412af..4c4b564 100644 --- a/cpp/2204/CMakeLists.txt +++ b/cpp/2204/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2204) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2204 220412.cpp) +ADD_EXECUTABLE(2204 220413.cpp)