当前位置:网站首页>剑指 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];
}
}
边栏推荐
- 程序员副业赚钱之道,实现月收入增加20K
- WeChat applet wx.writeBLECharacteristicValue Chinese character to buffer problem
- Radon 变换原理和应用
- Tensorflow 2.0 使用流程详解
- The Principle of Union Search and API Design
- Convolutional Neural Network (CNN) for Clothing Image Classification
- 详解 Hough 变换(下)圆形检测
- Convolutional Neural Network (CNN) for mnist handwritten digit recognition
- STM32F407ZG 串口通信+固定帧头帧尾传输数据帧
- 每日刷题(day01)——leetcode 53. 最大子数组和
猜你喜欢

51单片机手动自动智能窗户窗帘控制系统手动自动定时

The way for programmers to make money from a sideline business and increase their monthly income by 20K

LeetCode 292. Nim Game (Simple)

在Unity中利用代码动态更改场景中的天空盒

LeetCode 1720.解码异或后的数组(简单)

LruCache与DiskLruCache结合简单实现ImageLoader

pytorch-09.多分类问题

LeetCode 1894.找到需要补充粉笔的学生编号

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

LeetCode refers to the offer 21. Adjust the order of the array so that the odd numbers are in front of the even numbers (simple)
随机推荐
Machine Learning - Clustering - Shopping Mall Customer Clustering
程序员副业赚钱之道,实现月收入增加20K
LeetCode refers to offer 10-I. Fibonacci sequence (simple)
卷积神经网络(CNN)实现服装图像分类
序列化、编码、requests库json和data参数
ASP.NET有关于文件上传、下载、全选、删除选中重要干货(亲测有效)
Notes for Netual Network
LeetCode 1720.解码异或后的数组(简单)
51单片机AD590温度测量ADC0832运放2.73V减法电压变换
离散数学的学习记录
详解样条曲线(上)(包含贝塞尔曲线)
Likou - Number of Provinces
钴镍回收树脂的工艺原理
Pytorch配置与实战--Tips
过大数组导致爆栈的解决方法记录(堆栈)
.Net Core imports tens of millions of data to Mysql
STM32单片机手机APP蓝牙高亮RGB彩灯控制板任意颜色亮度调光
【简易笔记】PyTorch官方教程简易笔记 EP4
在Unity的Update中通过物体自身位置判断运动方向
pytorch-06. Logistic regression