题解 | #表示数值的字符串#

表示数值的字符串

https://www.nowcoder.com/practice/e69148f8528c4039ad89bb2546fd4ff8

/*
    请实现一个函数用来判断字符串str是否表示数值(包括科学计数法的数字,小数和整数)。
*/
#include <string>
#include <vector>
#include <iostream>
using namespace std;

class Solution {
private:
    bool isScientific(vector<char> strVec){
        auto it1 = find(strVec.begin(), strVec.end(), 'e');
        auto it2 = find(strVec.begin(), strVec.end(), 'E');
        int index_e = 0;

        if(it1 == strVec.end() && it2 == strVec.end()) 
            return false;
        else if(it1 != strVec.end()){
            index_e = distance(strVec.begin(), it1);
        }
        else{
            index_e = distance(strVec.begin(), it2);
        }

        // 看e前后的类型
        if((isInteger(strVec, 0, index_e-1) || isDecimal(strVec, 0, index_e-1))
            && isInteger(strVec, index_e+1, strVec.size()-1))
            return true;
        else
            return false;
    }   
    bool isDecimal(vector<char> strVec, int start, int end){
        if(strVec[start] =='+' || strVec[start] == '-'){ 
            start += 1;
            // strVec.erase(strVec.begin());
        }
        if(start>end) return false;

        auto it = find(strVec.begin(), strVec.begin()+end+1, '.');
        int index_dot = distance(strVec.begin(), it);

        if(index_dot == start){
            return isInteger(strVec, start+1, end);
        }
        else if(index_dot == end){
            return isInteger(strVec, start, end-1);
        }
        else{
            return isInteger(strVec, start, index_dot-1) && isInteger(strVec, index_dot+1, end);
        }
    } 
    bool isInteger(vector<char> strVec, int start, int end){
        if(strVec[start] =='+' || strVec[start] == '-'){
            start += 1;
            // strVec.erase(strVec.begin());
        }
        if(start>end) return false;

        for(int i=start; i<end+1; i++){
            if(strVec[i] > '9' || strVec[i] < '0'){
                return false;
            }
        }
        return true;
    }
public:
    bool isNumeric(string str) {
        if(str.empty()) return false;

        for(int index=0; index<str.size(); index++){
            vector<char> strVec;
            int start = 0;

            // if(str[index] == ' ') continue;

            if( !(str[index] == '+' || str[index] == '-'  || str[index] == '.' || str[index] == 'e'
                    || (str[index] <= '9' && str[index] >= '0')) ){
                        continue;
                    }
            else{
                start = index;
                while(index <str.size() && str[index] != ' '){
                    strVec.push_back(str[index]);
                    index++;
                }
                if(index>start) index--;
            }
            if(isScientific(strVec) || isDecimal(strVec, 0, strVec.size()-1) || isInteger(strVec, 0, strVec.size()-1)) return true;
            strVec.clear();
        }

        return false;
    }
};


全部评论

相关推荐

06-27 18:53
门头沟学院 Java
这样才知道自己不适合搞代码,考公去咯
只爱喝白开水:我也发现不适合搞代码,打算转非技术方向了
点赞 评论 收藏
分享
06-12 17:46
门头沟学院 Java
运营你豪哥:来说重点: ​1.项目前置,时间倒序。​​ 2.​项目描述强化结果与量化效果(STAR原则里的R)。​​ ​3.个人技能精炼,明确掌握程度,突出核心。​​ ​4.增加强有力开头的个人总结部分。​​ 5.​优化教育背景(成绩排名)、合并奖项与活动。​​
听劝,我这个简历该怎么改...
点赞 评论 收藏
分享
06-17 00:26
门头沟学院 Java
程序员小白条:建议换下项目,智能 AI 旅游推荐平台:https://github.com/luoye6/vue3_tourism_frontend 智能 AI 校园二手交易平台:https://github.com/luoye6/vue3_trade_frontend GPT 智能图书馆:https://github.com/luoye6/Vue_BookManageSystem 选项目要选自己能掌握的,然后最好能自己拓展的,分布式这种尽量别去写,不然你只能背八股文了,另外实习的话要多投,尤其是学历不利的情况下,多找几段实习,最好公司title大一点的
无实习如何秋招上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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