当前位置:网站首页>刷题《剑指Offer》day04
刷题《剑指Offer》day04
2022-07-27 09:28:00 【吃豆人编程】
题目来源:力扣《剑指Offer》第二版
完成时间:2022/07/25
11. 旋转数组的最小数字

我的题解
这道题坑有点多,因为会有几种特殊情况,我一开始没有思路,看书上的代码敲的,不够简洁。
class Solution {
public:
int minArray(vector<int>& numbers) {
int right = numbers.size() - 1,left = 0,mid = left;
while(numbers[left] >= numbers[right]) {
if(right - left == 1){
mid = right;
break;
}
mid = left + (right - left) / 2;
//如果三者相同,则只能通过顺序查找
if(numbers[mid] == numbers[right] && numbers[mid] == numbers[left]){
return minInOrder(left,right,numbers);
}
if(numbers[mid] >= numbers[right]) {
left = mid;
}else if(numbers[mid] <= numbers[right]){
right = mid;
}
}
return numbers[mid];
}
//顺序查找
int minInOrder(int left,int right,vector<int>& numbers) {
int min = numbers[left];
for(int i = 0;i < right;i++) {
if(numbers[i] < min){
min = numbers[i];
}
}
return min;
}
};
13. 机器人的运动范围

我的题解
这道题用深搜就可以解决了
class Solution {
public:
//检查格子是否能进入
bool check(int x,int y,int k,int m,int n,vector<vector<int>>& myMap) {
if(x > m-1 || y > n-1 || x < 0 || y < 0 || myMap[x][y] == 1){
return false;
}
int result = 0;
while(x > 0) {
result += x % 10;
x /= 10;
}
while(y > 0) {
result += y % 10;
y /= 10;
}
return result <= k;
}
int dfs(int m,int n,int x,int y,int k,vector<vector<int>>& myMap) {
int count = 0;
if(check(x,y,k,m,n,myMap)){
myMap[x][y] = 1;
count = 1;
count += dfs(m,n,x+1,y,k,myMap);
count += dfs(m,n,x-1,y,k,myMap);
count += dfs(m,n,x,y+1,k,myMap);
count += dfs(m,n,x,y-1,k,myMap);
}
return count;
}
int movingCount(int m, int n, int k) {
vector<vector<int>> myMap(m);
for(int i= 0;i < m;i++){
myMap[i].resize(n);
}
return dfs(m,n,0,0,k,myMap);
}
};
边栏推荐
- July training (day 15) - depth first search
- Google Earth Engine APP——利用S2影像进行最大值影像合成分析
- [C language - zero foundation _ study _ review _ lesson 4] data types and operations
- [C language - zero foundation lesson 14] variable scope and storage class
- 工程测量模拟A卷
- 好久不送书,浑身不舒服
- Nacos做注册中心使用
- 拟搬迁!211中国石油大学(华东)新校区,正式启用!
- STL container - basic operation of queue and deque
- 645. Wrong set
猜你喜欢

ESP8266-Arduino编程实例-ADC

vscode使用remote-ssh连接以及连接失败的解决方法

【武汉理工大学】考研初试复试资料分享

Special exercises for beginners of C language to learn code for the first time

Sentinel 万字教程 | 文末送书

ES6 new symbol data type

The command prompt cannot start mysql, prompting system error 5. Access denied. terms of settlement

Hard core structure, violent interpretation

BGP联邦实验

The whole process of principle, simulation and verification of breath lamp controlled by FPGA keys
随机推荐
Community attribute of BGP
Nccl collective communication --collective operations
如何使用TDengine Sink Connector?
Nacos is used as a registration center
Google Earth Engine APP——利用S2影像进行最大值影像合成分析
2068. Check whether the two strings are almost equal
July training (day 06) - sliding window
ES6 new - deconstruction assignment of array / object
七月集训(第04天) —— 贪心
1344. 时钟指针的夹角
【线性代数01】矩阵的转置和逆
BGP的社团属性
You haven't heard of such 6 question brushing websites, have you? Are you out?
35-Spark Streaming反压机制、Spark的数据倾斜的解决和Kylin的简单介绍
ESP8266-Arduino编程实例-ADC
走向人生巅峰....
七月集训(第08天) —— 前缀和
Read the paper snunet CD: a densely connected Siamese network for change detection of VHR images
2068. 检查两个字符串是否几乎相等
July training (day 10) - bit operation