javascript--Interview--algorithm--string-2

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.