当前位置:网站首页>牛客刷题——剑指offer(第三期)
牛客刷题——剑指offer(第三期)
2022-08-02 09:48:00 【学好c语言的小王同学】
前言
作者简介:友友们大家好,我是你们的小王同学
个人主页:小王同学
系列专栏:牛客刷题专栏
推荐一款非常火的面试、刷题神器牛客网
觉得小王写的不错的话 麻烦动动小手 点赞 收藏 评论
今天给大家带来的刷题系列是:
剑指offer 链接: 剑指offer



里面有非常多的题库 跟面经知识 真的非常良心了!!
目录
JZ9 用两个栈实现队列
题目描述

解题思路
借助栈的先进后出规则模拟实现队列的先进先出
1、当插入时,直接插入 stack1
2、当弹出时,当 stack2 不为空,弹出 stack2 栈顶元素,如果 stack2 为空,将 stack1 中的全部数逐个出栈入栈 stack2,再弹出 stack2 栈顶元素
代码详解
import java.util.*;
import java.util.Stack;
public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.push(node);
}
public int pop() {
if(stack1.empty()&&stack2.empty()){
return -1;
}
if(stack2.empty()){
while(!stack1.empty()){
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
}
过辣 这道题 思路出来了 就好写了!~

JZ14 剪绳子
题目描述

解题思路
- 当n<3时 直接计算即可
- 用dp数组表示长度为i的绳子可以被剪出来的最大乘积,初始化前面4个的规律很好推出
- 遍历每个长度,对于每个长度的最大乘积,可以遍历从1到i的每个固定一段,按照上述公式求的最大值。
- 数组最后一个就是最大值
代码详解
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param n int整型
* @return int整型
*/
public int cutRope (int n) {
if(n<=3){
return n-1;
}
int dp[]=new int[n+1]; //dp[i]表示长度为i的绳子剪出来的最大乘积
//初始化 dp 前四段长度的乘积
dp[1]=1;
dp[2]=2;
dp[3]=3;
dp[4]=4;
//遍历后面的长度
for(int i=5;i<=n;i++){
for(int j=1;j<i;j++){
dp[i]=Math.max(dp[i],j*dp[i-j]);
}
}
return dp[n];
// write code here
}
} 

JZ18 删除链表的节点
题目描述

解题思路
既然是整个链表元素都不相同,我们要删除给定的一个元素,那我们首先肯定要找到这个元素,然后考虑删除它。
删除一个链表节点,肯定是断掉它的前一个节点指向它的指针,然后指向它的后一个节点,即越过了需要删除的这个节点。
代码详解
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* public ListNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @param val int整型
* @return ListNode类
*/
public ListNode deleteNode (ListNode head, int val) {
//加入一个虚拟头结点
ListNode res= new ListNode(0);
res.next=head;
//前序节点
ListNode pre= res;
ListNode cur =head;
while(cur!=null){
//如果找到当前节点
if(cur.val==val){
//断开连接
pre.next=cur.next;
break;
}
pre=cur;
cur=cur.next;
}
//返回删除后链表的头节点
return res.next;
// write code here
}
}

牛客是一款不论是面试 还是刷题 都是非常有用的 还等什么注册起来吧! 全部动态_牛客网 (nowcoder.com)
边栏推荐
- mysql进阶(二十一)删除表数据与数据库四大特性
- 开源一夏 | GO语言框架中如何快速集成日志模块
- 迭代器失效问题
- ConvNeXt论文及实现
- 干货|如何在海量文件系统中选择合适自己的文件系统
- R语言时间序列数据的平滑:使用KernSmooth包的dpill函数和locpoly函数对时间序列数据进行平滑以消除噪声
- R语言时间序列数据算术运算:使用log函数将时间序列数据的数值对数化、使用diff函数计算对数化后的时间序列数据的逐次差分(计算价格的对数差分)
- Two-dimensional array piecemeal knowledge sorting
- In the whole development of chi V853 board tried to compile QT test
- List-based queuing and calling system
猜你喜欢

软件测试H模型

Facebook自动化数据分析方案,广告投放省心省力

Re22:读论文 HetSANN An Attention-based Graph Neural Network for Heterogeneous Structural Learning

Supervised learning of Li Hang's "Statistical Learning Methods" Notes

The perceptron perceptron of Li Hang's "Statistical Learning Methods" notes

Use compilation to realize special effects of love

php组件漏洞

瑞萨RZ/G2L处理器详细测评

第十五章 多线程

转转反爬攻防战
随机推荐
记某社区问答
The ggbarplot function of the R language ggpubr package visualizes the grouped histogram, sets the add parameter to mean_se to visualize the histogram of the mean values of different levels and adds
CFdiv2-The Number of Imposters-(两种点集图上染色问题总结)
system_error错误处理库学习
[Concurrent programming] - Thread pool uses DiscardOldestPolicy strategy, DiscardPolicy strategy
R language ggplot2 visualization: use the ggbarplot function of the ggpubr package to visualize the horizontal column chart (bar chart), use the orientation parameter to set the column chart to be tra
用了TCP协议,就一定不会丢包嘛?
npm ERR! 400 Bad Request - PUT xxx - Cannot publish over previously published version “1.0.0“.
Have you ever learned about these architecture designs and architecture knowledge systems?(Architecture book recommendation)
The heavyweights are coming!Spoilers for the highlights of the Alibaba Cloud Life Science and Intelligent Computing Summit
R语言使用ggpubr包的ggtexttable函数可视化表格数据(直接绘制表格图或者在图像中添加表格数据)、设置theme主题参数自定义表格中表头内容的填充色(使用colnames.style参数)
一文带你了解推荐系统常用模型及框架
干货|如何在海量文件系统中选择合适自己的文件系统
2022.7.25-7.31 AI行业周刊(第108期):值钱比赚钱更重要
ConvNeXt论文及实现
R语言ggplot2可视化:使用ggpubr包的ggbarplot函数可视化水平柱状图(条形图)、使用orientation参数设置柱状图转置为条形图
php组件漏洞
function call to print lua internal structure
Use compilation to realize special effects of love
【OpenCV】-霍夫变换