Validate if a given string can be interpreted as a decimal number.

Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true
" -90e3   " => true
" 1e" => false
"e3" => false
" 6e-1" => true
" 99e2.5 " => false
"53.5e93" => true
" --6 " => false
"-+3" => false
"95a54e53" => false

Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one. However, here is a list of characters that can be in a valid decimal number:

  • Numbers 0-9
  • Exponent - "e"
  • Positive/negative sign - "+"/"-"
  • Decimal point - "."

Of course, the context of these characters also matters in the input.

思路

There is no complex algorithem, just checking each char from input String

代码

 // {前缀空格}{浮点数}{e}{正数}{后缀空格}
class Solution {
public boolean isNumber(String s) {
int len = s.length();
int i = 0;
int right = len - 1; // delete white spaces on both sides
while (i <= right && Character.isWhitespace(s.charAt(i))) i++;
if (i > len - 1) return false;
while (i <= right && Character.isWhitespace(s.charAt(right))) right--; // check +/- sign
if (s.charAt(i) == '+' || s.charAt(i) == '-') i++; boolean num = false; // is a digit
boolean dot = false; // is a '.'
boolean exp = false; // is a 'e' while (i <= right) {
char c = s.charAt(i);
// first char should be digit
if (Character.isDigit(c)) {
num = true;
}
else if (c == '.') {
// exp and dot cannot before '.'
if(exp || dot) return false;
dot = true;
}
else if (c == 'e') {
// only one 'e'can exist
// 'e' should after num
if(exp || num == false) return false;
exp = true;
// after 'e' should exist num, so set num as default false
num = false;
}
else if (c == '+' || c == '-') {
if (s.charAt(i - 1) != 'e') return false;
}
else {
return false;
}
i++;
}
return num;
}
}

变种之简化版本的Valid Number : checking +/- numbers including decimal numbers

需要跟面试官confirm

以下哪些算valid,若以下test case中,蓝色字体是valid的

123
-123
123.123
-123.123
.123
-.123
123.
123-.

那么:

1.  Decimal point 前面必须是数字

2. Decimal point 后面必须是数字

3. 整个valid number最后是以数字结尾

 public boolean isNumber(String s) {
int len = s.length();
int i = 0;
int right = len - 1; // delete white spaces on both sides
while (i <= right && Character.isWhitespace(s.charAt(i))) i++;
if (i > len - 1) return false;
while (i <= right && Character.isWhitespace(s.charAt(right))) right--; // check +/- sign
if (s.charAt(i) == '+' || s.charAt(i) == '-') i++; boolean num = false; // is a digit
boolean dot = false; // is a '.' while (i <= right) {
char c = s.charAt(i);
if (Character.isDigit(c)) {
num = true;
}
else if (c == '.') {
// (1) .. two dots (2) no number before dot (3) - before dot
if( dot || num == false || s.charAt(i-1) == '-') return false;
dot = true;
// check whether there is number after decimal point
num = false;
}
else {
return false;
}
i++;
}
return num;
}

[leetcode]65. Valid Number 有效数值的更多相关文章

  1. leetCode 65.Valid Number (有效数字)

    Valid Number  Validate if a given string is numeric. Some examples: "0" => true " ...

  2. [LeetCode] 65. Valid Number 验证数字

    Validate if a given string can be interpreted as a decimal number. Some examples:"0" => ...

  3. LeetCode 65 Valid Number

    (在队友怂恿下写了LeetCode上的一个水题) 传送门 Validate if a given string is numeric. Some examples: "0" =&g ...

  4. Leetcode 65 Valid Number 字符串处理

    由于老是更新简单题,我已经醉了,所以今天直接上一道通过率最低的题. 题意:判断字符串是否是一个合法的数字 定义有符号的数字是(n),无符号的数字是(un),有符号的兼容无符号的 合法的数字只有下列几种 ...

  5. [LeetCode] 65. Valid Number(多个标志位)

    [思路]该题题干不是很明确,只能根据用例来理解什么样的字符串才是符合题意的,本题关键在于几个标志位的设立,将字符串分为几个部分,代码如下: class Solution { public: strin ...

  6. 【LeetCode】65. Valid Number

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...

  7. 【leetcode】Valid Number

    Valid Number Validate if a given string is numeric. Some examples:"0" => true" 0.1 ...

  8. 【一天一道LeetCode】#65. Valid Number

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Validat ...

  9. 65. Valid Number

    题目: Validate if a given string is numeric. Some examples:"0" => true" 0.1 " = ...

随机推荐

  1. dmi-ipmi

    api,cli,gui,tui,dmi(smbios),ipmi,bios,efi,uefi SMBIOS(System Management BIOS)是主板或系统制造者以标准格式显示产品管理信息所 ...

  2. Linux umask命令详解

    Linux umask命令 Linux umask命令指定在建立文件时预设的权限掩码.一般来说,umask命令是在/etc /profile文件中设置的 umask可用来设定[权限掩码].[权限掩码] ...

  3. 阿里巴巴数据源Druid在tomcat中的配置

    这里只说需要的配置文件,不讲具体的项目,仅作为备忘. pom.xml文件添加 <!-- druid --> <dependency> <groupId>com.al ...

  4. 5.IAP - FLASH

    一.Flash与时钟系统的关系            STM32系统时钟:                 HSE 高速外部时钟,电路上焊接的外部时钟,一般是4Mhz-16Mhz,板子上的是8Mhz ...

  5. python:函数初始

    一.函数 1.函数初始:函数就是封装一个功能 2.函数名,函数体,关键字,函数的返回值 def 关键字,定义一个函数 my_len 函数名书写规则和变量一样 def 与函数名中间一个空格 函数名(): ...

  6. 【3D美术教程】手雷(传统与PBR流程)

    转自:https://www.sohu.com/a/156489635_718614 随着最新的次时代技术PBR流程的普及,越来越多的公司由传统流程转向了PBR流程,主要原因在于PBR材质不仅效果上更 ...

  7. 用GDB调试程序(七)

    改变程序的执行——————— 一旦使用GDB挂上被调试程序,当程序运行起来后,你可以根据自己的调试思路来动态地在GDB中更改当前被调试程序的运行线路或是其变量的值,这个强大的功能能够让你更好的调试你的 ...

  8. suricata HTTP关键字

    http request http request请求包括请求行.请求头.空行和内容.一个普通的request请求如下: http response http response应答包括应答行,头部,空 ...

  9. 61.纯 CSS 创作一只咖啡壶(这个不好看)

    原文地址:https://segmentfault.com/a/1190000015376202 感想: 好像不像呀,啊啊啊.伪元素.定位.动画.width和height包括内边距|边框|内容区. H ...

  10. ARCore中四元数的插值算法实现

    ARCore中四元数差值算法: 其中t的取值范围为[0, 1],当 t = 0 时,结果为a:当t = 1 时,结果为b. public static Quaternion makeInterpola ...