Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 0 additions & 34 deletions clojure/6.clj

This file was deleted.

3 changes: 0 additions & 3 deletions clojure/input.txt

This file was deleted.

12 changes: 12 additions & 0 deletions solutions/0001-0100/0006-zigzag-conversion/README.md
Original file line number Diff line number Diff line change
@@ -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.
34 changes: 34 additions & 0 deletions solutions/0001-0100/0006-zigzag-conversion/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "solution.hpp"

#include <iterator>
#include <vector>

namespace leetcode::p0006 {

std::string solve(const std::string& s, int numRows) {
if (numRows == 1 || numRows >= std::ssize(s)) {
return s;
}

std::vector<std::string> rows(static_cast<std::size_t>(numRows));
int row = 0;
int direction = -1;

for (char c : s) {
rows[static_cast<std::size_t>(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
11 changes: 11 additions & 0 deletions solutions/0001-0100/0006-zigzag-conversion/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <string>

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
9 changes: 9 additions & 0 deletions solutions/0001-0100/0006-zigzag-conversion/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <catch2/catch_test_macros.hpp>

#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");
}
Loading