diff --git a/DIRECTORY.md b/DIRECTORY.md index 518538062f..3d5e5255f6 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -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) diff --git a/leetcode/README.md b/leetcode/README.md index ef59a3c41e..0cd07e3d96 100644 --- a/leetcode/README.md +++ b/leetcode/README.md @@ -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| diff --git a/leetcode/src/6.c b/leetcode/src/6.c new file mode 100644 index 0000000000..0c67b4f8db --- /dev/null +++ b/leetcode/src/6.c @@ -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 /// for assert +#include /// for unsigned int with fixed size +#include /// for IO operations +#include /// for malloc +#include /// 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; +}