6. leetcode — C language

Time:2022-6-6

Method 1:

Execution time:76 ms, beat 23.00% of users in all C submissions
Memory consumption:6.4 MB, beat 60.00% of users in all C submissions

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *zStr(char *str, int numRows)
{
  int k = 0;
  int len = strlen(str);
  if (numRows < 2) {
        return str;
  }
  char *resStr = (char *)malloc((len + 1) * sizeof(char));
  for (int row = 0; row < numRows; row++)
  {
    for (int i = 0; i < len; i++)
    {
      //By observing the Z-shaped string, we can find the following rule: it is grouped into a group of 2 * NumRows - 2 numbers, and the subscript of each group is the sum of the numbers in this group
      //Two sets of results can be obtained: one is equal to row (that is, the subscript of the row of the Z-shaped string, starting from 0), and the other is equal to NumRows - 2
      //Next, start the loop from the first row, and put the qualified elements in the first row into the new array, and then proceed in turn in each row until all the elements are placed into the new array
      if (i % (2 * numRows - 2) == row || i % (2 * numRows - 2) == 2 * numRows - 2 - row)
      {
        resStr[k++] = str[i];
      }
    }
  }
  resStr[len] = '
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *zStr(char *str, int numRows)
{
int k = 0;
int len = strlen(str);
if (numRows < 2) {
return str;
}
char *resStr = (char *)malloc((len + 1) * sizeof(char));
for (int row = 0; row < numRows; row++)
{
for (int i = 0; i < len; i++)
{
//By observing the Z-shaped string, we can find the following rule: it is grouped into a group of 2 * NumRows - 2 numbers, and the subscript of each group is the sum of the numbers in this group
//Two sets of results can be obtained: one is equal to row (that is, the subscript of the row of the Z-shaped string, starting from 0), and the other is equal to NumRows - 2
//Next, start the loop from the first row, and put the qualified elements in the first row into the new array, and then proceed in turn in each row until all the elements are placed into the new array
if (i % (2 * numRows - 2) == row || i % (2 * numRows - 2) == 2 * numRows - 2 - row)
{
resStr[k++] = str[i];
}
}
}
resStr[len] = '\0';
return resStr;
}
int main(void)
{
char *str = "nfgszvcsdx";
int numRows = 5;
char *retStr = zStr(str, numRows);
int i = 0;
while (retStr[i] != '\0')
{
printf("%c", retStr[i]);
i++;
}
printf("\n");
}
'; return resStr; } int main(void) { char *str = "nfgszvcsdx"; int numRows = 5; char *retStr = zStr(str, numRows); int i = 0; while (retStr[i] != '
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *zStr(char *str, int numRows)
{
int k = 0;
int len = strlen(str);
if (numRows < 2) {
return str;
}
char *resStr = (char *)malloc((len + 1) * sizeof(char));
for (int row = 0; row < numRows; row++)
{
for (int i = 0; i < len; i++)
{
//By observing the Z-shaped string, we can find the following rule: it is grouped into a group of 2 * NumRows - 2 numbers, and the subscript of each group is the sum of the numbers in this group
//Two sets of results can be obtained: one is equal to row (that is, the subscript of the row of the Z-shaped string, starting from 0), and the other is equal to NumRows - 2
//Next, start the loop from the first row, and put the qualified elements in the first row into the new array, and then proceed in turn in each row until all the elements are placed into the new array
if (i % (2 * numRows - 2) == row || i % (2 * numRows - 2) == 2 * numRows - 2 - row)
{
resStr[k++] = str[i];
}
}
}
resStr[len] = '\0';
return resStr;
}
int main(void)
{
char *str = "nfgszvcsdx";
int numRows = 5;
char *retStr = zStr(str, numRows);
int i = 0;
while (retStr[i] != '\0')
{
printf("%c", retStr[i]);
i++;
}
printf("\n");
}
') { printf("%c", retStr[i]); i++; } printf("\n"); }

The event complexity of this method is O (numrows*n), n is the string length, and NumRows is the string length.
The space complexity is O (n).
Method 2:
Execution time:0 ms, beat 100.00% of users in all C submissions
Memory consumption:6.6 MB, beat 44.94% of users in all C submissions

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *convert(char *str, int numRows)
{
  int k = 0;
  int len = strlen(str);
  char *resStr = (char *)malloc((len + 1) * sizeof(char));
  //Special case: if the number of lines is less than 2, or the length of the string is not greater than the number of lines, the string is returned directly
  //Because there is only one row or one column at this time
  if (numRows < 2 || len <= numRows)
  {
    return str;
  }
  for (int row = 0; row < numRows; row++)
  {
    //Stores the first character of each line specified by NumRows into the return character
    resStr[k++] = str[row];
    for (int i = row; i < len;)
    {
      //In each NumRows line, find the corresponding element subscript in the original string

      //The second element subscript in each row has the following relationship with the first element subscript: J = I + 2numrows - 2 - 2row
      //Special case: the first and second elements of the last line coincide, so the following expression is: I + = 0 - > I, that is, I does not change
      i += 2 * (numRows - row - 1);
      //Special case: the first and second elements of the last line are coincident, so calculation is not required,
      //Exclude the last row here: NumRows - row > 1
      if (numRows - row > 1 && i < len) {
        resStr[k++] = str[i];
      }
      //The second and third element subscripts in each row have the following relationship: J = I + 2row
      //Special case: the first and second elements of the last line coincide, so the third element and the subscript of the first element conform to this relationship
      i += 2 * row;
      if (row > 0 && i < len)
      {
        resStr[k++] = str[i];
      }
    }
  }
  resStr[k] = '
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *convert(char *str, int numRows)
{
int k = 0;
int len = strlen(str);
char *resStr = (char *)malloc((len + 1) * sizeof(char));
//Special case: if the number of lines is less than 2, or the length of the string is not greater than the number of lines, the string is returned directly
//Because there is only one row or one column at this time
if (numRows < 2 || len <= numRows)
{
return str;
}
for (int row = 0; row < numRows; row++)
{
//Stores the first character of each line specified by NumRows into the return character
resStr[k++] = str[row];
for (int i = row; i < len;)
{
//In each NumRows line, find the corresponding element subscript in the original string
//The second element subscript in each row has the following relationship with the first element subscript: J = I + 2numrows - 2 - 2row
//Special case: the first and second elements of the last line coincide, so the following expression is: I + = 0 - > I, that is, I does not change
i += 2 * (numRows - row - 1);
//Special case: the first and second elements of the last line are coincident, so calculation is not required,
//Exclude the last row here: NumRows - row > 1
if (numRows - row > 1 && i < len) {
resStr[k++] = str[i];
}
//The second and third element subscripts in each row have the following relationship: J = I + 2row
//Special case: the first and second elements of the last line coincide, so the third element and the subscript of the first element conform to this relationship
i += 2 * row;
if (row > 0 && i < len)
{
resStr[k++] = str[i];
}
}
}
resStr[k] = '\0';
return resStr;
}
int main(void)
{
char *str = "nfaskofncklasnfclas";
int numRows = 5;
char *retStr = convert(str, numRows);
int i = 0;
while (retStr[i] != '\0')
{
printf("%c", retStr[i]);
i++;
}
printf("\n");
}
'; return resStr; } int main(void) { char *str = "nfaskofncklasnfclas"; int numRows = 5; char *retStr = convert(str, numRows); int i = 0; while (retStr[i] != '
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *convert(char *str, int numRows)
{
int k = 0;
int len = strlen(str);
char *resStr = (char *)malloc((len + 1) * sizeof(char));
//Special case: if the number of lines is less than 2, or the length of the string is not greater than the number of lines, the string is returned directly
//Because there is only one row or one column at this time
if (numRows < 2 || len <= numRows)
{
return str;
}
for (int row = 0; row < numRows; row++)
{
//Stores the first character of each line specified by NumRows into the return character
resStr[k++] = str[row];
for (int i = row; i < len;)
{
//In each NumRows line, find the corresponding element subscript in the original string
//The second element subscript in each row has the following relationship with the first element subscript: J = I + 2numrows - 2 - 2row
//Special case: the first and second elements of the last line coincide, so the following expression is: I + = 0 - > I, that is, I does not change
i += 2 * (numRows - row - 1);
//Special case: the first and second elements of the last line are coincident, so calculation is not required,
//Exclude the last row here: NumRows - row > 1
if (numRows - row > 1 && i < len) {
resStr[k++] = str[i];
}
//The second and third element subscripts in each row have the following relationship: J = I + 2row
//Special case: the first and second elements of the last line coincide, so the third element and the subscript of the first element conform to this relationship
i += 2 * row;
if (row > 0 && i < len)
{
resStr[k++] = str[i];
}
}
}
resStr[k] = '\0';
return resStr;
}
int main(void)
{
char *str = "nfaskofncklasnfclas";
int numRows = 5;
char *retStr = convert(str, numRows);
int i = 0;
while (retStr[i] != '\0')
{
printf("%c", retStr[i]);
i++;
}
printf("\n");
}
') { printf("%c", retStr[i]); i++; } printf("\n"); }

The time complexity of this method is O (n), and N is the string length. Although the loop is nested, the number of final loops is n, and all n characters are placed in the new array.
The space complexity is O (n), and N is the string length.

Method III

This is also the official method, which is very similar to method 2. BothTraversalZigzag string.
By observing the zigzag string, the following properties can be obtained:

  • Row is the subscript of the horizontally looped row, 0 = < row < NumRows
  • The subscript of the first line element is K (2numrows – 2) + row, and K is an even number, starting from 0
  • The subscript of the middle row element is K (2numrows – 2) + row, (K + 1) (2numrows – 2) – row, and K is an integer starting from 0.
  • The subscript of the last line element is K (2numrows – 2) + row, and K is an even number, starting from 0

According to the above properties, it is easy to obtain the final string by performing a horizontal loop.

Execution time: 0 ms, beating 100.00% of users in all C submissions
Memory consumption: 6.5 MB, beating 58.46% of users in all C submissions

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *convert (char *str, int numRows) {
  int len = strlen(str);
  //Non zigzag conditions must be excluded
  if (numRows < 2)
  {
    return str;
  }
  int k = 0;
  int loopConst = 2 * numRows - 2;
  char *retStr = (char *)malloc((len + 1) * sizeof(char));
  for (int row = 0; row < numRows; row++) {
    for (int i = 0; i + row < len; i += loopConst) {
      retStr[k++] = str[i + row];
      if (row != 0 && row != numRows - 1 && i + loopConst - row < len ) {
        retStr[k++] = str[i + loopConst - row]; 
      }
    }
  }
  retStr[k] = '
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *convert (char *str, int numRows) {
int len = strlen(str);
//Non zigzag conditions must be excluded
if (numRows < 2)
{
return str;
}
int k = 0;
int loopConst = 2 * numRows - 2;
char *retStr = (char *)malloc((len + 1) * sizeof(char));
for (int row = 0; row < numRows; row++) {
for (int i = 0; i + row < len; i += loopConst) {
retStr[k++] = str[i + row];
if (row != 0 && row != numRows - 1 && i + loopConst - row < len ) {
retStr[k++] = str[i + loopConst - row]; 
}
}
}
retStr[k] = '\0';
return retStr;
}
int main (void) {
char *str = "msd";
int numRows = 4;
char *retStr = NULL;
int i = 0;
retStr = convert(str, numRows);
while (retStr[i] != '\0')
{
printf("%c", retStr[i++]);
}
printf("\n");
}
'; return retStr; } int main (void) { char *str = "msd"; int numRows = 4; char *retStr = NULL; int i = 0; retStr = convert(str, numRows); while (retStr[i] != '
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *convert (char *str, int numRows) {
int len = strlen(str);
//Non zigzag conditions must be excluded
if (numRows < 2)
{
return str;
}
int k = 0;
int loopConst = 2 * numRows - 2;
char *retStr = (char *)malloc((len + 1) * sizeof(char));
for (int row = 0; row < numRows; row++) {
for (int i = 0; i + row < len; i += loopConst) {
retStr[k++] = str[i + row];
if (row != 0 && row != numRows - 1 && i + loopConst - row < len ) {
retStr[k++] = str[i + loopConst - row]; 
}
}
}
retStr[k] = '\0';
return retStr;
}
int main (void) {
char *str = "msd";
int numRows = 4;
char *retStr = NULL;
int i = 0;
retStr = convert(str, numRows);
while (retStr[i] != '\0')
{
printf("%c", retStr[i++]);
}
printf("\n");
}
') { printf("%c", retStr[i++]); } printf("\n"); }

Time complexity: O (n)
Space complexity: O (n)