剑指数据结构-机器人的运动范围

该博客主要探讨了一个基于整数拆分和累加的算法问题,涉及递归方法来解决移动计数。在给定阈值、行数和列数的条件下,算法通过拆分数字并计算子部分之和来确定可以移动的计数。文章详细解释了如何在不同数值范围下拆分整数,并展示了如何计算总和以判断是否超过阈值。
摘要由CSDN通过智能技术生成
//
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;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值