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. C++对象的构造、析构与拷贝构造

    今天下午在研究虚函数的时候遇到了一个问题,觉得很有意思,记录一下. 先看代码: class Base { public: Base(int value) { m_nValue = value; cou ...

  2. mysql悲观锁

    悲观锁与乐观锁是两种常见的资源并发锁设计思路,也是并发编程中一个非常基础的概念. 悲观锁(Pessimistic Lock) 悲观锁的特点是先获取锁,再进行业务操作,即“悲观”的认为获取锁是非常有可能 ...

  3. [转]golang的goroutine调度机制

    golang的goroutine调度机制 版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[-] 一直对goroutine的调度机制很好奇最近在看雨痕的golang源码分析基于go ...

  4. c 链表和动态内存分配

    兜兜转转又用到了c.c的一些基本却忘记的差不多了(笑哭)!! 动态内存分配 当malloc完将返回的指针类型强制转换成想要的类型后,指针中存有该指针的数据结构,而分配的内存恰好可用于该数据结构. 链表 ...

  5. Centos7创建CA和申请证书 转自https://www.cnblogs.com/mingzhang/p/8949541.html

    Centos7.3创建CA和申请证书 openssl 的配置文件:/etc/pki/tls/openssl.cnf 重要参数配置路径 dir   = /etc/pki/CA               ...

  6. react 在 componentWillMount() 中调用异步函数时,componentWillMount() finishes after render()

    刚开始使用 react,很多属性.方法不是很熟.在此记录下我所遇到的问题及解决方法. 我在 componentWillMount() 中调用了一个异步函数,在返回结果中调用 this.setState ...

  7. 文件-- 字节相互转换(word、图片、pdf...)

    方式一: /// <summary> /// word文件转换二进制数据(用于保存数据库) /// </summary> /// <param name="wo ...

  8. 【C++】vector内存机制和性能分析

    转自:https://blog.csdn.net/mfcing/article/details/8746256 一些好的公司校园招聘过程中(包括笔试.面试环节),经常会涉及到STL中vector的使用 ...

  9. Verilog HDL中的运算符关系

    1,位运算符 按位运算的运算符是位运算符,原来的操作数有几位,结果就有几位,若两个操作数位数不同,则位数短的操作数左端会自动补0. (1),按位取反:~ (2),按位与:& (3),按位或:| ...

  10. linux查看用户登录,操作历史等

    who 命令:显示当前当登录的用户的信息 who -b命令:显示系统最近一次的启动时间 w 命令:显示登录的用户及其当前执行的任务 last 命令:显示当前与过去登录系统的用户的信息 lastb 命令 ...