mirror of
https://github.com/TheAlgorithms/C
synced 2025-05-17 01:18:10 +03:00

* perf: faster implementation of the TwoSum problem * doc: fixed typos on comments * updating DIRECTORY.md * feat: leetcode ZigZag conversion solution * doc: leetcode README added ZigZag conversion solution * fix: clang-tidy linter corrections * doc: fixed typo on leetcode README * Update leetcode/src/6.c @file does not want parameters Co-authored-by: David Leal <halfpacho@gmail.com> * Update leetcode/src/6.c Co-authored-by: David Leal <halfpacho@gmail.com> * fix: unsigned int and include comments * fix: comments on includes. Doxygen file. static test function * fix: add missing headers * Delete 1.c * Revert "Merge branch 'master' into leetcode/zigzag" This reverts commit b46a6afd52ae950e54a9c3ae04d54c523965f0bf. * fix: revert 1.c * updating DIRECTORY.md * Empty commit to test the CI * Update leetcode/src/6.c Co-authored-by: David Leal <halfpacho@gmail.com> * Update leetcode/src/6.c Co-authored-by: David Leal <halfpacho@gmail.com> * fix: missing main * updating DIRECTORY.md * doc: added missing comment Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: David Leal <halfpacho@gmail.com>
152 lines
3.8 KiB
C
152 lines
3.8 KiB
C
/**
|
|
* @file
|
|
* @brief Implementation of the [ZigZag
|
|
* Conversion](https://leetcode.com/problems/zigzag-conversion/) Leetcode
|
|
* problem
|
|
* @details
|
|
* A decent solution to the ZigZag conversion problem.
|
|
* Take advantage of the fact that the maximum gap between the chars is 2 times
|
|
* the depth(the number of rows).
|
|
* The actual gap between the two first chars of a rows depends on the depth of
|
|
* the row. The gaps between successives chars on the same row is the complement
|
|
* of the first gap to the maximum gap.
|
|
* @author [straight_into_the_wall](https://github.com/straight-into-the-wall)
|
|
*/
|
|
|
|
#include <assert.h> /// for assert
|
|
#include <stdint.h> /// for unsigned int with fixed size
|
|
#include <stdio.h> /// for IO operations
|
|
#include <stdlib.h> /// for malloc
|
|
#include <string.h> /// for string tools
|
|
|
|
/**
|
|
* @brief Convert a string to the it's zigzag equivalent on a given number of
|
|
* rows.
|
|
* @param in the string in input.
|
|
* @param numRows the desired number of rows.
|
|
* @returns the converted new (malloced) string.
|
|
*/
|
|
char* convert(char* in, uint16_t numRows)
|
|
{
|
|
uint16_t len = strlen(in);
|
|
|
|
if (len < numRows)
|
|
{
|
|
numRows = len;
|
|
}
|
|
char* out = calloc(len + 1, sizeof(char));
|
|
|
|
if (numRows < 2)
|
|
{
|
|
memcpy(out, in, len + 1);
|
|
return out;
|
|
}
|
|
|
|
uint16_t max = numRows - 1;
|
|
uint16_t rr = 2 * max;
|
|
uint16_t i = 0;
|
|
uint16_t o = 0;
|
|
uint16_t delta = 0;
|
|
|
|
// first row
|
|
while (i < len)
|
|
{
|
|
out[o++] = in[i];
|
|
i += rr;
|
|
}
|
|
|
|
// middle rows
|
|
for (uint16_t l = 1; l < max; l++)
|
|
{
|
|
i = l;
|
|
delta = 2 * l;
|
|
while (i < len)
|
|
{
|
|
out[o++] = in[i];
|
|
delta = rr - delta;
|
|
i += delta;
|
|
}
|
|
}
|
|
|
|
// last row
|
|
i = max;
|
|
while (i < len)
|
|
{
|
|
out[o++] = in[i];
|
|
i += rr;
|
|
}
|
|
|
|
return out;
|
|
}
|
|
|
|
/**
|
|
* @brief Self-test implementations
|
|
* @returns void
|
|
*/
|
|
static void testZigZag(char* s, int numRows, char* expected)
|
|
{
|
|
char* ret = convert(s, numRows);
|
|
int len = strlen(s);
|
|
int cmp = strncmp(ret, expected, len);
|
|
assert(!cmp);
|
|
|
|
free(ret);
|
|
}
|
|
|
|
/**
|
|
* @brief Self-test implementations
|
|
* @returns void
|
|
*/
|
|
static void test()
|
|
{
|
|
char* s01 = "PAYPALISHIRING";
|
|
|
|
char* r01 = "PINALSIGYAHRPI";
|
|
testZigZag(s01, 4, r01);
|
|
|
|
char* r02 = "PAHNAPLSIIGYIR";
|
|
testZigZag(s01, 3, r02);
|
|
|
|
char* s03 = "A";
|
|
testZigZag(s03, 1, s03);
|
|
testZigZag(s03, 3, s03);
|
|
|
|
char* s04 =
|
|
"cbxdwjccgtdoqiscyspqzvuqivzptlpvooynyapgvswoaosaghrffnxnjyeeltzaiznicc"
|
|
"ozwknwyhzgpqlwfkjqipuu"
|
|
"jvwtxlbznryjdohbvghmyuiggtyqjtmuqinntqmihntkddnalwnmsxsatqqeldacnnpjfe"
|
|
"rmrnyuqnwbjjpdjhdeavkn"
|
|
"ykpoxhxclqqedqavdwzoiorrwwxyrhlsrdgqkduvtmzzczufvtvfioygkvedervvudnegh"
|
|
"bctcbxdxezrzgbpfhzanff"
|
|
"eccbgqfmzjqtlrsppxqiywjobspefujlxnmddurddiyobqfspvcoulcvdrzkmkwlyiqdch"
|
|
"ghrgytzdnobqcvdeqjystm"
|
|
"epxcaniewqmoxkjwpymqorluxedvywhcoghotpusfgiestckrpaigocfufbubiyrrffmwa"
|
|
"eeimidfnnzcphkflpbqsvt"
|
|
"dwludsgaungfzoihbxifoprwcjzsdxngtacw";
|
|
|
|
char* r04 =
|
|
"cbxdwjccgtdoqiscyspqzvuqivzptlpvooynyapgvswoaosaghrffnxnjyeeltzaiznicc"
|
|
"ozwknwyhzgpqlwfkjqipuu"
|
|
"jvwtxlbznryjdohbvghmyuiggtyqjtmuqinntqmihntkddnalwnmsxsatqqeldacnnpjfe"
|
|
"rmrnyuqnwbjjpdjhdeavkn"
|
|
"ykpoxhxclqqedqavdwzoiorrwwxyrhlsrdgqkduvtmzzczufvtvfioygkvedervvudnegh"
|
|
"bctcbxdxezrzgbpfhzanff"
|
|
"eccbgqfmzjqtlrsppxqiywjobspefujlxnmddurddiyobqfspvcoulcvdrzkmkwlyiqdch"
|
|
"ghrgytzdnobqcvdeqjystm"
|
|
"epxcaniewqmoxkjwpymqorluxedvywhcoghotpusfgiestckrpaigocfufbubiyrrffmwa"
|
|
"eeimidfnnzwccpahtkgfnl"
|
|
"xpdbsqzsjvctwdrwploufdisxgbahuinogzf";
|
|
|
|
testZigZag(s04, 472, r04);
|
|
}
|
|
|
|
/**
|
|
* @brief Main function
|
|
* @returns 0 on exit
|
|
*/
|
|
int main(void)
|
|
{
|
|
test(); // run self-test implementations
|
|
return 0;
|
|
}
|