当前位置:网站首页>剑指 Offer(第 2 版)7/6 9-13
剑指 Offer(第 2 版)7/6 9-13
2022-08-10 05:36:00 【吉良吉影__.】
1.剑指 Offer 11. 旋转数组的最小数字 简单
只需遍历一遍即可
当出现numbers[i] < numbers[i - 1]
时找打答案
class Solution {
public int minArray(int[] numbers) {
int n = numbers.length;
for (int i = 1; i < n; i++) {
if (numbers[i] < numbers[i - 1])return numbers[i];
}
//未反转返回第一个最小元素
return numbers[0];
}
}
2.剑指 Offer 12. 矩阵中的路径 中等
回溯
记得要细心
class Solution {
public boolean exist(char[][] board, String word) {
boolean[][] isVisited = new boolean[board.length][board[0].length];
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (exist(board,word,0,i,j,isVisited))return true;
}
}
return false;
}
/** * * @param board * @param word * @param i 为待寻找第i个字母 * @param x 起始的x坐标 * @param y 起始的y坐标 * @return */
public boolean exist(char[][] board, String word, int i, int x,int y, boolean[][] isVisited ){
if (i == word.length())return true;
if (x < 0 || x >= board.length || y < 0 || y >= board[0].length)return false;
if (isVisited[x][y])return false;
if (board[x][y] != word.charAt(i))return false;
isVisited[x][y] = true;
//寻找顺序为 右 下 左 上
//右
if (exist(board,word,i + 1,x,y + 1,isVisited))return true;
//下
if (exist(board,word,i + 1,x + 1,y,isVisited))return true;
//左
if (exist(board,word,i + 1,x,y - 1,isVisited))return true;
//上
if (exist(board,word,i + 1,x - 1,y,isVisited))return true;
isVisited[x][y] = false;
return false;
}
}
3.剑指 Offer 13. 机器人的运动范围 中等
递归
class Solution {
int ans;
public int movingCount(int m, int n, int k) {
boolean[][] dp = new boolean[m][n];//记录格子是否被访问过
move(m,n,k,0,0,dp);
return ans;
}
public void move(int m, int n, int k,int x,int y,boolean[][] dp){
if (x < 0 || x >= m || y < 0 || y >= n)return;
if (dp[x][y])return;
//判断该格子是否可以访问
String xstr = x + "";
String ystr = y + "";
int count = 0;
for (int i = 0; i < xstr.length(); i++) {
count += Integer.parseInt(xstr.charAt(i) + "");
}
for (int i = 0; i < ystr.length(); i++) {
count += Integer.parseInt(ystr.charAt(i) + "");
}
if (count > k)return;
dp[x][y] = true;//可以访问本节点
ans++;
//左 右 上 下
move(m,n,k,x,y - 1,dp);
move(m,n,k,x,y + 1,dp);
move(m,n,k,x - 1,y,dp);
move(m,n,k,x + 1,y,dp);
}
}
4.剑指 Offer 14- I. 剪绳子 中等
此题数据范围较小可以使用动态规划
class Solution {
/* 动态规划,枚举最后一段绳子的长度 dp[i] = Math.max(j * dp[i - j],j * (i - j)) */
public int cuttingRope(int n) {
//dp[i] = 长度为i的绳子,切割成多份绳子乘积的最大值
int[] dp = new int[n + 1];
dp[1] = 1;
dp[2] = 1;
for (int i = 3; i <= n; i++) {
//枚举最后一段绳子的长度
int max = Integer.MIN_VALUE;
for (int j = 1; j < i; j++) {
//绳子的切割方式有两种
max = Math.max(max,j * dp[i - j]);//切割成 > 2段
max = Math.max(max,j * (i - j));//切割成 = 2段
}
dp[i] = max;
}
return dp[n];
}
}
5.剑指 Offer 14- II. 剪绳子 II 中等
此题和力扣中的整数拆分问题几乎一模一样,可以去参考整数拆分的思想,本题额外解决了如何
处理大数取余的问题.
数学知识解决,暂时不要求掌握
方法一:数学思想 + 循环大数取余
//数学相关,了解即可 不要求掌握此解
class Solution {
/* 大数求余公式:(xy)⊙p=[(x⊙p)(y⊙p)]⊙p ⊙p : 对p取余 可以采用循环求余的方式 */
public int cuttingRope(int n) {
//当 n < 4时单独考虑
if (n < 4) return n - 1;
//当 n >= 4时
int x = n / 3;
int y = n % 3;
int p = 1000000007;
long ret = 1;
//这里只计算到 3 的 (x - 1)次方
//保证每轮结束后都不超过int的最大值,但是不保证ret * 3小于int最大值
//所以这里ret的类型为long
for (int i = 1; i < x; i++) {
ret = ret * 3 % p;
}
if (y == 0) return (int) (ret * 3 % p);
//余数为1则拆出两个2,其余为3
else if (y == 1) return (int) (ret * (2 * 2) % p);
//余数为1则拆出一个2,其余为3
else if (y == 2) return (int) (ret * 3 * 2 % p);
return -1;
}
}
边栏推荐
猜你喜欢
每日刷题(day03)——leetcode 899. 有序队列
Deep learning TensorFlow entry environment configuration
LruCache与DiskLruCache结合简单实现ImageLoader
STM32F407ZG GPIO输入相关实验
三种素数筛总结——(朴素筛,埃氏筛,线性筛)
通过adb devices命令在控制台显示企业级PicoNeo3设备号
样条曲线(下)之插值问题(贝塞尔曲线、B样条和一般样条曲线插值)
STM32单片机手机APP蓝牙高亮RGB彩灯控制板任意颜色亮度调光
在Unity中利用代码动态更改场景中的天空盒
Notes for RNN
随机推荐
pytorch-08. Load dataset
每日刷题(day02)——leetcode 622. 设计循环队列
酸回收树脂工艺技术详解
Notes for Netual Network
在TypeScript中使用parseInt()
LeetCode 1894. Find the student number that needs to be supplemented with chalk
氨氮的有效吸附材料
C#对MySQL数据库进行增删改查操作(该操作还有防止MySQL注入功能)
I don't like my code
51单片机BH1750智能补光灯台灯光强光照恒流源LED控制系统
Flutter Package 插件开发
pytorch-09. Multi-classification problem
VTK 初步 (1) ----- 可视化管线
每日刷题(day03)——leetcode 899. 有序队列
Tkinter 模块学习
开源游戏服务器框架NoahGameFrame(NF)客户端环境搭建(三)
作为测试,常用的adb命令
ASP.NET连接SQL Server的步骤
Notes for RNN
钴镍回收树脂的工艺原理