Algorithm problem--recursive algorithm

Source: Internet
Author: User

Today in the project to use a recursive, unexpectedly did not find the error of the algorithm, recorded. Be free to think about ...

Here is the error code:

The function is that if the string ends in rts_mon->, the last face is truncated rts_mon->

Processing results should be:1 rts_mon-> 2 rts_mon-> 3 rts_mon-> 4 rts_mon-> 5 rts_mon-> 6 rts_mon-> 7 rts_mon->" + "\ r \ n"+"rts_mon-(Admin) >

String   str="1 rts_mon->  2 rts_mon->  3  rts_mon->   4  rts_mon->  5  Rts_mon->  6  rts_mon->  7  rts_mon->" +            "\ r \ n  "+"rts_mon-(Admin) >   rts_mon->    rts_mon->rts_mon-> ";

 Publicstring jiequ (String data) {if(Data.trim (). EndsWith ("rts_mon->") ) {Data= Data.substring (0, Data.trim (). Length ()-9). Trim (); LOG.I (TAG,"onCreate: Current data:"+temp); jiequ (temp); }Else{log.i (TAG,"onCreate: Current data else:"+data); returndata;    } return null; }

The code error is: log.i (TAG, "onCreate: Current data else:" + data ); The data printed here is correct, but runs to return data, and after that the training runs down to return null, and executes multiple return null;

After the modified code is as follows, it runs normally.

 Public string jiequ (String data) {        if (Data.trim (). EndsWith ("rts_mon->")) {             = Data.substring (0, Data.trim (). Length ()-9). Trim ();            " OnCreate: Current Data: "+ data" ;             return jiequ (data);        } Else {            "onCreate: Current Data else:" + data );             return data;        }    }

See Error codes <!---->

  Publicstring jiequ (String data) {log.i (TAG,"Function: *******"); if(Data.trim (). EndsWith ("rts_mon->") ) {Data= data.substring (0, Data.trim (). Length ()-9). Trim (); LOG.I (TAG,"OnCreate: Current Data:" +data);        jiequ (data); }Else{log.i (TAG,"OnCreate: Current Data else:" +data); returndata; } log.i (TAG,"Return: *******"); return  NULL; }

When the last face of the string is more than 6 rts_mon-> , you need to enter the jiequ() Method 7 times, log.i (TAG, "function: *******"); Print out 7 times. log.i (TAG, "return: *******"); Will walk 6 times,

LOG.I (TAG, "onCreate: Current Data else:" + data), go once.

After entering if () {}, walk through the code, will go to return null, only the last walk into else{} return data;

Algorithm problem--recursive algorithm

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.