首页 > 2022.09.07 广发银行
头像
Dummpling
发布于 2022-09-07 18:22 新加坡
+ 关注

2022.09.07 广发银行

1. 判断整数的二进制字符串是否是回文串

如 5 的二进制为 101 是回文串,6 的二进制为 110 不是回文串
bool isPalindromeBit(int n) {
    string str="";
    while(n!=0){
        str = to_string(n%2) + str;
        n=n/2;
    }
    cout<<str<<endl;
    for(int i = 0;i<str.length()/2;i++){
        if(str[i]!=str[str.length()-1-i]){
            return false;
        }
    }
    return true;
}


2. 识别邮箱字符串并输出 字符串

邮箱字符串的定义:

  1. 带@
  2. @前缀:至少2字符;首字符为大小写英文,数字,下划线;非首字符为大小写英文,数字,下划线_,点. ,减号-。
  3. @后缀:大小写字母,数字,点.;至少一个点. ;点后字符是2个以上的大小写字母。
如果存在邮箱字符串,则输出 true + 空格 + 所有的字符串。当存在多个可以同时匹配的邮箱字符串的时候输出最长的。
如果不存在这样的邮箱字符串,输出false
用正则匹配应该很简单,但是为什么不支持regex呢?
class Solution
{
public:
    bool isFirst(char c)
    {
        if ((c >= 'a' && c <= 'z') || 
            (c >= 'A' && c <= 'Z') || 
            (c >= '0' && c <= '9') || c == '_')
        {
            return true;
        }
        return false;
    }
    bool isFront(char c)
    {
        if (isFirst(c))
        {
            return true;
        }
        if (c == '.' || c == '-')
        {
            return true;
        }
        return false;
    }
    bool isBack(char c)
    {
        if ((c >= 'a' && c <= 'z') || 
            (c >= 'A' && c <= 'Z') || 
            (c >= '0' && c <= '9') || c == '.')
        {
            return true;
        }
        return false;
    }
    bool isEnd(char c)
    {
        if ((c >= 'a' && c <= 'z') || 
            (c >= 'A' && c <= 'Z'))
        {
            return true;
        }
        return false;
    }
    string printEmail(string str)
    {
        bool flag = false;
        string temp = "";
        int index = str.find('@');
        int front = index;
        int end = index;
        while (index >= 0)
        {
            //@前面有两位
            if (index >= 2 && isFront(str[index - 1]) && isFront(str[index - 2]))
            {   
                //找到前缀的起始位置front
                front = index - 2;
                while (front-1 >= 0 && isFront(str[front-1]))
                {
                    front--;
                }
                while (!isFirst(str[front]) && front <= index - 2)
                {
                    front++;
                }
                //@后面有小数点
                bool backAva = true;
                string back = str.substr(index + 1, str.size() - index);
                // 找到最远的小数点
                int point = back.find_last_of('.');
                if (point >= 0)
                {
                    int i = 0;
                    // 判断@和.之内的字符是否合法
                    while (i < point && backAva)
                    {
                        if (!isBack(back[i]))
                        {
                            // 不合法,找更近一点的小数点
                            back = back.substr(0, i);
                            point = back.find_last_of('.');
                            // 如果没有其他小数点,则说明后缀无效
                            if(point < 0){
                                backAva = false;
                                break;
                            }
                            i = 0;
                            continue;
                        }
                        i++;
                    }
                    // @和.之间的字符有效,再找到.之后的结束位置end
                    if (backAva && point + 2 < back.size() && isEnd(back[point + 1]) && isEnd(back[point + 2]))
                    {
                        point = point + 2;
                        while (point + 1 < back.size() && isEnd(back[point + 1]))
                        {
                            point++;
                        }
                        end = index + point + 1;
                    }
                }
            }
            if (front <= index - 2 && end >= index + 3)
            {
                if (!flag)
                {
                    flag = true;
                }
                temp += " " + str.substr(front, end - front + 1);
            }
            str = str.substr(end + 1, str.size() - end + 1);
            index = str.find('@');
        }
        if (flag)
        {
            return "true" + temp;
        }
        return "false";
    }
};


全部评论

(7) 回帖
加载中...
话题 回帖