剑指offer——字符串

JZ2——替换空格

在这里插入图片描述

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param s string字符串 
     * @return string字符串
     */
    string replaceSpace(string s) {
        // write code here
        string res;
        for(auto it = s.begin(); it != s.end(); it++)
        {
            if( ! isspace(*it) )
                res += *it;
            else
                res += "%%";
                
        }
        
        return res;
    }
};

JZ44——翻转单词顺序列

在这里插入图片描述
注:注意一些特殊情况

class Solution {
public:
    string ReverseSentence(string str) {
        string res;
        string temp;
        int i = 0;
        for(auto it = str.cbegin(); it != str.cend(); it++)
        {
            
            if( !isspace(*it))
                temp += *it;
            else{
                i++;
                if(i == 1)
                    res = temp;
                else
                    res = temp + " " + res;
                temp.clear();
            }
        }
        i++;
        if(i == 1)
            res = temp;
        else
            res = temp + " " + res;
        return res;
    }
};

JZ45——扑克牌顺子

在这里插入图片描述

思路:
满足两个条件
1. 除了0之外没有重复的数
2. max - min <= 4

class Solution {
public:
    int max = -1, min = 14;
    map<int,int> count;
    bool IsContinuous( vector<int> numbers ) {
        if(numbers.size() == 0)
            return false;
        for(auto it : numbers)
        {
            count[it]++;
            if(count[it] > 1 && it!= 0)
                return false;
            if(it < min && it != 0)
                min = it;
            if(it > max)
                max = it;
        }
        
        if(max - min > 4)
            return false;
        else
            return true;
    }
};

/*思路:
    满足两个条件
        1. 除了0之外没有重复的数
        2. max - min <= 4  
        */

JZ49——把字符串转换成整数

在这里插入图片描述

**主要考虑的几个点

  1. 首位是不是 ‘+’ ‘-’ 符号,并考虑对结果的影响
  2. 首位是不是字母,若是直接返回0
  3. 中间是否出现字母,一旦出现字母立刻返回0**
class Solution {
public:
    int StrToInt(string str) {
        int len = str.size();
        int flag = 1;
        int res = 0;
        if(len == 0)
            return 0;
        if(str[0] == '+')
            flag = 1;
        else if(str[0] == '-')
            flag = -1;
        else if(isalpha(str[0]))
            return 0;
        else
            res = str[0] - '0';
        for(int i = 1; i < len; i++)
        {
            if(isalpha(str[i]))
                return 0;
            else
                res = res * 10 + (str[i] - '0');
        }
        return flag*res;
    }
};

JZ54——字符流中第一个不重复的字符

在这里插入图片描述

使用map可以很简单的解决

class Solution
{
public:
  //Insert one char from stringstream
    void Insert(char ch) {
        str.push_back(ch);
        count[ch]++;
    }
  //return the first appearence once char in current stringstream
    char FirstAppearingOnce() {
        for(int i = 0; i < str.size(); i++){
            if(count[str[i]] == 1)
                return str[i];
        }
        return '#';
    }
   
private:
    string str;
    map<char,int> count;

};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值