LeetCode //C - 65. Valid Number

65. Valid Number

Given a string s, return whether s is a valid number.

For example, all the following are valid numbers: “2”, “0089”, “-0.1”, “+3.14”, “4.”, “-.9”, “2e10”, “-90E3”, “3e+7”, “+6e-1”, “53.5e93”, “-123.456e789”, while the following are not valid numbers: “abc”, “1a”, “1e”, “e3”, “99e2.5”, “–6”, “-+3”, “95a54e53”.

Formally, a valid number is defined using one of the following definitions:

  1. An integer number followed by an optional exponent.
  2. A decimal number followed by an optional exponent.

An integer number is defined with an optional sign ‘-’ or ‘+’ followed by digits.

A decimal number is defined with an optional sign ‘-’ or ‘+’ followed by one of the following definitions:

  1. Digits followed by a dot ‘.’.
  2. Digits followed by a dot ‘.’ followed by digits.
  3. A dot ‘.’ followed by digits.

An exponent is defined with an exponent notation ‘e’ or ‘E’ followed by an integer number.

The digits are defined as one or more digits.
 

Example 1:

Input: s = “0”
Output: true

Example 2:

Input: s = “e”
Output: false

Example 3:

Input: s = “.”
Output: false

Constraints:
  • 1 <= s.length <= 20
  • s consists of only English letters (both uppercase and lowercase), digits (0-9), plus ‘+’, minus ‘-’, or dot ‘.’.

From: LeetCode
Link: 65. Valid Number


Solution:

Ideas:

1. Flag Variables:

  • numSeen: This flag indicates whether at least one digit has been encountered in the string. It helps ensure there’s a valid numeric component in the string.
  • dotSeen: Tracks if a decimal point has been seen. This prevents multiple decimals which would invalidate the format.
  • eSeen: Indicates if an exponent character (e or E) has appeared, ensuring no repeated exponents and that the exponent is properly formatted.
  • numberAfterE: After an exponent is seen, this flag checks if a valid number follows. The presence of a number after the exponent is crucial for a valid scientific notation.

