From 47b772015d870fec04c6030e4efada834782c7cb Mon Sep 17 00:00:00 2001 From: zhaocchen Date: Tue, 18 May 2021 15:36:36 +0800 Subject: [PATCH] feat: add javascript solution to lcci problem: No.08.01.Three Steps Problem --- lcci/08.01.Three Steps Problem/README.md | 17 +++++++++++++++++ lcci/08.01.Three Steps Problem/README_EN.md | 17 +++++++++++++++++ lcci/08.01.Three Steps Problem/Solution.js | 12 ++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 lcci/08.01.Three Steps Problem/Solution.js diff --git a/lcci/08.01.Three Steps Problem/README.md b/lcci/08.01.Three Steps Problem/README.md index fa172c0f5a7ee..970c9e60a1785 100644 --- a/lcci/08.01.Three Steps Problem/README.md +++ b/lcci/08.01.Three Steps Problem/README.md @@ -73,6 +73,23 @@ class Solution { } ``` +### **JavaScript** + +```js +/** + * @param {number} n + * @return {number} + */ + var waysToStep = function(n) { + if (n < 3) return n; + let a = 1, b = 2, c = 4; + for (let i = 3; i < n; i++) { + [a, b, c] = [b, c, (a + b + c) % 1000000007]; + } + return c; +}; +``` + ### **C++** ```cpp diff --git a/lcci/08.01.Three Steps Problem/README_EN.md b/lcci/08.01.Three Steps Problem/README_EN.md index 9ce39db976a8c..0d6ba0d858fca 100644 --- a/lcci/08.01.Three Steps Problem/README_EN.md +++ b/lcci/08.01.Three Steps Problem/README_EN.md @@ -67,6 +67,23 @@ class Solution { } ``` +### **JavaScript** + +```js +/** + * @param {number} n + * @return {number} + */ + var waysToStep = function(n) { + if (n < 3) return n; + let a = 1, b = 2, c = 4; + for (let i = 3; i < n; i++) { + [a, b, c] = [b, c, (a + b + c) % 1000000007]; + } + return c; +}; +``` + ### **C++** ```cpp diff --git a/lcci/08.01.Three Steps Problem/Solution.js b/lcci/08.01.Three Steps Problem/Solution.js new file mode 100644 index 0000000000000..eef462ef6f83a --- /dev/null +++ b/lcci/08.01.Three Steps Problem/Solution.js @@ -0,0 +1,12 @@ +/** + * @param {number} n + * @return {number} + */ + var waysToStep = function(n) { + if (n < 3) return n; + let a = 1, b = 2, c = 4; + for (let i = 3; i < n; i++) { + [a, b, c] = [b, c, (a + b + c) % 1000000007]; + } + return c; +}; \ No newline at end of file