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

表示数值的字符串

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;
    }
};


全部评论

相关推荐

07-04 21:23
已编辑
东莞城市学院 后端
秋招和春招只收到几个中大厂的笔试,本人比较菜,感觉大厂的笔试太难,算法题不能全部做出来就没过了,但是CVTE和小天才的感觉不是很难,基本上都做出来了,笔试还是挂了。Boss上投了Java后端开发都没有回音,boss上有面试机会都是C#工控软件开发方向的,但是这个方向不太懂,资料又少,面试的表现有点差,现在还是想看看Java这边,面试的时候比较有把握点。想请教一下,这份简历还有什么问题,一直没什么机会,还有什么地方要修改的。
程序员小白条:学历太差,民办和公办,外包还得区分的,这个学历+这个简历,没的办法,除非你有人脉,太难了,这环境,何况你都毕业了,连一段实习都没,肯定没公司会挑选了,没竞争力,开发才招几个人,跟你竞争的可不是二本,三本的人哦,何况你在二本,三本里面也排名不高
投递小天才等公司7个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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