feat: leetcode zigzag solution (#897)

* 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 b46a6afd52.

* 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>
This commit is contained in:
straight-into-the-wall 2021-12-09 18:51:02 +01:00 committed by GitHub
parent 1370c44aee
commit 37534f4d8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 153 additions and 0 deletions

View File

@ -222,6 +222,7 @@
* [520](https://github.com/TheAlgorithms/C/blob/master/leetcode/src/520.c)
* [53](https://github.com/TheAlgorithms/C/blob/master/leetcode/src/53.c)
* [561](https://github.com/TheAlgorithms/C/blob/master/leetcode/src/561.c)
* [6](https://github.com/TheAlgorithms/C/blob/master/leetcode/src/6.c)
* [617](https://github.com/TheAlgorithms/C/blob/master/leetcode/src/617.c)
* [647](https://github.com/TheAlgorithms/C/blob/master/leetcode/src/647.c)
* [66](https://github.com/TheAlgorithms/C/blob/master/leetcode/src/66.c)

View File

@ -10,6 +10,7 @@ LeetCode
|2|[Add Two Numbers](https://leetcode.com/problems/add-two-numbers/) | [C](./src/2.c)|Medium|
|3|[Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [C](./src/3.c)|Medium|
|4|[Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/) | [C](./src/4.c)|Hard|
|6|[ZigZag conversion](https://leetcode.com/problems/zigzag-conversion/) | [C](./src/4.c)|Medium|
|7|[Reverse Integer](https://leetcode.com/problems/reverse-integer/) | [C](./src/7.c)|Easy|
|8|[String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi) | [C](./src/8.c)|Medium|
|9|[Palindrome Number](https://leetcode.com/problems/palindrome-number/) | [C](./src/9.c)|Easy|

151
leetcode/src/6.c Normal file
View File

@ -0,0 +1,151 @@
/**
* @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;
}