Problem Description: Implement flip string function reverse;
If no additional space is allocated, the string is flipped directly in place;
//Implementing string Inversion functionReverse (str) {varStr_array = Str.split ("); varLow = 0, height = str_array.length-1; while(Low <height) { varTMP =Str_array[low]; Str_array[low]=Str_array[height]; Str_array[height]=tmp; Low++; Height--; } returnstr_array.join ();} varstr= "Hello, World"; Console.log (reverse (str));
When you do not request memory, it is difficult to flip the string directly in place, especially if the recursive operation is involved.
function reverse_2 (str) { ifreturn str; return Str.charat (str.length-1) + reverse_2 (str.substr (0, str.length-1));} var str= "Hello, World"; Console.log (reverse_2 (str));
Recursive operation, mainly to consider the cutoff conditions.
javascript--Interview--algorithm--string-2