diff --git a/clojure/6.clj b/clojure/6.clj deleted file mode 100644 index bfa4131..0000000 --- a/clojure/6.clj +++ /dev/null @@ -1,34 +0,0 @@ -(ns 6 - (:require [clojure.test :refer [deftest testing is run-tests]])) - -(defn read-input - [] - (slurp "./clojure/input.txt")) - -(defn convert - [num-rows] - (let [input (read-input) - n (count input) - result (vec (repeat num-rows ""))] - (loop [i 0 - row 0 - direction 1 - result result] - (if (= i n) - (apply str result) - (let [updated-result (update result row str (nth input i)) - new-row (if (or (= row 0) (= row (dec num-rows))) - (+ row direction) - (+ row direction))] - (recur (inc i) - new-row - (if (or (= new-row 0) (= new-row (dec num-rows))) - (- direction) - direction) - updated-result)))))) - -(deftest zigzag-problem - (testing "should return the zigzag string" - (is (= (convert 3) "PAYPALISHIRING")) - (is (= (convert 4) "PINALSIGYAHRPI")) - (is (= (convert 1) "A")))) diff --git a/clojure/input.txt b/clojure/input.txt deleted file mode 100644 index 83dcfc8..0000000 --- a/clojure/input.txt +++ /dev/null @@ -1,3 +0,0 @@ -PAYPALISHIRING -PINALSIGYAHRPI -A \ No newline at end of file diff --git a/solutions/0001-0100/0006-zigzag-conversion/README.md b/solutions/0001-0100/0006-zigzag-conversion/README.md new file mode 100644 index 0000000..7054b59 --- /dev/null +++ b/solutions/0001-0100/0006-zigzag-conversion/README.md @@ -0,0 +1,12 @@ +# 6. Zigzag Conversion + +[LeetCode problem 6](https://leetcode.com/problems/zigzag-conversion/) + +- **Difficulty**: Medium +- **Tags**: string, simulation + +## Description + +Given a string `s` and an integer `numRows`, arrange the characters of `s` in +a zigzag pattern across `numRows` rows (down one column, then diagonally up, +repeating), then read the rows back left to right, top to bottom. diff --git a/solutions/0001-0100/0006-zigzag-conversion/solution.cpp b/solutions/0001-0100/0006-zigzag-conversion/solution.cpp new file mode 100644 index 0000000..39d7ee0 --- /dev/null +++ b/solutions/0001-0100/0006-zigzag-conversion/solution.cpp @@ -0,0 +1,34 @@ +#include "solution.hpp" + +#include +#include + +namespace leetcode::p0006 { + +std::string solve(const std::string& s, int numRows) { + if (numRows == 1 || numRows >= std::ssize(s)) { + return s; + } + + std::vector rows(static_cast(numRows)); + int row = 0; + int direction = -1; + + for (char c : s) { + rows[static_cast(row)] += c; + if (row == 0 || row == numRows - 1) { + direction = -direction; + } + row += direction; + } + + std::string result; + result.reserve(s.size()); + for (const auto& r : rows) { + result += r; + } + + return result; +} + +} // namespace leetcode::p0006 diff --git a/solutions/0001-0100/0006-zigzag-conversion/solution.hpp b/solutions/0001-0100/0006-zigzag-conversion/solution.hpp new file mode 100644 index 0000000..a823471 --- /dev/null +++ b/solutions/0001-0100/0006-zigzag-conversion/solution.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include + +namespace leetcode::p0006 { + +// optimized: O(n) time, O(n) space — simulate the zigzag by appending each +// character to its current row, bouncing the row index at the edges. +std::string solve(const std::string& s, int numRows); + +} // namespace leetcode::p0006 diff --git a/solutions/0001-0100/0006-zigzag-conversion/test.cpp b/solutions/0001-0100/0006-zigzag-conversion/test.cpp new file mode 100644 index 0000000..c0b9420 --- /dev/null +++ b/solutions/0001-0100/0006-zigzag-conversion/test.cpp @@ -0,0 +1,9 @@ +#include + +#include "solution.hpp" + +TEST_CASE("Problem 6", "[p0006]") { + CHECK(leetcode::p0006::solve("PAYPALISHIRING", 3) == "PAHNAPLSIIGYIR"); + CHECK(leetcode::p0006::solve("PAYPALISHIRING", 4) == "PINALSIGYAHRPI"); + CHECK(leetcode::p0006::solve("A", 1) == "A"); +}