2. Initial Whitespace Handling:

  • The loop while (*s == ’ ') { s++; } moves the pointer s past any leading white spaces. Although the problem constraints do not mention spaces, handling them might make the function more generally applicable.

3. Character-by-Character Validation:

  • The for loop iterates through each character of the string:
    Digits: On encountering a digit, it sets numSeen to true and numberAfterE to true, indicating that there’s a valid number component.
    • Decimal Point (‘.’): Valid only if no previous decimal point or exponent has been seen (dotSeen or eSeen are false).
    • Exponent (‘e’ or ‘E’): Valid only if no previous exponent has been seen and there has been at least one digit before it. It sets eSeen to true and expects digits to follow (numberAfterE to false).
    • Signs (‘+’ or ‘-’): Signs are valid only at the beginning of the string or immediately following an exponent.
      Invalid Characters: Any character that is not a digit, a sign, a decimal point, or an exponent character leads to an immediate return of false.

4. Final Validity Check:

  • After exiting the loop, the function checks if at least one number has been seen (numSeen) and if a valid number follows any e or E (numberAfterE). This final validation ensures the overall string represents a valid number format.

5. Edge Case and Error Handling:

  • The function is designed to immediately reject strings upon encountering invalid scenarios, making it efficient in terms of runtime, especially for strings that fail early in the validation process.
Code:
bool isNumber(char* s) {// State flagsbool numSeen = false;bool dotSeen = false;bool eSeen = false;bool numberAfterE = true;// Trim leading whitespacewhile (*s == ' ') {s++;}// Process each characterfor (int i = 0; s[i] != '\0'; i++) {if (isdigit(s[i])) {numSeen = true;numberAfterE = true;} else if (s[i] == '.') {// There must not be a dot or 'e'/'E' already seenif (dotSeen || eSeen) {return false;}dotSeen = true;} else if (s[i] == 'e' || s[i] == 'E') {// There must not be an 'e'/'E' already seen and there must be a number before itif (eSeen || !numSeen) {return false;}eSeen = true;numberAfterE = false;} else if (s[i] == '+' || s[i] == '-') {// Sign must come after 'e' or 'E', or be at the startif (i > 0 && (s[i-1] != 'e' && s[i-1] != 'E')) {return false;}} else {// Invalid characterreturn false;}}// Trim trailing whitespacewhile (*s == ' ') {s++;}// Valid number if we've seen a number and if after 'e'/'E' there is also a numberreturn numSeen && numberAfterE;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/1412595.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

牛客NC383 主持人调度(一)【简单 排序 Java/Go/C++】

题目 题目链接&#xff1a; https://www.nowcoder.com/practice/e160b104354649b69600803184094adb 思路 直接看代码&#xff0c;不难Java代码 import java.util.*;public class Solution {/*** 代码中的类名、方法名、参数名已经指定&#xff0c;请勿修改&#xff0c;直接返…

学习如何使用PyQt5实现notebook功能

百度搜索“pyqt5中notebook控件”&#xff0c;AI自动生成相应例子的代码。在 PyQt5 中&#xff0c;QTabWidget 类被用作 Notebook 控件。以下是一个简单的示例&#xff0c;展示如何创建一个带有两个标签的 Notebook 控件&#xff0c;并在每个标签中放置一些文本。 import sys f…

伺服电机初识

目录 一、伺服电机的介绍二、伺服电机的基本原理三、伺服电机的技术特点四、伺服电机的分类五、实际产品介绍1、基本技术规格&#xff1a;2、MD42电机硬件接口3、通讯协议介绍3.1 通讯控制速度运行3.2 通讯控制位置运行3.3 通讯控制转矩运行 4、状态灯与报警信息 一、伺服电机的…

45. UE5 RPG 增加角色受击反馈

在前面的文章中&#xff0c;我们实现了对敌人的属性的初始化&#xff0c;现在敌人也拥有的自己的属性值&#xff0c;技能击中敌人后&#xff0c;也能够实现血量的减少。 现在还需要的就是在技能击中敌人后&#xff0c;需要敌人进行一些击中反馈&#xff0c;比如敌人被技能击中后…

40.乐理基础-拍号-什么是一拍

拍&#xff1a; 首先 以Y分音符的时长为一拍 这一句话&#xff0c;然后拍是音乐中的时长单位&#xff0c;但这个时长单位有点特殊&#xff0c;它并不是完全绝对的某一个时间&#xff0c;而正是因为如此&#xff0c;所以不能用 秒 之类的&#xff0c;已经很确定很绝对的时间单位…

C++手写协程项目(协程实现线程结构体、线程调度器定义,线程挂起、切换、恢复函数,模块测试)

协程结构体定义 之前我们使用linux下协程函数实现了线程切换&#xff0c;使用的是ucontext_t结构体&#xff0c;和基于这个结构体的四个函数。现在我们要用这些工具来实现我们自己的一个线程结构体&#xff0c;并实现线程调度和线程切换、挂起。 首先我们来实现以下线程结构体…

C语言自定义类型枚举、枚举类型的定义、枚举的特点、以及自定义类型联合体、联合类型的定义、联合的特点、联合大小的计算、联合判断大小端 的介绍

文章目录 前言一、枚举1. 枚举类型的定义2. 枚举的特点 二、联合&#xff08;共用体&#xff09;1. 联合类型的定义2. 联合的特点3. 联合大小的计算4. 联合体判断大小端1. 不适用联合体判断大小端2. 使用联合体判断大小端 总结 前言 C语言自定义类型枚举、枚举类型的定义、枚举…

520表白代码

一、以下代码用html及css编写 代码用记事本打开可直接使用 二、效果如下 代码如下&#xff0c;以下代码复制记事本里面&#xff0c;文件名称后缀改成.html格式&#xff0c;即可运行 名称可在记事本里进行更改 文件名称更改后&#xff0c;文件会变成如下图所示的样式 <ht…

STM32 PWM波定时溢出中断

打开定时器和中断 主函数初始化开启PWM和中断 HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_1); __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, Pwm_data); HAL_TIM_Base_Start_IT(&htim2); 回调函数中判断是否为tim2 void HAL_TIM_PeriodElapsedCallback(TIM_Han…

Docker目录迁移

我们在生产环境中安装Docker时&#xff0c;默认的安装目录是 /var/lib/docker&#xff0c;而通常情况下&#xff0c;规划给系统盘的目录一般为50G&#xff0c;该目录是比较小的&#xff0c;一旦容器过多或容器日志过多&#xff0c;就可能出现Docker无法运行的情况&#xff0c;所…

视频下载器 UC网盘

老王导航 - 复杂问题找老王&#xff0c;简单问题百度搜 神器啊

Mysql 8.0.33 迁移至 Postgresql 16.2

小伙伴们&#xff0c;你们好&#xff0c;我是老寇&#xff0c;我又回来&#xff0c;几个月不见&#xff0c;甚是想念啊&#xff01;&#xff01;&#xff01;&#xff01; 这不&#xff0c;云平台需要改造&#xff0c;将Mysql替换成Postgresql&#xff0c;话说回来&#xff0c…

nginx--反向代理

反向代理 指的是代理外网用户的请求到内部的指定web服务器器&#xff0c;并将数据返回给用户的一种方式&#xff0c;这是用的比较多的一种方式 模块和功能 ngx_http_proxy_module&#xff1a; 将客户端的请求以http协议转发至指定服务器进行处理。ngx_stream_proxy_module&…

【Python项目】基于时间序列的【大气污染预测系统】

技术简介&#xff1a;使用Python技术、B/S架构、MYSQL数据库等实现。 系统简介&#xff1a;本系统的主要使用角色为普通用户和管理员用户&#xff0c;两者的功能几乎是一致的&#xff0c;但管理员用户比普通用户多了用户管理的功能&#xff0c;可以对系统内的用户进行管理。普通…

实时聊天系统设计

设计一个聊天系统最主要是保证消息能够及时可靠的从一端传入到另外一端&#xff0c;同时要支持对历史消息的查看。按照同时聊天人数聊天系统可以分&#xff0c;一对一&#xff08;one on one&#xff09;和群聊&#xff08;group chat&#xff09;&#xff0c;按照消息传递的及…

JavaEE >> Spring Boot 日志

日志的作用以及什么是日志 日志就是为了当程序出错的时候&#xff0c;程序员们可以通过日志看到是哪部分出现错误了&#xff0c;为了发现和定位问题。当然&#xff0c;我们还可以通过日志实现一些功能&#xff0c;如下&#xff1a; 记录系统的操作⽇志&#xff0c;⽅便数据恢…

Java常用命令总结 持续更新中!!!

蓝桥杯JAVA组 推荐输入输出示例 // 基础输入 import java.util.*;public class Main{public static void main(String[] args){} }// 非静态方法调用 new Main.Solution();//static函数里面调用非static函数 类.函数// 更快的输入方式 BufferedReader // 更快的输出方式 Print…

如何将jsp项目转成springboot项目

昨天说过&#xff0c;springboot推荐使用Thymeleaf作为前后端渲染的模板引擎&#xff0c;为什么推荐用Thymeleaf呢&#xff0c;有以下几个原因&#xff1a; 动静结合&#xff1a;Thymeleaf支持HTML原型&#xff0c;允许在HTML标签中增加额外的属性来实现模板与数据的结合。这样…

docker-compose编排集成工具,consul服务更新与发现

一、引言 我们知道使用一个 Dockerfile 模板文件可以定义一个单独的应用容器&#xff0c;如果需要定义多个容器就需要服务编排。服务编排有很多种技术方案&#xff0c;今天给大家介绍 Docker 官方产品 Docker-Compose Dockerfile 可以定义一个单独的应用容器&#xff1…

虾皮(Shopee)商品详情API接口:轻松获取商品深度信息

API接口概述 虾皮的商品详情API接口是专为商家和开发者提供的服务接口&#xff0c;通过该接口&#xff0c;您可以快速、准确地获取指定商品的详细信息。这些信息包括但不限于商品标题、价格、库存、描述、图片、规格参数等&#xff0c;为您的商品展示、比价、推荐等场景提供有…