//
class Solution {
public:
vector<int> splitnum(int n){
vector<int> ans;
if(n<10){
ans.push_back(n);
}
else if(n<100){
// 68 - 6- 8 强制整型
ans.push_back(n/10);
ans.push_back(n%10);
}
else if(n<1000){
// 896 - 8 -9 6
ans.push_back(n/100);
ans.push_back(n%100/10);
ans.push_back(n%100%10);
}
return ans;
}
int getSum(vector<int> vec){
int sum = 0;
for(auto n: vec){
sum += n;
}
return sum;
}
int movingCount(int threshold, int rows, int cols) {
int cnt = 0;
for(int i=0; i<rows; i++){
if(getSum(splitnum(i))>threshold){
break;
}
for(int j=0; j<cols; j++){
if(getSum(splitnum(i))+getSum(splitnum(j))<=threshold){
cnt++;
}
else if(rows==1){
return cnt;
}
}
}
return cnt;
}
};
剑指数据结构-机器人的运动范围
最新推荐文章于 2024-09-13 19:36:07 发布
该博客主要探讨了一个基于整数拆分和累加的算法问题,涉及递归方法来解决移动计数。在给定阈值、行数和列数的条件下,算法通过拆分数字并计算子部分之和来确定可以移动的计数。文章详细解释了如何在不同数值范围下拆分整数,并展示了如何计算总和以判断是否超过阈值。
摘要由CSDN通过智能技术生成