---questions about algorithm steps _javascript skill in JS face test

Source: Internet
Author: User

There are 100 steps, can be 1 steps across 2 steps, then a number of ways to go;

Telephone interview today. Encountered an algorithm problem, and then an instant face Meng force;

Then wit me, think smart of the thought if a person every time walk 1 steps, then the most steps, 2 steps at least step, and then obviously digress ... Fortunately, the other party in time to interrupt me ... or I guess I'm going to die on this thing . Go all the way to the black.

And then came home. Take my Mac, then think quietly, and finally write it.

var stairs = new Step ();
function Step () {
  this.n1=1;
  this.n2=2;
  this.total=100;
  This.getfunction = getfunction;
}
function GetFunction () {for
    (i=2;i<this.total;i++) {
      res = this.n1 + this.n2;
      This.n1 = this.n2;
      THIS.N2 = res;
    }
  return res;
}
var totalstairs = Stairs.getfunction ();
Alert (Totalstairs)

It's only 1 times. can only walk 1 steps .... Just 1 kinds .

When it's only 2, you can 1+1| | 2... 2 kinds

3 lattice time, 1+1+1| | 2+1| | 1+2 ... 3 kinds

4 Lattice Time 1+1+1+1| | 2+2| | 2+1+1| | 1+1+2| | 1+2+1 ... 5 kinds

sn = s (n-1) +s (n-2)

Fibonacci algorithm ... And then you can use

for (i=2;i<this.total;i++) {
   res = this.n1 + this.n2;
   This.n1 = this.n2;
   THIS.N2 = res;
}

Perhaps I am not particularly good at the algorithm ~ If there are objections welcome correction

The above is small series for everyone to bring the JS face questions---On the algorithm steps of the problem of all content, I hope that we support cloud Habitat Community ~

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.