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

表示数值的字符串

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


全部评论

相关推荐

不放弃的小鱼干很洒脱:好可爱的离职理由
点赞 评论 收藏
分享
双飞二本嵌入式求拷打我是在&nbsp;BOSS&nbsp;上投递的简历,好多都没人回复,这是开场白和简历求大神帮忙看看。您好!我是2025届应届生,最快可在一周内上岗,能够实习六个月以上,并接受加班。以下是我的核心优势和相关经验:1.&nbsp;嵌入式开发能力:&nbsp;&nbsp;&nbsp;熟练掌握STM32系列单片机及其外设(如GPIO、定时器、ADC、DAC、I2C、SPI、UART等),能够独立完成硬件驱动开发和调试。&nbsp;&nbsp;熟悉FreeRTOS实时操作系统,具备多任务调度和资源管理经验。&nbsp;&nbsp;熟悉LVGL图形库开发,能够实现嵌入式设备的图形界面设计。2.&nbsp;硬件设计能力:&nbsp;&nbsp;&nbsp;具备PCB设计经验,曾为2023年工创赛物流搬运赛道设计小车主板,带领团队获得国家级银奖。&nbsp;&nbsp;&nbsp;熟悉硬件原理图分析,能够快速理解并调试硬件电路。3.&nbsp;机器人开发与竞赛经验:&nbsp;&nbsp;&nbsp;在全国大学生智能车竞赛、ROS机器人竞赛中多次获得国家级奖项,具备丰富的机器人开发经验。&nbsp;&nbsp;&nbsp;熟悉Linux环境,对ROS和ROS&nbsp;2有一定了解,能够进行机器人系统的开发与调试。4.&nbsp;编程能力:&nbsp;&nbsp;&nbsp;熟悉C/C++,熟悉Python,能够高效完成嵌入式开发和算法实现。&nbsp;&nbsp;&nbsp;具备良好的代码规范和文档编写能力。5.&nbsp;团队协作与领导能力:&nbsp;&nbsp;&nbsp;在多个项目中担任核心开发或团队负责人,具备良好的沟通能力和团队协作精神。&nbsp;&nbsp;&nbsp;在工创赛中带领团队完成项目规划、任务分配和技术攻关,展现了较强的领导力。我对嵌入式开发、机器人技术和智能硬件充满热情,期待加入贵公司,与团队共同成长,为公司创造价值!如果有合适的岗位,欢迎随时联系我,期待进一步沟通!
沉淀一会:嵌入式就是狗屎
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务