当前位置:网站首页>Brush the title "sword finger offer" day04
Brush the title "sword finger offer" day04
2022-07-27 09:43:00 【Pac Man programming】
Title source : Power button 《 The finger of the sword Offer》 The second edition
Completion time :2022/07/25
List of articles
11. Minimum number of rotation array

My explanation
There are a lot of holes in this problem , Because there will be several special situations , I had no idea at first , The code on the book is typed , It's not simple enough .
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 the three are the same , Then you can only search by order
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];
}
// In order to find
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. The range of motion of the robot

My explanation
This problem can be solved by deep search
class Solution {
public:
// Check whether the grid can enter
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);
}
};
边栏推荐
- 拟搬迁!211中国石油大学(华东)新校区,正式启用!
- 吃透Chisel语言.26.Chisel进阶之输入信号处理(二)——多数表决器滤波、函数抽象和异步复位
- Transpose and inverse of [linear algebra 01] matrix
- How to install cpolar intranet penetration on raspberry pie
- 深入浅出详解Knative云函数框架!
- NPM common commands
- July training (day 24) - segment tree
- Why do microservices have to have API gateways?
- 食品安全 | 无糖是真的没有糖吗?这些真相要知道
- 七月集训(第06天) —— 滑动窗口
猜你喜欢

ESP8266-Arduino编程实例-中断
![WordPress prohibits login or registration of plug-ins with a specified user name [v1.0]](/img/94/92ad89751e746a18edf80296db9188.png)
WordPress prohibits login or registration of plug-ins with a specified user name [v1.0]

Eureka delayed registration of a pit

刷题《剑指Offer》day03

1344. Included angle of clock pointer

通俗易懂!图解Go协程原理及实战

Quick apply custom progress bar

Nacos做注册中心使用

NCCL (NVIDIA Collective Communications Library)

习题 --- 快排、归并、浮点数二分
随机推荐
吃透Chisel语言.23.Chisel时序电路(三)——Chisel移位寄存器(Shift Register)详解
七月集训(第13天) —— 双向链表
Google Earth engine app - maximum image synthesis analysis using S2 image
July training (day 05) - double pointer
基于 FPGA 按键控制呼吸灯原理、仿真及验证全过程
July training (day 20) - binary search tree
1344. Included angle of clock pointer
7/26 思维+dp+后缀数组的学习
Understand chisel language. 27. Chisel advanced finite state machine (I) -- basic finite state machine (Moore machine)
Towards the peak of life
July training (day 21) - heap (priority queue)
九种方式,教你读取 resources 目录下的文件路径
1640. Can you connect to form an array -c language implementation
七月集训(第26天) —— 并查集
七月集训(第11天) —— 矩阵
July training (day 04) - greed
npm常用命令
七月集训(第24天) —— 线段树
七月集训(第14天) —— 栈
Introduction to memory wall