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



里面有非常多的题库 跟面经知识 真的非常良心了!!
目录
JZ12 矩阵中的路径🥣
矩阵中的路径_牛客题霸_牛客网 (nowcoder.com)
题目描述🥣


解题思路🥣
这道题是一道标准的dfs例题
通用板子如下!
if 满足结束条件:
result.add(路径)
return
for 选择 in 选择列表:
做选择
backtrack(路径, 选择列表)
撤销选择
因为是一个二维矩阵 首先两层for循环 遍历全部的点
接着就是最重要的dfs函数
一个点向周围四个地方递归
递归之后再把当前的坐标复原
代码详解🥣
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param matrix char字符型二维数组
* @param word string字符串
* @return bool布尔型
*/
public boolean hasPath (char[][] matrix, String word) {
char worlds[]= word.toCharArray();
for(int i=0;i<matrix.length;i++){
for( int j=0;j<matrix[0].length;j++){
if(dfs(matrix,worlds,i,j,0))
return true;
}
}
return false;
// write code here
}
boolean dfs(char[][] matrix,char []world , int i,int j,int index){
//处理边界问题 index表示的是查找到字符串word的第几个字符,
if(i>=matrix.length||i<0||j>=matrix[0].length||j<0||matrix[i][j]!=world[index])
return false;
//如果world 每个字符串都找到了 直接return true
if(index==world.length-1)
return true;
char temp=matrix[i][j]; //把走过的坐标标记下来 最后输出
//修改当前坐标的值
matrix[i][j]=' ';
//从当前坐标的上下左右四个方向去寻找 递归
boolean res=dfs(matrix,world,i+1,j,index+1)|| //向右递归
dfs(matrix,world,i-1,j,index+1)||//向左递归
dfs(matrix,world,i,j-1,index+1)||//向下递归
dfs(matrix,world,i,j+1,index+1);//向上递归
//递归后再把当前坐标复原
matrix[i][j]=temp;
return res;
} 
过辣~
JZ16 数值的整数次方🥣
数值的整数次方_牛客题霸_牛客网 (nowcoder.com)
题目描述🥣

解题思路🥣
- 先处理次方数为负数的情况,将底数化为分数解决。
- 遍历次方数的次数,不断累乘底数。
代码详解🥣
import java.util.*;
public class Solution {
public double Power(double base, int exponent) {
//先处理次方数是负数的情况
if(exponent<0){
base=1/base;
exponent=-exponent;
}
double ret=1.0;
for(int i=0;i<exponent;i++){
ret*=base;
}
return ret;
}
} 
一声清脆的声音 过过辣
JZ40 最小的K个数🥣
最小的K个数_牛客题霸_牛客网 (nowcoder.com)
题目描述🥣

解题思路🥣
这道题直接用java 自带的排序函数 Arrays.sort()
代码详解🥣
import java.util.*;
public class Solution {
public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
ArrayList<Integer> list =new ArrayList<>();
//特判k=0时 直接返回
if(k==0) return list;
Arrays.sort(input);//将数组从小到大升序排列 去最前面k个即可
for(int i=0;i<k;i++){
list.add(input[i]);
}
return list;
}
}
牛客是一款不论是面试 还是刷题 都是非常有用的 还等什么注册起来 跟着王同学一起刷题 进大厂吧! 牛客刷题库
边栏推荐
- What is the difference between College coder and 985 programmer?
- Kingbasees SQL language reference manual of Jincang database (8. Function (9))
- Chapter 4 Executing Commands
- 比你老师详细系列————结构体
- 网线水晶头接法图解8根顺序
- 【Delphi】制作控件面板安装图标的简单方法(译)
- Redis transaction - detailed implementation process of seckill case simulation
- 编译构建工具-bazel
- Chrome selenium uses the default profile without emptying it every time
- redis伪集群一键部署脚本---亲测可用
猜你喜欢

C# 客户端程序调用外部程序的3种实现方法

Chapter 4: runtime data area - shared space

SQLZOO——SELECT from WORLD Tutorial

李宏毅机器学习2022-HW1

Ultra Fast Deep Lane Detection with Hybrid Anchor Driven Ordinal Classification论文解读
![[Internet of vehicles prototype system II] database + application layer protocol design](/img/6f/a4a6b2b70d9a873d6e66d1a4efcb9a.png)
[Internet of vehicles prototype system II] database + application layer protocol design

Chrome selenium uses the default profile without emptying it every time

美团8年经验之谈,测试工程师如何进阶(自动化、性能、测开)

数据湖:Delta Lake介绍

12 个适合做外包项目的开源后台管理系统
随机推荐
Self operation and maintenance: a new sample of it operation and maintenance services in Colleges and Universities
selenium JD爬虫
Accessory mode
MapReduce进阶
0基础转行软件测试,月薪6000和11000的必备技能,截然不同...
《Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks》论文阅读
Chapter2 Standard Output
[Internet of vehicles prototype system II] database + application layer protocol design
8.< tag-动态规划和LCS问题>lt.300. 最长递增子序列 + lt.674. 最长连续递增序列
无套路、无陷阱、无广告 | 这个免费的即时通讯软件确定不用吗?
2022/7/22
Practice of RTC performance automation tool in memory optimization scenario
No routines, no traps, no advertisements | are you sure you don't need this free instant messaging software?
MySQL查询优化-详解
toco生成tflite模型
PyQt5_QListWidget分页多选控件
Unityc realizes the conversion of Chinese characters to Pinyin - using Microsoft chspinyinconv Library
【Warning】YOLOV5训练时的ignoring corrupt image/label: [Errno 2].....,无法全部训练数据集,快速带你解决它
Hololens third perspective development [nanny level tutorial] [stepping on the pit record]
第一章概述-------第一节--1.2互联网概述