未验证 提交 4c7fabb1 编写于 作者: K Keqi Huang 提交者: GitHub

Merge pull request #248 from Ctum/master

jianzhiOffer-js
function Find(target, array)
{
// write code here
const num_row = array.length;
for(let i=0; i<num_row;i++) {
if(array[i].indexOf(target) > -1) {
return true
}
}
}
module.exports = {
Find : Find
};
function replaceSpace(str)
{
// write code here
let strArr = str.split('');
return strArr.reduce((pre, next) => {
return pre + (next === ' ' ? '%20' : next)
}, '');
}
module.exports = {
replaceSpace : replaceSpace
};
/*function ListNode(x){
this.val = x;
this.next = null;
}*/
function printListFromTailToHead(head)
{
// write code here
let arr = [];
if (!head) {
return arr;
}
while(head) {
arr.unshift(head.val)
head=head.next
}
return arr;
}
module.exports = {
printListFromTailToHead : printListFromTailToHead
};
let stack1 = [];
let stack2 = [];
function push(node)
{
// write code here
stack1.push(node)
}
function pop()
{
// write code here
if(stack2.length === 0) {
while (stack1.length!==0) {
stack2.push(stack1.pop())
}
}
return stack2.pop()
}
module.exports = {
push : push,
pop : pop
};
function minNumberInRotateArray(rotateArray)
{
// write code here
if (rotateArray.length === 0) {
return 0;
} else {
return Math.min.apply(null, rotateArray)
}
}
module.exports = {
minNumberInRotateArray : minNumberInRotateArray
};
function Fibonacci(n)
{
// write code here
if (n===0 || n===1) {
return n;
}
let n1 = 0, n2=1, sum=1;
for(let i=2;i<=n;i++) {
sum = n1+n2;
n1=n2;
n2=sum;
}
return sum;
}
module.exports = {
Fibonacci : Fibonacci
};
/**
*
* @param {number} number
* 思路: n级台阶只能由第n-1和第n-2级台阶跳上来,得到动态规划方程
*/
function jumpFloor(number)
{
// write code here
if(number===0) return 0;
if(number==1)return 1;
if(number===2) return 2;
let a=1, b=2, sum=0;
for(let i=3; i<= number; i++) {
sum=a+b;
a=b;
b=sum;
}
return sum;
}
module.exports = {
jumpFloor : jumpFloor
};
function sumArr(arr) {
return arr.reduce((pre, next) => pre+next)
}
function jumpFloorII(number)
{
// write code here
if(number===0) return 0;
if(number==1)return 1;
if(number===2) return 2;
let ans = [1,2];
for(let i=3;i<=number;i++) {
// 额外加一是可以直接跳到第n级台阶
ans.push(sumArr(ans)+1);
}
return ans[ans.length -1];
}
module.exports = {
jumpFloorII : jumpFloorII
};
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册