String_day01

这篇博客涵盖了多种算法问题的解决方案,包括LeetCode中的字符串操作,如计算括号操作后的变量值,找出句子中的最多单词数;还涉及了数字处理,如找到十-二进制数的最少数目;此外,还讨论了Excel表中某个范围内的单元格生成,以及括号的嵌套深度计算。通过递归和遍历等方法,展示了如何高效地解决这些编程挑战。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

LeetCode

String

2011. 执行操作后的变量值
class Solution {
public:
    int finalValueAfterOperations(vector<string>& operations) {
        int num = 0;
        for(string i : operations){
            if(i[1] == '+')
            num++;
            else
            num--;
        }
        return num;
    }
};
2114. 句子中的最多单词数
class Solution {
public:
    int mostWordsFound(vector<string>& sentences) {
        vector<int>v;
        for(string str : sentences){
            int count = 0;
            for(int i = 0; i < str.size(); i++){
                if(str[i] == ' '){
                    count++;
                }
            }
            v.push_back(count);
        }
        sort(v.begin(), v.end());
    return v.back() + 1;;
    }
};
1689. 十-二进制数的最少数目
class Solution 
{
public:
    int minPartitions(string n) 
    {
        return *max_element(n.begin(), n.end()) - '0';
    }
};
2194. Excel 表中某个范围内的单元格
class Solution {
public:
    vector<string> cellsInRange(string s) {
        vector<string> ans;
        for (char a = s[0]; a <= s[3]; a++) {
            for (char b = s[1]; b <= s[4]; b++) {
                string tmp = {a, b};
                ans.push_back(tmp);
            }
        }
        return ans;
    }
};
剑指 Offer II 085. 生成匹配的括号
class Solution {
public:
    vector<string> res;
    vector<string> generateParenthesis(int n) {
        backtrack(n,0,"");
        return res;
    }

    void backtrack(int left,int right,string path){
        if(!left&&!right){res.push_back(path);return;}
        if(left)backtrack(left-1,right+1,path+'(');
        if(right)backtrack(left,right-1,path+')');
    }
};

771. 宝石与石头
class Solution {
public:
    int numJewelsInStones(string jewels, string stones) {
        int count = 0;
        for(int i = 0; i < jewels.size(); i++){
            for(int j = 0; j < stones.size(); j++){
                if(jewels[i] == stones[j]){
                    count++;
                }
            }
        }
        return count;
    }
};
1614. 括号的最大嵌套深度
class Solution {
public:
    int maxDepth(string s) {
        int ans = 0, size = 0;
        for (char ch : s) {
            if (ch == '(') {
                ++size;
                ans = max(ans, size);
            } else if (ch == ')') {
                --size;
            }
        }
        return ans;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

测试小白~o(〃'▽'〃)o

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值