Baidu's two-face algorithm analysis: complement the full bracket sequence

Source: Internet
Author: User

Algorithm title: Complete parenthesis Sequence

Baidu encountered a problem on the two side

probably means that.

Given a sequence of brackets, you can add the bracket character before and after the sequence to complete it ...

At that time did not think of the solution, and then cool, and then specifically to engage in this problem, finally fix

The idea in the note is written in more detail, here no longer repeat (using a similar stack of ideas)

<?php/*** String to array * @param $strstring input Strings * @returnarray of results after array conversion */functionStrtoarray($str){//cast to String  $str=(String)$str;  $arr=[];  //Calculate length, consider Chinese  $len=Mb_strlen($str);  //Loop intercept, put into the array   for ($i=0; $i<$len;++$i){$arr[]=Mb_substr($str, $i, 1);}return $arr;}/*** Determine if parentheses have been matched * @param $strsequence of parentheses for string input * @returnArray Returns the result [bool, followed by a simplified bracket] */functionBracketstest($str){$ls= Strtoarray($str);  $sp=0; //Stackpointer think of the array as a stack, only at the end of the stack  if (strlen($str)==0){return [true, []];}Else if (strlen($str)==1){return [false, $ls];} while ($sp+1<Count($ls)){//If the end of the stack character matches the next character in the stack    if ($ls[$sp]=='['&&$ls[$sp+1]==']'){//Delete these two characters      Array_splice($ls, $sp, 2);      if ($sp>0){//stack tail pointer forward 1 bits--$sp;}    }Else{//into the stack++$sp;}  }if (Count($ls)==0){return [true, []];}Else{Echo implode($ls), "\ n";    return [false, $ls];}}/*** Complete parenthesis sequence * @param $strstring brace sequence to be fully complement * @returnstring */functionCompletingbrackets($str){//first judgment, get judgment result and simplified result  $testRes= Bracketstest($str);  if ($testRes[0]){return $str;}///left and right strings to be fully complement  $lC=$rC='';  foreach ($testRes[1]  as $b){if ($b=='['){$rC.=']';}Else{$lC.='[';}  }//Flattening  return $lC.$str.$rC;}EchoCompletingbrackets('] [][');

At that time two side of the time did not think of the solution, and then calmly analyze it is not too difficult

My article warehouse: GitHub address Https://github.com/JeffreyWxj/MarkdownNote
This address: https://github.com/JeffreyWxj/MarkdownNote/blob/master/Coding%E7%AC%94%E8%AE%B0/%E7%AE%97%E6%B3%95%EF%BC% 9a%e6%8b%ac%e5%8f%b7%e5%ba%8f%e5%88%97%e8%a1%a5%e5%85%a8.md

Reprint please indicate the source

Baidu's two-face algorithm analysis: complement the full bracket sequence

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.