From ce692c840eceeb7b370879c075868a0c43db771a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Borba?= Date: Sat, 19 Sep 2026 09:21:44 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=9F=A1=20Port=20problem=206=20(Zigzag=20C?= =?UTF-8?q?onversion)=20from=20Clojure=20to=20C++23?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces clojure/6.clj (and its clojure/input.txt fixture) with solutions/0001-0100/0006-zigzag-conversion/. The Clojure version didn't actually take the string as a parameter — it always slurped the same clojure/input.txt file regardless of the numRows argument, so it wasn't a general solution. Replaced with the standard row-simulation algorithm that takes (s, numRows) directly, verified against LeetCode's own examples. --- clojure/6.clj | 34 ------------------- clojure/input.txt | 3 -- .../0006-zigzag-conversion/README.md | 12 +++++++ .../0006-zigzag-conversion/solution.cpp | 34 +++++++++++++++++++ .../0006-zigzag-conversion/solution.hpp | 11 ++++++ .../0001-0100/0006-zigzag-conversion/test.cpp | 9 +++++ 6 files changed, 66 insertions(+), 37 deletions(-) delete mode 100644 clojure/6.clj delete mode 100644 clojure/input.txt create mode 100644 solutions/0001-0100/0006-zigzag-conversion/README.md create mode 100644 solutions/0001-0100/0006-zigzag-conversion/solution.cpp create mode 100644 solutions/0001-0100/0006-zigzag-conversion/solution.hpp create mode 100644 solutions/0001-0100/0006-zigzag-conversion/test.cpp 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"); +}