当前位置:网站首页>剑指 Offer(第 2 版)7/5 5-8
剑指 Offer(第 2 版)7/5 5-8
2022-08-10 05:36:00 【吉良吉影__.】
1.剑指 Offer 06. 从尾到头打印链表 简单
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */
class Solution {
public int[] reversePrint(ListNode head) {
int n = 0;
ListNode temp = head;
//统计链表节点个数
while (temp != null){
n++;
temp = temp.next;
}
int[] res = new int[n];
while (head != null){
res[--n] = head.val;
head = head.next;
}
return res;
}
}
2.剑指 Offer 09. 用两个栈实现队列 简单
//两个栈实现队列
class CQueue {
Stack<Integer> stack = new Stack<>();
Stack<Integer> stack2 = new Stack<>();
public CQueue() {
}
public void appendTail(int value) {
stack.push(value);
}
public int deleteHead() {
//两个栈都空,表示此时队列中元素个数为0
if (stack.isEmpty() && stack2.isEmpty())return -1;
if (!stack2.isEmpty())return stack2.pop();//栈2有元素则直接返回
while (!stack.isEmpty())stack2.push(stack.pop());//转移
if (!stack2.isEmpty())return stack2.pop();//栈2有元素则返回
return -1;
}
}
/** * Your CQueue object will be instantiated and called as such: * CQueue obj = new CQueue(); * obj.appendTail(value); * int param_2 = obj.deleteHead(); */
3.剑指 Offer 10- I. 斐波那契数列 简单
注意,要在循环中取模,不能等到循环结束再取模,防止越界错误
斐波那契数列
class Solution {
public int fib(int n) {
if (n < 2)return n;
int a = 0;
int b = 1;
int c = 0;
for (int i = 2; i <= n; i++) {
//必须在这里提前取模
c = a + b > 1000000007 ? (a + b) % 1000000007 : a + b;
a = b;
b = c;
}
//不能前面没有取模,直接在这里取模,否则前面循环中可能越界
return c;
}
}
4.剑指 Offer 10- II. 青蛙跳台阶问题 简单
注意,要在循环中取模,不能等到循环结束再取模,防止越界错误
斐波那契数列
class Solution {
public int numWays(int n) {
if (n == 0)return 1;//第0阶有1种方案
if (n < 3)return n;
//dp[i] = 跳到第i阶台阶的方案数
int[] dp = new int[n + 1];
dp[0] = dp[1] = 1;
for (int i = 2; i <= n; i++) {
dp[i] = (dp[i - 1] + dp[i - 2]) % 1000000007;
}
return dp[n];
}
}
边栏推荐
- 新建STM32F407ZG Keil5项目
- 分享一款恋爱星座男女配对微信小程序源码
- Unity中Xml简介以及通过脚本读取Xml文本中的内容
- Convolutional Neural Network (CNN) for Clothing Image Classification
- LeetCode 1351. Counting Negative Numbers in Ordered Matrices (Simple)
- 微信小程序-小程序的宿主环境
- Convolutional Neural Network (CNN) for mnist handwritten digit recognition
- LeetCode 938. Range Sum of Binary Search Trees (Simple)
- STM32F407ZG 看门狗 IWDG & WWDG
- LeetCode 1720. Decoding XORed Arrays (Simple)
猜你喜欢

中间件-Rocktmq

LeetCode 94. Inorder Traversal of Binary Trees (Simple)

LeetCode 938. Range Sum of Binary Search Trees (Simple)

通过配置CubeMX的TIMER的PWM初始化实现硬件PWM呼吸灯闪烁

LeetCode 剑指offer 21.调整数组顺序使奇数位于偶数前面(简单)

pytorch-06. Logistic regression

深度学习阶段性报告(一)

LeetCode 1351. Counting Negative Numbers in Ordered Matrices (Simple)

详解 Hough 变换(下)圆形检测

.Net Core imports tens of millions of data to Mysql
随机推荐
ASP.NET有关于文件上传、下载、全选、删除选中重要干货(亲测有效)
Unity中采用二进制存档与读档
STM32F407ZG PWM
VTK 初步 (1) ----- 可视化管线
PyTorch之模型定义
Collection set interface
深度学习TensorFlow入门环境配置
详解 Hough 变换(下)圆形检测
Multisim软件的基本使用
以STM32F103C6TA为例通过配置CubeMX实现GPIO输出完成点灯实例
pytorch-06.逻辑斯蒂回归
I don't like my code
LeetCode 1720.解码异或后的数组(简单)
深度学习阶段性报告(一)
详解样条曲线(上)(包含贝塞尔曲线)
常用模块封装-csv文件操作封装
R语言聚类分析——代码解析
Pico设备中的截图以及视频文件通过adb命令保存到电脑中
【fiddler2】使用fiddler mock response 数据
PyTorch之CV