当前位置:网站首页>【近日力扣】斐波那契数列+用两个栈实现队列+从尾到头打印链表
【近日力扣】斐波那契数列+用两个栈实现队列+从尾到头打印链表
2022-04-22 04:05:00 【foolBirdd】
斐波那契数列(简单)
- 动态规划
var fib = function(n) {
if (n === 0 || n === 1) return n
let n1 = 0, n2 = 1, res = 0
for (let i = 1; i < n; i++) {
res = (n1 + n2) % 1000000007
n1 = n2
n2 = res
}
return res
};
用两个栈实现队列(简单)
JS 的栈使用起来非常方便,因为其用数组即可实现栈,数组的方法很多
- 首先,入栈也就相当于入队了,push 即可实现。但如何将一个栈中先进去的元素(底部)移到顶部删除(出队)呢?栈,先进后出;队列,先进先出。可以想到将一个栈中的元素再入到另一个栈中,所有元素排序则倒过来了,底部的元素到了顶部,然后再出栈即实现出队
var CQueue = function() {
this.stack1 = []
this.stack2 = []
};
/**
* @param {number} value
* @return {void}
*/
CQueue.prototype.appendTail = function(value) {
this.stack1.push(value)
};
/**
* @return {number}
*/
CQueue.prototype.deleteHead = function() {
// 此处用了解构赋值,为何不直接使用 this,说实话我也没想通,且那样无法通过
let [outStack, inStack] = [this.stack2, this.stack1]
if (!outStack.length) {
while(inStack.length) outStack.push(inStack.pop())
}
return outStack.pop() || -1;
};
从尾到头打印链表(简单)
思考:从尾到头打印,很像栈的方式,先遍历的头部元素最后打印
- 循环
var reversePrint = function(head) {
let arr = []
while (head) {
arr.push(head.val)
head = head.next
}
return arr.reverse()
};
- 递归
var reversePrint = function(head) {
let arr = [] // arr 在 recursion 外部定义
let recursion = (head) => {
if (!head) return
recursion(head.next)
arr.push(head.val)
}
recursion(head)
return arr // 此处不需要 reverse,为什么?
};
版权声明
本文为[foolBirdd]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_43510829/article/details/119488801
边栏推荐
- [server data recovery] a data recovery case in which multiple hard disks of server raid6 are offline successively
- What is the future direction of GPU?
- 24 pictures to conquer border image
- DO447Ansible Tower导航
- matplotlib画3dbox
- Ncurses installation package and PKG config information
- 安装班和免安装版
- Redis database
- Smart Life - how convenient is it to schedule smart home devices?
- Implement joint type verification of parameters in nest
猜你喜欢

go gin框架配置日志输出到文件

AI video cloud vs narrowband HD, who is the darling of the video era

The WiFi button of win11 is missing and cannot be connected to the Internet
JDBC使用预编译执行DQL语句输出都是占位符内容,这是为什么呢?

sumo-绕圈行驶

The core of improving data utilization efficiency in the transportation industry is to do a good job in data exchange and sharing

Leetcode1615. Maximum network rank (medium)

Virtual DOM

交通行业提升数据利用效率的核心是做好数据交换与共享

JS dynamically generates a table and adds a scroll bar
随机推荐
JDBC uses precompiling to execute DQL statements, and the output is placeholder content. Why?
The core of improving data utilization efficiency in the transportation industry is to do a good job in data exchange and sharing
2022-04-21:给定一个包含 [0,n) 中不重复整数的黑名单 blacklist, 写一个函数从 [0, n) 中返回一个不在 blacklist 中的随机整数, 对它进行优化使其尽量少调用系
oracle连接数据库增删改查
How do programmers ensure that software is free of bugs?
JDBC使用预编译执行DQL语句输出都是占位符内容,这是为什么呢?
Zhongtian steel holds "golden cup" in 18 products
专家有料 | 张祖优:腾讯云DevSecOps实践与开源治理探索
win11系统开机后没有输入法——解决方法亲测有效
Solve themedata in fluent Primarycolor does not take effect in components such as AppBar
Mongodb - $match operation of aggregation pipeline
Ronglian Qimo empowers enterprises with intelligent services and redefines the value of customer service
Sumo course - public transport course
Data cleaning chapter05 | data grouping and data imbalance
Insert a number into the ordered array (Bubble + rand function)
Web page performance optimization
数据挖掘系列(2)_Excel的数据挖掘插件连接SQL Server
机器学习系列(5)_特征工程03碳排放小案例
DR/AP4029 outdoor IPQ-4019/4029 Outdoor directional-antennas
Where is the whole house intelligence that Huawei, Haier Zhijia and Xiaomi are all doing?