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. utf-8mb4和排序规则

    MySQL在5.5.3之后增加了这个utf8mb4的编码,mb4就是most bytes 4的意思,专门用来兼容四字节的unicode. 最新的 UTF-8 规范只使用一到四个字节,最大能编码21位, ...

  2. Zuul 跨域

    JS访问会出现跨域问题的解决, 一.对单个接口,处理跨域,只需要在被调用的类或或方法增加注解 CoossOrigin 如下设置 allowCredenticals=true,表示运行Cookie跨域 ...

  3. Android的发展历史

    Android一词最早出现于法国作家利尔亚当(Auguste Villiers de l’Isle-Adam)在1886年发表的科幻小说<未来夏娃>(L’ève future)中.他将外表 ...

  4. 自己遇到的ajax调用ashx文件无法获取返回值的一种情况

    无法获取返回值的ashx文件大致如下: public void ProcessRequest (HttpContext context) { context.Response.ContentType ...

  5. python文件打开方式详解——a、a+、r+、w+区别

    出处: http://blog.csdn.net/ztf312/ 第一步 排除文件打开方式错误: r只读,r+读写,不创建 w新建只写,w+新建读写,二者都会将文件内容清零 (以w方式打开,不能读出. ...

  6. 初识docker-镜像

    前言: 以前学习docker 都是零零碎碎的,只知道用,有些莫名其妙的报错自己也没有思路去解决,所以基于一本专业的介绍docker的书籍,重新开启学习,该博客就记录下我自己的学习过程吧. 1.dock ...

  7. Promise事件比timeout优先

    Promise, setTimeout 和 Event Loop 下面的代码段,为什么输出结果是1,2,3,5,4而非1,2,3,4,5?(function test() { setTimeout(f ...

  8. android toolbar效果4

    两个标题的,右边一个按钮 activity_main.xml: <android.support.v7.widget.Toolbar style="style/toolbarStyle ...

  9. C#调用java代码(IKVMC)

    参考资料:https://blog.csdn.net/threadroc/article/details/51406587 参考1:http://www.cnblogs.com/Jack-Blog/p ...

  10. BASIC GIT WORKFLOW

    BASIC GIT WORKFLOW Generalizations You have now been introduced to the fundamental Git workflow. You ...