LeetCode Valid Number
2016-01-09 12:41
288 查看
Description:
Validate if a given string is numeric.
Some examples:
Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.
Update (2015-02-10):
The signature of the
Solution:
我这里写的复杂了,可以一个valid的number可以这么写
a.bec
abc都是整数,a和c可以带上符号。这么写了之后再判断就容易很多。
Validate if a given string is numeric.
Some examples:
"0"=>
true
" 0.1 "=>
true
"abc"=>
false
"1 a"=>
false
"2e10"=>
true
Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.
Update (2015-02-10):
The signature of the
C++function had been updated. If you still see your function signature accepts a
const char *argument, please click the reload button to reset your code definition.
Solution:
我这里写的复杂了,可以一个valid的number可以这么写
a.bec
abc都是整数,a和c可以带上符号。这么写了之后再判断就容易很多。
<span style="font-size:18px;">public class Solution { public boolean isNumber(String s) { s = s.trim(); if (s.length() == 0) return false; if (s.charAt(0) == '+' || s.charAt(0) == '-') s = s.substring(1); char ch; int count_dot = 0, count_e = 0, count_num = 0; for (int i = 0; i < s.length(); i++) { ch = s.charAt(i); if (ch == '.') { if (count_dot == 0) count_dot++; else return false; } else if (ch >= '0' && ch <= '9') { count_num++; } else if (ch == '-') { if (i != 0) return false; } else if (ch == 'e' && i > 0 && i + 1 < s.length()) { for (int j = i + 1; j < s.length(); j++) { if ((s.charAt(j) == '+' || s.charAt(j) == '-') && (j == i + 1) && (j + 1 < s.length())) continue; else if (s.charAt(j) < '0' || s.charAt(j) > '9') return false; } if (count_num == 0) return false; return true; } else { return false; } } if (count_num == 0) return false; return true; } }</span>
相关文章推荐
- mailx定时任务不能执行
- PHP中mysql_affected_rows()和mysql_num_rows()区别
- Win10专业版怎么解决电脑关机后又自动开机的问题?
- PAT程序设计考题——甲级1006 签到问题(求所有签到人的最早时间和最晚时间)
- Log4j2介绍和特性实例(四)--过滤器Filter
- 登录界面注册存储过程
- 【机器学习实战】笔记三:朴素贝叶斯算法
- java打造手机远程控制电脑之详细教程
- sqlserver与java数据类型对应
- PLpgsql语言中returning-into的使用方法 总结
- Ubuntu14.04 YouCompleteMe Configure
- ubuntu 安装Nginx + rtmp + ffmpeg
- A beginner's guide to writing a custom stream buffer (std::streambuf)
- 快速排序算法小结
- BZOJ 2190[SDOI2008]仪仗队
- ios字典用字符串输出
- hdu1864(贪心or01背包)
- Java数据类型和MySql数据类型对应表
- session 持久化问题(重启服务器session 仍然存在)
- 深入理解HBase Memstore