当前位置:网站首页>牛客刷题——剑指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)
边栏推荐
- The R language uses the rollapply function in the zoo package to apply the specified function to the time series in a rolling manner and the window moves, and set the align parameter to specify that t
- R语言ggpubr包的ggline函数可视化分组折线图、add参数为mean_se和dotplot可视化不同水平均值的折线图并为折线图添加误差线(se标准误差)和点阵图、自定义palette设置颜色
- Smoothing of time series data in R language: smoothing time series data to remove noise using the dpill function and locpoly function of the KernSmooth package
- 全新荣威RX5,27寸大屏吸引人,安全、舒适一个不落
- 新“内卷”席卷科技圈,Google CEO 要求 174000 员工提高工作效率!
- 练习16-两道模拟题
- R language ggplot2 visualization: use the ggtexttable function of the ggpubr package to visualize tabular data (directly draw tabular graphs or add tabular data to images), use tbody_add_border to add
- 【SeaTunnel】从一个数据集成组件演化成企业级的服务
- node封装一个图片拼接插件
- 单词接龙 II
猜你喜欢

关于缓存数据的探讨

Do you agree with this view?Most businesses are digitizing just to ease anxiety

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

第十六章 协程

SAP 云平台上一种 Low Code Development(低代码开发)解决方案

【云原生】快出数量级的性能是怎样炼成的?就提升了亿点点

一文带你了解推荐系统常用模型及框架

leetcode 62. Unique Paths(独特的路径)

Naive Bayesian Method of Li Hang's "Statistical Learning Methods" Notes

周鸿祎称微软抄袭 360 安全模式后发文否认;英特尔CEO基辛格回应市值被AMD超越:股价下跌是咎由自取|极客头条...
随机推荐
【New Edition】DeepFakes: Creation, Detection and Influence
每日一题练习1-15
Implementation of mysql connection pool
Have you ever learned about these architecture designs and architecture knowledge systems?(Architecture book recommendation)
Linux system uninstall, install, upgrade, migrate clickHouse database
AlterNET Studio用户界面设计功能扩展
The R language uses the rollapply function in the zoo package to apply the specified function to the time series in a rolling manner and the window moves, and set the align parameter to specify that t
第十五章 多线程
第15章 泛型
要长续航还是更安全?海豹与深蓝SL03对比导购
AutoJs学习-存款计算器
迭代器失效问题
leetcode 62. Unique Paths(独特的路径)
练习-17
R语言ggpubr包的ggline函数可视化分组折线图、add参数为mean_se和dotplot可视化不同水平均值的折线图并为折线图添加误差线(se标准误差)和点阵图、自定义palette设置颜色
8月份的.NET Conf 活动 专注于 .NET MAUI
二维数组零碎知识梳理
【云原生】快出数量级的性能是怎样炼成的?就提升了亿点点
开源一夏 | GO语言框架中如何快速集成日志模块
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