An example of JavaScript recursion to implement reverse array string, javascript Recursion

Source: Internet
Author: User

An example of JavaScript recursion to implement reverse array string, javascript Recursion

<! DOCTYPE html> 

Use JavaScript to sort string arrays.

/**
* Sort
@ Param opt: String Array to be sorted
*/
Function sortString (opt ){
If (! Opt) return;
Var result = [], nresult = [],
TempNum = 0, tempTNum = 0, nk = 0;
For (var I = 0; I <opt. length; I ++ ){
Var syt = opt [I], num = 0;
For (var j = 0; j <syt. length; j ++ ){
Var k = 0, tempnum = 0,
Str = syt. substr (j, j + 1 );
While (syt. indexOf (str, k )! =-1 ){
Tempnum ++;
K = syt. indexOf (str, k) + 1;
}

If (tempnum> num ){
Num = tempnum
}
}
Result [result. length] = num;
}
For (var n = 0; n <result. length; n ++ ){
TempTNum = result [n];
For (var m = 0; m <result. length; m ++ ){
If (tempTNum <result [m]) {
TempTNum = result [m];
Nk = m;
}
}
Nresult [nresult. length] = opt [nk];
Result [nk] =-1;
}
Return nresult;
}

In fact, this is an algorithm, but the above Code does not take efficiency into account, but only achieves the effect

C ++ compiles a recursive function to output a string in reverse order.

Char str [] = "backward output ";

Void rec_print (char * str)
{
If (0 = str | 0 = str_len (str) return;
Printf ("% c", str [strlen (str)-1]);
Str [strlen (str)-1] = 0;
Rec_print (str );
}

Void rec_print_no_pointer (std:; string & str)
{
If (! Str. size () return;
Std: cout <str. substr (str. size ()-1, 1 );
Str = str. substr (0, str. size ()-1 );
Rec_print_no_pointer (str );
}

Related Article

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.