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 ...
随机推荐
- Idea之Tomcat
安装配置 启动 三种方式,run,debug,coverage 面板说明 上面一排 Debugger:debug模式的时候显示方法,调用关系,参数值等, Server:打印日志 ...
- grep、egrep命令用法
何谓正则表达式 正则表达式,又称正规表示法.常规表示法(Regular Expression,在代码中常简写为regex.regexp或RE),是一类字符所书写的模式,其中许多字符不表示其字面意义,而 ...
- iOS论App推送方案
1.APNS介绍(原生推送实现原理) 在iOS平台上,大部分应用是不允许在后台运行并连接网络的.在应用没有被运行的时候,只能通过 Apple Push Notification Service (AP ...
- FW qunit introduction
自动化测试软件对于开发来说是一个很重要的工具,而单元测试对于自动化测试来说是基本组成部分:软件的每一个组件或者单元可以在非人工介入的情况下,使用测试工具一遍遍的重复执行.换句话说,就是你可以写一次测试 ...
- Java 常用语法和数据结构
Collection 首先Java中的collection都是支持泛型和类型安全 由于Java单根继承, 所以不指定, 可以在collection里面放任何对象, collection会都当作obje ...
- Storm-源码分析- bolt (backtype.storm.task)
Bolt关键的接口为execute, Tuple的真正处理逻辑, 通过OutputCollector.emit发出新的tuples, 调用ack或fail处理的tuple /** * An IBolt ...
- 第05章—Swagger2打造在线接口文档
spring boot 系列学习记录:http://www.cnblogs.com/jinxiaohang/p/8111057.html 码云源码地址:https://gitee.com/jinxia ...
- cookies与session
一.cookies 本质:浏览器端保存的键值对 方便客户按照自己的习惯操作页面或软件,例如:用户验证,登陆界面,右侧菜单隐藏,控制页面列表显示条数... cookies是由服务端写在浏览器端,以后每次 ...
- django模板复用 extends,block,include
template复用 extends block include render 参考:https://code.ziqiangxuetang.com/django/django-template.ht ...
- 【我的Android进阶之旅】Android Studio如何轻松整理字符串到string.xml中
使用Android Studio一段时间了,还有很多小技巧没有掌握.比如:平常将字符串整理到string.xml中,都是手动的去复制字符串到string.xml中,然后再回来修改引用该字符串的代码,这 ...