【LeetCode】—— 字符串OJ题(C++实现)

本文深入解析了LeetCode上的两道经典题目:验证回文串(125题)和字符串相加(415题)。对于验证回文串,我们提供了一种高效的算法,能够忽略非字母数字字符和大小写差异进行判断。对于字符串相加,我们提出了一种不使用内置BigInteger库的方法,通过逐位相加并处理进位的方式,实现了两个大数的加法运算。

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

一、验证回文串LeetCode125题

1.1 题目描述
给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。

说明:本题中,我们将空字符串定义为有效的回文串。

示例 1:

输入: "A man, a plan, a canal: Panama"
输出: true

示例 2:

输入: "race a car"
输出: false

1.2 解题思路
验证回文串
1.3 代码实现

class Solution {
public:
    bool isNumberOrLetter(char c)
    {
        if(c >= '0' && c <= '9'
          || c >= 'a' && c <= 'z'
          || c >= 'A' && c <= 'Z')
        {
            return true;
        }
        else
            return false;
    }
    bool isPalindrome(string s) {
        if(s.empty())
            return true;
        
        int begin = 0;
        int end = s.size() - 1;
        
        while(begin < end)
        {
             //从前往后找符合条件的字符,用begin记录下标
        while(begin != end)
        {
            if(isNumberOrLetter(s[begin]) == 1)
                break;
            else
                ++begin;
        }
        
        //从后往前找符合条件的字符,用end记录下标
        while(begin < end)
        {
            if(isNumberOrLetter(s[end]) == 1)
                break;
            else
                --end;
        }
        
        //比较找到的两个字符是否相同,或者是不区分大小写的相同
        if(begin < end)
        {
            if((s[begin]+32-'a') % 32 != (s[end]+32-'a') % 32)
                return false;
            
                ++begin;
                --end;                    
        }
        }       
        return true;  
    }
};

二、字符串相加LeetCode415题

2.1 题目描述
给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和。

注意:

1.num1 和num2 的长度都小于 5100.
2.num1 和num2 都只包含数字 0-9.
3.num1 和num2 都不包含任何前导零。
4.你不能使用任何內建 BigInteger 库, 也不能直接将输入的字符串转换为整数形式。

2.2 解题思路
字符串相加
2.3 代码实现

class Solution {
public:
    string addStrings(string num1, string num2) {
        int index1 = num1.size()-1;
        int index2 = num2.size()-1;
        int next = 0;
        
        string retstr;
        while(index1 >= 0 || index2 >= 0)
        {
            int val1 = 0;
            int val2 = 0;
            
            if(index1 >= 0)
            {
                //将字符数字转换为对应的数字
                val1 = num1[index1] - '0';
                --index1;
            }
            
            if(index2 >= 0)
            {
                val2 = num2[index2] - '0';
                --index2;
            }
            
            int addret = val1 + val2 + next;
            
            if(addret > 9)
            {
                addret %= 10;
                next = 1;
            }
            else
                next = 0 ;
            //retstr=addret+'0';若这样写,retstr最后只是最后一个字符;
		        retstr += (addret + '0');
        }
            if (next == 1)
		        retstr += '1';
	        reverse(retstr.begin(), retstr.end());
	        
        return retstr;
        
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值