Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: leetcode zigzag solution #897

Merged
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
4ff5849
perf: faster implementation of the TwoSum problem
straight-into-the-wall Oct 21, 2021
6453a9e
doc: fixed typos on comments
straight-into-the-wall Oct 21, 2021
80e54d9
updating DIRECTORY.md
Oct 21, 2021
e45b4d9
feat: leetcode ZigZag conversion solution
straight-into-the-wall Oct 22, 2021
6d3985c
doc: leetcode README added ZigZag conversion solution
straight-into-the-wall Oct 22, 2021
ab710ee
fix: clang-tidy linter corrections
straight-into-the-wall Oct 22, 2021
9e8e4b8
doc: fixed typo on leetcode README
straight-into-the-wall Oct 22, 2021
495d780
Update leetcode/src/6.c
straight-into-the-wall Oct 22, 2021
30f3cea
Update leetcode/src/6.c
straight-into-the-wall Oct 22, 2021
335fbb0
fix: unsigned int and include comments
straight-into-the-wall Oct 22, 2021
420628e
fix: merging static test func
straight-into-the-wall Oct 22, 2021
23c0485
fix: comments on includes. Doxygen file. static test function
straight-into-the-wall Oct 22, 2021
b46a6af
Merge branch 'master' into leetcode/zigzag
straight-into-the-wall Oct 24, 2021
ce5fb53
fix: add missing headers
straight-into-the-wall Oct 24, 2021
35a75f4
Delete 1.c
straight-into-the-wall Oct 24, 2021
3a627d7
Revert "Merge branch 'master' into leetcode/zigzag"
straight-into-the-wall Oct 24, 2021
13330eb
Merge branch 'master' of github.com:TheAlgorithms/C
straight-into-the-wall Oct 25, 2021
e57c16d
fix: revert 1.c
straight-into-the-wall Oct 25, 2021
310c333
updating DIRECTORY.md
Oct 25, 2021
a18708c
Empty commit to test the CI
Panquesito7 Oct 31, 2021
def0eb6
Update leetcode/src/6.c
straight-into-the-wall Nov 4, 2021
143a741
Update leetcode/src/6.c
straight-into-the-wall Nov 4, 2021
00985b4
Merge branch 'TheAlgorithms:master' into master
straight-into-the-wall Nov 9, 2021
1f4a876
Merge remote-tracking branch 'upstream/master'
straight-into-the-wall Nov 21, 2021
d7923cd
fix: missing main
straight-into-the-wall Nov 21, 2021
ec83f11
Merge branch 'master' into leetcode/zigzag
straight-into-the-wall Nov 21, 2021
fca859c
updating DIRECTORY.md
Nov 21, 2021
3af6340
doc: added missing comment
straight-into-the-wall Nov 21, 2021
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
1 change: 1 addition & 0 deletions leetcode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
139 changes: 139 additions & 0 deletions leetcode/src/6.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/**
* @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);
}