【C++ • STL • 力扣】详解string相关OJ

文章目录


ヾ(๑╹◡╹)ノ" 人总要为过去的懒惰而付出代价 ヾ(๑╹◡╹)ノ"


1、仅仅翻转字母

力扣链接
代码1展示:【下标】

cpp 复制代码
class Solution {
bool isLetter(const char& c)
{
    if (c >= 'a' && c <= 'z')
        return true;
    else if (c >= 'A' && c <= 'Z')
        return true;
    else
        return false;
}
public:
    string reverseOnlyLetters(string s)
    {
        int left = 0;
        int right = s.size() - 1;
        while (left < right)
        {
            while (left < right && !isLetter(s[left]))
            {
                left++;
            }
            while (left < right && !isLetter(s[right]))
            {
                right--;
            }
            swap(s[left], s[right]);
            ++left;
            --right;
        }        
        return s;
    }
};

代码2展示:【迭代器】

cpp 复制代码
class Solution {
bool isLetter(const char& c)
{
    if (c >= 'a' && c <= 'z')
        return true;
    else if (c >= 'A' && c <= 'Z')
        return true;
    else
        return false;
}
public:
    string reverseOnlyLetters(string s)
    {
        string::iterator leftIt = s.begin();
        string::iterator rightIt = s.end() - 1;
        while (leftIt < rightIt)
        {
            while (leftIt < rightIt && !isLetter(*leftIt))
            {
                leftIt++;
            }
            while (leftIt < rightIt && !isLetter(*rightIt))
            {
                rightIt--;
            }
            swap(*leftIt, *rightIt);
            ++leftIt;
            --rightIt;
        }        
        return s;
    }
};

思路:快速排序中的单趟排序

知识点:C++库提供了swap的函数,可以直接调用。

2、字符串中的第一个唯一字符

力扣链接
代码展示

cpp 复制代码
class Solution {
public:
    int firstUniqChar(string s) 
    {
        int count[26] = { 0 };
        for (auto ch : s)
        {
            count[ch - 'a']++;
        }
        for (size_t i = 0; i < s.size(); i++)
        {
            if (count[s[i] - 'a'] == 1)
            {
                return i;
            }
        }
        return -1;

    }
};

思路:计数排序的思想

3、字符串里最后一个单词的长度

牛客链接
代码展示

cpp 复制代码
#include <iostream>
using namespace std;

int main()
{
    string s;
    getline(cin, s);
    size_t pos = s.rfind(' ');
    if (pos != string::npos)
    {
        cout << s.size() - (pos + 1);
    }
    else 
    {
        cout << s.size();
    }
    return 0;
}

思路:首先得到倒数第一个空格的下标,然后size进行减去(下标+1)

知识点:cin遇见空格会认为获取结束,当遇见一行字符串的时候,该字符串中间有空格,那么就不会获取到整行的字符串。getline会获取一行的字符串,遇到空格也不会认为获取终止。

4、验证一个字符串是否是回文

力扣链接
代码展示

cpp 复制代码
class Solution {
public:
    bool isLetterOrNumber(char ch)
    {
        return (ch >= '0' && ch <= '9')
            || (ch >= 'a' && ch <= 'z')
            || (ch >= 'A' && ch <= 'Z');
    }

    bool isPalindrome(string s) 
    {

        //小写换成大写
        for(auto& ch : s)
        {
            if (ch >= 'a' && ch <= 'z')
            {
                ch -= 32;
            }
        }

        int begin = 0;
        int end = s. size() - 1;
        while (begin < end)
        {
            while(begin < end && !isLetterOrNumber(s[begin]))
            {
                ++begin;
            }
            while(begin < end && !isLetterOrNumber(s[end]))
            {
                --end;
            }

            if(s[begin] != s[end])
            {
                return false;
            }
            else
            {
                ++begin;
                --end;
            }
        }
        return true;
    }
};

5、字符串相加

力扣链接
代码1展示:(头插)

cpp 复制代码
class Solution {
public:
    string addStrings(string num1, string num2) 
    {
        int end1 = num1.size() - 1;
        int end2 = num2.size() - 1;
        int carry = 0;//进位
        string s;
        while (end1 >= 0 || end2 >= 0)
        {
            //字符转换为数字
            int val1 = end1 >= 0 ? num1[end1] - '0' : 0;
            int val2 = end2 >= 0 ? num2[end2] - '0' : 0;
            int ret = val1 + val2 + carry;
            if (ret >= 10)
            {
                ret -= 10;
                carry = 1;
            }
            else
            {
                carry = 0;
            }
            s.insert(s.begin(), ret + '0');
            end1--;
            end2--;
        }
        if (carry == 1)
        {
            s.insert(s.begin(), '1');
        }
        return s;
    }
};

这个代码,使用头插,时间复杂度为O(N^2),算法不是最优的
代码2展示

cpp 复制代码
class Solution {
public:
    string addStrings(string num1, string num2) 
    {
        int end1 = num1.size() - 1;
        int end2 = num2.size() - 1;
        int carry = 0;//进位
        string s;
        while (end1 >= 0 || end2 >= 0)
        {
            //字符转换为数字
            int val1 = end1 >= 0 ? num1[end1] - '0' : 0;
            int val2 = end2 >= 0 ? num2[end2] - '0' : 0;
            int ret = val1 + val2 + carry;
            if (ret >= 10)
            {
                ret -= 10;
                carry = 1;
            }
            else
            {
                carry = 0;
            }
            //s.insert(s.begin(), ret + '0');
            s += (ret + '0');
            end1--;
            end2--;
        }
        if (carry == 1)
        {
            s += '1';
            //s.insert(s.begin(), '1');
        }
        reverse(s.begin(), s. end());
        return s;
    }
};

思路:倒着进行加法运算,当两个字符串的位数不一样的时候,位数少的前面进行补0【两个字符串都结束的时候才可以认为结束。【注意下标问为0的位置的进位】】

知识点:大数运算(当一个数字非常大的时候,可以用字符串的形式进行存放),字符串是不能进行加减运算的。此时就需要我们写代码。


总结

以上就是今天要讲的内容,本文详细的介绍了string的OJ题。希望给友友们带来帮助!

相关推荐
tdtsmt4 小时前
穿戴硬件量产工艺实录:天地通电子智能手表主板 SMT 贴片案
c++
m0_734571767 小时前
深入理解C++ 析构函数<一>基本概念
开发语言·c++
t-think8 小时前
C++ string 类(上)—— string类初识与常用接口详解
开发语言·c++
cpp_learners8 小时前
C++ 实现责任链模式(Chain of Responsibility):从一堆 if-else 到可插拔的处理管道
开发语言·c++·责任链模式
郝学胜_神的一滴8 小时前
C++11 工程级应用 12:编译期类型魔法,干掉重复与臃肿的代码
c++·visual studio
David猪大卫10 小时前
【C++修炼】哈希表的实现
数据结构·c++·笔记·学习·算法·哈希算法·散列表
广州山泉婚姻10 小时前
列表初始化:C++11全新初始化体系
c++·人工智能
莫生灬灬10 小时前
灵码 LingBuilder:用中文写代码,确定性生成真实 C++ 工程(开源)
c++·开源·mfc
浅念-10 小时前
C++ Protobuf 入门实战:基于通讯录项目吃透proto3语法与序列化
linux·服务器·网络·c++·protobuf·proto·proto3
汉克老师12 小时前
CSP-J 初赛(以满分为目标):第三十九课《数学与逻辑②——阶乘、排列数与组合数:公式到底从哪里来?》
c++·csp-j·小学生·学c++编程