Interpreting three methods of reverse permutation except PHP function Strrev () _php Tutorial

Source: Internet
Author: User
We are using

For the reverse order of strings, the PHP function Strrev () has the following test code:

 
 
  1. header (' content-type:text/html; CharSet = UTF -8 ');
  2. $ Str = implode (', Range (9, 0));
  3. print ' < p><strong>before reversed: strong>'. $str. '< / p> ';
  4. print ' < p> < strong>afterreversed: < /strong>'. Strrev ($str). ' < / P > ';
  5. /*
  6. The output is as follows:
  7. Before reversed:9876543210
  8. After reversed:0123456789
  9. */


What if we don't use the built-in PHP function Strrev ()? 3 Methods (dichotomy, cyclic, recursive) were tested, but no performance tests were performed.

1. Dichotomy

 
 
  1. /**
  2. * Binary method for string reverse order
  3. * @param string $STR source strings
  4. * @return string returned in reverse order
  5. */
  6. function Reverse ($str=") {
  7. $ Len = strlen ($STR);//cannot use count or sizeof
  8. $ Mid = Floor ($len/2);
  9. For ($i=0; $i<$mid; $i + +) {
  10. $ Temp = $str [$i];
  11. $STR [$i] = $str [$len-$i-1];
  12. $str [$len-$i-1] = $temp;
  13. }
  14. return $str;
  15. }


2. Cyclic method

 
 
  1. /**
  2. * Cyclic implementation of strings in reverse order (efficiency is lower than dichotomy)
  3. * @param string $STR source strings
  4. * @return string returned in reverse order
  5. */
  6. function Reverse ($str=") {
  7. $ result = '' ;
  8. For ($i=1; $i<=strlen ($STR); $i + +) {
  9. $result . = substr ($str,-$i, 1);
  10. }
  11. return $result;
  12. }

3. Recursive method

 
 
  1. /**
  2. * Recursive implementation of strings in reverse order (inefficient)
  3. * @param string $STR source strings
  4. * @return string returned in reverse order
  5. */
  6. function Reverse ($str=") {
  7. Static $ result = '' ;
  8. /* Use the stack to understand recursive calls */
  9. if (strlen ($str) > 0) {
  10. Reverse (substr ($STR, 1));
  11. $result . = substr ($str, 0, 1);//The sentence must be placed after the previous statement
  12. }
  13. return $result;
  14. }

This is the specific use of PHP function Strrev (), as well as three other ways to implement the reverse order.


http://www.bkjia.com/PHPjc/446151.html www.bkjia.com true http://www.bkjia.com/PHPjc/446151.html techarticle we are using the reverse order of the string, PHP function Strrev () The test code is as follows: Header (' content-type:text/html; charset = Utf-8 '); $ str = implode (", Range ( 9,0)); Prin ...

  • 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.