Valid Number,判断是否为合法数字
问题描述:
Validate if a given string is numeric.
Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => true
public boolean isNumber(String s)
{
s = s.trim();
if (s.length() == 0) return false;
boolean hasE = false;
boolean hasDot = false;
boolean hasNumber = false; for (int i = 0; i < s.length(); i++)
{
// e cannot be the first character
if (i == 0 && s.charAt(i) == 'e') return false;
if (s.charAt(i) == 'e') {
// e cannot be replicated nor placed before number
if (hasE == true || hasNumber == false) {
return false;
} else {
hasE = true;
}
} if (s.charAt(i) == '.') {
// '.' cannot be replicated nor placed after 'e'
if (hasDot == true || hasE == true) {
return false;
} else {
hasDot = true;
}
}
// the sign can be placed at the beginning or after 'e'
if (i != 0 && s.charAt(i - 1) != 'e' && (s.charAt(i) == '+' || s.charAt(i) == '-')) return false; // no other chacraters except '+', '-', '.', and 'e'
if ((s.charAt(i) > '9' || s.charAt(i) < '0') && s.charAt(i) != '+' && s.charAt(i) != '-' && s.charAt(i) != '.' && s.charAt(i) != 'e')
return false; // check whether numbers are included.
if (s.charAt(i) <= '9' && s.charAt(i) >= '0')
{
hasNumber = true;
}
}
// '+', '-', and 'e' cannot be the last character
if (s.charAt(s.length() - 1) == '-' || s.charAt(s.length() - 1) == '+' || s.charAt(s.length() - 1) == 'e') return false; return hasNumber;
}
}
Valid Number,判断是否为合法数字的更多相关文章
- lintcode:Valid Sudoku 判断数独是否合法
题目: 判断数独是否合法 请判定一个数独是否有效.该数独可能只填充了部分数字,其中缺少的数字用 . 表示. 样例 下列就是一个合法数独的样例. 注意 一个合法的数独(仅部分填充)并不一定是可解的.我们 ...
- valid number 判断字符串是否为有效数字
RT,面试题,给定一个字符串判断是否为科学计数法的有效数字.此题各种繁琐考虑.今天终于学会了用有限状态机来处理.理解之后简洁易懂.在此分享我的理解推导思路,大有收获啊. 网上有解法说先记录每个状态代表 ...
- 65. Valid Number 判断字符串是不是数字
[抄题]: Validate if a given string is numeric. Some examples:"0" => true" 0.1 " ...
- Bestcoder BestCoder Round #28 A Missing number(查找缺失的合法数字)
Problem Description There is a permutation without two numbers in it, and now you know what numbers ...
- C# 判断一字符串是否为合法数字(正则表达式)
判断一个字符串是否为合法整数(不限制长度) public static bool IsInteger(string s) { string pattern = @"^\d*$"; ...
- [LintCode] Valid Number 验证数字
Validate if a given string is numeric. Have you met this question in a real interview? Yes Example & ...
- LintCode389.判断数独是否合法
LintCode简单题:判断数独是否合法 问题描述: 请判定一个数独是否有效. 该数独可能只填充了部分数字,其中缺少的数字用 . 表示. 注意事项: 一个合法的数独(仅部分填充)并不一定是可解的.我们 ...
- 判断数独是否合法(LintCode)
判断数独是否合法 请判定一个数独是否有效. 该数独可能只填充了部分数字,其中缺少的数字用. 表示. 样例 下列就是一个合法数独的样例. 注意 一个合法的数独(仅部分填充)并不一定是可解的.我们仅需使填 ...
- 【leetcode】Valid Number
Valid Number Validate if a given string is numeric. Some examples:"0" => true" 0.1 ...
随机推荐
- GetDesktopWindow和GetWindow区别
GetWindow The GetWindow function retrieves a handle to a window that has the specified relationship ...
- VC程序只运行一个实例,并在打开多个时激活原窗口
(一)单文档应用程序 1.在应用程序类C~~App::InitInstance()函数中判断是否已有一个应用程序实例正在运行 BOOL C~~App::InitInstance() { . ...
- Django框架视图类
类视图 在写视图的时候,Django除了使用函数作为视图,也可以使用类作为视图.使用类视图可以使用类的一些特性,比如继承等. View django.views.generic.base.View是主 ...
- linux下Tomcat shutdown无效
问题: linux下Tomcat shutdown无效 linux下关闭tomcat后,发现重新启动Tomcat后.port号提示被占用, 原因: 这时可能是项目中的后台线程或者socket依旧在执行 ...
- java开发的zimg客户端
1.zimg的安装部署 最开始的时候是下载zimg的源码安装的,由于zimg依赖项众多,没有安装成功,刚好那期间在学习docker,于是docker search zimg一下,惊奇的发现有zimg镜 ...
- sql查询原理和Select执行顺序
一 sql语句的执行步骤 1)语法分析,分析语句的语法是否符合规范,衡量语句中各表达式的意义. 2) 语义分析,检查语句中涉及的所有数据库对象是否存在,且用户有相应的权限. 3)视图转换,将涉及视图的 ...
- Java AES512加密算法
AES - 高级加密标准: 高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这 ...
- ansible安装及使用
一.ansible介绍 1.ansible简介 官方的title是“Ansible is Simple IT Automation”——简单的自动化IT工具. Ansible跟其他IT自动化技术的区别 ...
- Ubuntu14.04安装QT5.5
1.进入qt目录下,修改qt安装文件属性 2:执行./qt-opensource-linux-xXXX; 3.启动Qt Creater:进入Qt5./Tools/QtCreater/bin/,可以鼠标 ...
- SCSS入门
1. CSS预处理器 定义了一种新的专门的编程语言,编译后成正常的CSS文件.为CSS增加一些编程的特性,无需考虑浏览器的兼容问题,让CSS更加简洁,适应性更强,可读性更佳,更易于代码的维护等诸多好处 ...