Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 004d6a1

Browse files
committed
add two drafts
1 parent 82ba2a1 commit 004d6a1

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

301-400/344-reverse-string.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var reverseString = function (s) {
2+
let left = 0, right = s.length - 1;
3+
4+
while (left < right) {
5+
const temp = s[left];
6+
s[left] = s[right];
7+
s[right] = temp;
8+
9+
left++;
10+
right--;
11+
}
12+
};

501-600/509-fibonacci-number.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
3+
// Recursive solution
4+
var fib = function (N) {
5+
if (N === 0 || N === 1) return N;
6+
7+
return fib(N - 1) + fib(N - 2);
8+
9+
};
10+
11+
// Iterative solution
12+
var fib = function (N) {
13+
if (N === 0 || N === 1) return N;
14+
15+
let first = 0, second = 1;
16+
let sum = first + second;
17+
18+
for (let i = 2; i < N; i++) {
19+
first = second;
20+
second = sum;
21+
22+
sum = first + second;
23+
}
24+
25+
return sum
26+
};

0 commit comments

Comments
 (0)