Palindrome is a common problem. This refers to reading strings or numbers from left to right and from right to left. For example, the string is abcba, and the number is 121. String retrieval problem 1. progressive complexity analysis from the beginning to the end to the middle because only one traversal is required, therefore, the time complexity of the above two methods is O (n ). Only two int variables are used as pointers, so the space complexity is
Return strings and numbers
Preface
Palindrome is a common problem. This refers to reading strings or numbers from left to right and from right to left. For example, string: "abcba", number: 121.
String-based retrieval
1. progressive process from beginning to end to middle
The code is as follows:
/* Verify the string's retrieval problem s is a string, and n is the string's length verification direction: progressive from the beginning and end to the Middle */bool palindromic (const char * s, int n) {if (s = NULL | n <1) // return false if the initial condition is incorrect; int I, j; I = 0, j = n-1; while (I <j) {if (s [I]! = S [j]) return false; I ++; j --;} return true ;}
II. progressive process from the center to the beginning and end
The code is as follows:
/* Verify the string's retrieval problem s is a string, n is the string length verification direction: from the middle to the beginning and end */bool palindromic (const char * s, int n) {if (s = NULL | n <1) // return false if the initial condition is incorrect; int I, j; // according to the parity of n, set the start value of I and j if (n % 2) {I = (n> 1)-1; j = I + 2; // j = (n> 1) + 1 ;}else {j = n> 1; I = j-1 ;}while (I> = 0) {if (s [I]! = S [j]) return false; I --; j ++;} return true ;}
Complexity Analysis
Because you only need to traverse a string at most, the time complexity of the above two methods is O (n ). Only two int variables are used as pointers, so the space complexity is O (1 ).
Number retrieval
How can we determine the number retrieval problem? Can you convert a number into a string first, so that you can use the method previously written. The code is simple:
char s[11];palindromic(itoa(n, s, 10));
The library function char * itoa (int value, const char * s, int radix) is used );
The library function itoa () converts int-type Numeric values to a string in base radix format, and adds an ending character '\ 0' to the end of the string '. Therefore, palindromic () requires only one parameter. the length of the string can be obtained through strlen (s) in the function. At the same time, the data of the ing type can only have 10 digits, and the string terminator cannot exceed 11 digits in total. Therefore, the string array s is set to 11 characters. You can make a slight modification to the input function of the above string to judge the number.
If-121 is used as a text return, you need to slightly modify the above method. If you use itoa () to process-121, you will get "-121". This is obviously not a return string. To make the judgment successful,-value must be input for negative numbers.
Self-implementation of itoa () functions
Char * I _itoa (int value, char * s, int radix) {if (s = NULL) return NULL; int I = 0; stack
Stack; if (value <0) {value =-value; * s = '-'; I ++;} // first save the result to the stack while (value) {stack. push ('0' + value % radix); value/= radix;} while (! Stack. empty () {s [I ++] = stack. top (); stack. pop ();} // end with the '\ 0' s [I] =' \ 0'; return s ;}
When using the itoa () function, the input string array is large enough, otherwise it is easy to cross-border. Therefore, itoa () is insecure.