题解 | 合法IP

合法IP

https://www.nowcoder.com/practice/995b8a548827494699dc38c3e2a54ee9

//容易遗漏的点在于数字串中有非法字符或字符'0'后跟字符的情况
//还有就是IP只有三个

#include <iostream>
#include <sstream>
#include <string>
#include <deque>
using namespace std;

int main() {
    string inputstr;
    cin >> inputstr;
    inputstr += ".";
    string tmp;
    int count = 0;
    for (int i = 0; i < inputstr.size(); ++i) {

        if (tmp == "" && ((!isdigit(inputstr[i])) || (inputstr[i] == '0' &&
                          inputstr[i + 1] != '.'))) {
            cout << "NO" << endl;
            return 0;
        }
        if (inputstr[i] == '.') {
            if (stoi(tmp) <= 255 && stoi(tmp) >= 0) {
                tmp = "";
                ++count;
                continue;
            } else {
                cout << "NO" << endl;
                return 0;
            }
        }
        tmp += inputstr[i];
    }
    if (count == 4)cout << "YES" << endl;
    else cout << "NO" << endl;
    return 0;
}

全部评论

相关推荐

牛客50327486...:腾讯官方:我们没有人机对局
点赞 评论 收藏
分享
09-22 15:45
门头沟学院 Java
谁给娃offer我给...:我也遇到了,我说只要我通过面试我就去,实际上我根本就不会去😁
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务