当前位置:网站首页>牛客刷题——剑指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)
边栏推荐
- 软件测试之发现和解决bug
- node封装一个图片拼接插件
- 软件测试H模型
- AutoJs学习-实现科赫雪花
- yolov7 innovation point
- 日元疲软令游戏机在日本变身“理财产品”:黄牛大赚
- 百战RHCE(第四十七战:运维工程师必会技-Ansible学习2-Ansible安装配置练习环境)
- R language ggplot2 visualization: use the ggbarplot function of the ggpubr package to visualize the stacked bar plot, the lab.pos parameter specifies the position of the numerical label of the bar cha
- 2022.7.25-7.31 AI行业周刊(第108期):值钱比赚钱更重要
- RPA助你玩转抖音,开启电商运营新引擎
猜你喜欢
STL中list实现
Long battery life or safer?Seal and dark blue SL03 comparison shopping guide
一款优秀的中文识别库——ocr
HikariCP database connection pool, too fast!
Use the scrapy to climb to save data to mysql to prevent repetition
It's time for bank data people who are driven crazy by reporting requirements to give up using Excel for reporting
Re23:读论文 How Does NLP Benefit Legal System: A Summary of Legal Artificial Intelligence
【New Edition】DeepFakes: Creation, Detection and Influence
瑞萨RZ/G2L处理器详细测评
QT专题:自定义部件
随机推荐
瑞萨RZ/G2L处理器详细测评
使用scrapy 把爬到的数据保存到mysql 防止重复
剑指offer专项突击版第17天
AutoJs学习-存款计算器
The 17th day of the special assault version of the sword offer
STL中list实现
Facebook's automated data analysis solution saves worry and effort in advertising
R语言ggplot2可视化:基于aes函数中的fill参数和shape参数自定义绘制分组折线图并添加数据点(散点)、使用theme函数的legend.position函数配置图例到图像右侧
Spearman's correlation coefficient
曲折的tensorflow安装过程(Tensorflow 安装问题的解决)
npm ERR! 400 Bad Request - PUT xxx - Cannot publish over previously published version “1.0.0“.
重磅大咖来袭!阿里云生命科学与智能计算峰会精彩内容剧透
Navicat连接MySQL时弹出:1045:Access denied for user ‘root’@’localhost’
李航《统计学习方法》笔记之监督学习Supervised learning
要长续航还是更安全?海豹与深蓝SL03对比导购
matlab-day02
练习-17
一款优秀的中文识别库——ocr
转转反爬攻防战
Implementation of mysql connection pool