< Sword offer> is programmed with JavaScript code (1-66)

Source: Internet
Author: User

The use of the bull's online system of the problem of JavaScript (V8 6.0.0)

1. Finding a two-D array

functionFind (target, array) {//Write code here     varrow = Array.Length;//Number of rows    varcol = array[0].length;//Number of columns        //start comparing from top right corner    varR = 0; varc = Col-1;  while(R <=row-1 && C >= 0) {//notice there's an equals sign here .        if(Target >Array[r][c]) {R++; }        Else if(Target <Array[r][c]) {C--; }        Else {            return true; }    }    return false;}

2. Replace spaces

function Replacespace (str) {    //  Write codehere   return str.replace (/\s +?/g, '%20 ')}

3. Print the list from tail to head

function Printlistfromtailtohead (head) {    //  Write codehere    var res = [];     while (head!=null) {        res.push (head.val);         = head.next;    }     return res.reverse ();}

4. Rebuilding the binary tree

functionReconstructbinarytree (Pre, VIN) {//Write code here   if(!pre | | pre.length = = 0) {        return; }        varTreeNode ={val:pre[0]    }     for(vari = 0; i < pre.length; i++) {        if(Vin[i] = = = Pre[0]) {treenode.left= Reconstructbinarytree (Pre.slice (1, i+1), Vin.slice (0, i)); Treenode.right= Reconstructbinarytree (Pre.slice (i+1), Vin.slice (i+1)); }    }    returnTreeNode;}

5. Using two stacks to implement a queue

var result=[]; function Push (node) {    //  Write codehere    result.push (node)}function  Pop () {    //  Write codehere    return  result.shift ()} 

6. Rotate the minimum number of the array

function Minnumberinrotatearray (rotatearray) {    //  Write codehere    rotatearray.sort (  function(b) {        if(a<b) {            return -1;        }         Else            return 1;            })     return rotatearray[0];}

7. Fibonacci Sequence (cyclic method)

 function   Fibonacci (n) { //  write code here  if  (n==0| | N==1 return      n;     var  f1 = 0;     var  F2 = 1;  for  (var  i =2;i<=n;i++ var  tmp = f1 + F2        ;        F1  = F2;    F2  = tmp;  return   tmp;}  

8. Jumping steps

functionJumpfloor (number) {//Write code here    if(number==1){        return1; }    if(number==2){        return2; }    varF1 = 1; varF2 = 2;  for(vari=2;i<number;i++){        varTMP = f1+F2; F1=F2; F2=tmp; }    returnF2;}

9. Abnormal jumping steps

function Jumpfloorii (number) {    //  Write codehere    if(number==1) {         return 1;    }     if (number==2) {        return 2;    }     Else        return 2*jumpfloorii (number-1);}

10. Rectangle Overlay

functionRectcover (number) {//Write code here    if(number<=0){        return0; }    if(number==1| | number==2){        returnNumber ; }           varF1 = 0; varF2 = 1;  for(vari=1;i<=number;i++){        varTMP = F1 +F2; F1=F2; F2=tmp; }    returnF2;}

11. Number of 1 in binary

function NumberOf1 (n) {    //  Write codehere    var count = 0;      while (n!=0) {        = n& (n-1);        Count+ +    }    return  count;}

12. Integer number of values

functionPower (base, exponent) {//Write code here    if(exponent==0){        return1; }    varresult = 1; if(exponent>0){         for(vari=1;i<=exponent;i++) {result*=Base; }        returnresult; }    if(exponent<0) {exponent=Math.Abs (exponent);  for(vari=1;i<=exponent;i++) {result*=Base; } result= 1/result;returnresult; }}

13. Adjust the array element order so that the odd digits are preceded by even numbers

function Reorderarray (array) {    //  Write codehere    var result1 = [];     var result2 = [];      for (var i=0;i<array.length;i++) {        if(array[i]%2==0) {            Result1.push (Array[i]);        }         Else {            result2.push (array[i]);        }     }     return Result2.concat (RESULT1);}

< Sword offer> is programmed with JavaScript code (1-66)

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.