[leetcode]65. Valid Number 有效数值
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 有效数值的更多相关文章
- leetCode 65.Valid Number (有效数字)
Valid Number Validate if a given string is numeric. Some examples: "0" => true " ...
- [LeetCode] 65. Valid Number 验证数字
Validate if a given string can be interpreted as a decimal number. Some examples:"0" => ...
- LeetCode 65 Valid Number
(在队友怂恿下写了LeetCode上的一个水题) 传送门 Validate if a given string is numeric. Some examples: "0" =&g ...
- Leetcode 65 Valid Number 字符串处理
由于老是更新简单题,我已经醉了,所以今天直接上一道通过率最低的题. 题意:判断字符串是否是一个合法的数字 定义有符号的数字是(n),无符号的数字是(un),有符号的兼容无符号的 合法的数字只有下列几种 ...
- [LeetCode] 65. Valid Number(多个标志位)
[思路]该题题干不是很明确,只能根据用例来理解什么样的字符串才是符合题意的,本题关键在于几个标志位的设立,将字符串分为几个部分,代码如下: class Solution { public: strin ...
- 【LeetCode】65. Valid Number
Difficulty: Hard More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...
- 【leetcode】Valid Number
Valid Number Validate if a given string is numeric. Some examples:"0" => true" 0.1 ...
- 【一天一道LeetCode】#65. Valid Number
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Validat ...
- 65. Valid Number
题目: Validate if a given string is numeric. Some examples:"0" => true" 0.1 " = ...
随机推荐
- The problems when using a new ubuntu 18.04
how to install dual systems (windows & ubuntu) Donwloading the ubuntu from web. Using refu to cr ...
- 集合总结五(Hashtable的实现原理)
一.概述 上一篇介绍了Java8的HashMap,接下来准备介绍一下Hashtable. Hashtable可以说已经具有一定的历史了,现在也很少使用到Hashtable了,更多的是使用HashMap ...
- Python 语言之 map/reduce
1.相关文献 大名鼎鼎的Google论文<MapReduce: Simplified Data Processing on Large Clusters> 对应的中文翻译<MapRe ...
- MySQL 批量添加
自己封装的一个批量添加. $data 是一个二维数组.key对应是数据表的字段名: /** * 批量创建 * @param array $data * @return int $res 影响行 * @ ...
- servlet-jsp-EL 表达式
jsp--EL表达式 jsp表达式<%= %>用于向页面中输出一个对象.jsp2.0时在页面中不允许出现jsp表达式和脚本片段,于是使用EL表达式来代替jsp表达式,标签代替脚本片段 基本 ...
- 物化视图SQL
物化视图SQL,如果没有结果集,就证明数据库中不存在物化视图 select a.owner,'' column_name,a.table_name,b.segment_name,b.segment_t ...
- NAP(Network Access Protection)
- [SQL]sql server中如何直接查询存储过程EXEC返回的结果集?
Declare @T Table (iDay VARCHAR(),iNum DECIMAL(,),yuxiang DECIMAL(,)) Insert @T --EXEC [dbo].[BSP0101 ...
- 关于CPU CACHE工作机制的学习
转自:http://blog.csdn.net/notbaron/article/details/48143409 1. 存储层次结构 由于两个不谋而合的因素如下: l 硬件:由于不同存储技术的访 ...
- 5-安装sqoop
1.解压,修改权限 sudo tar -zvxf sqoop-1.4.6.bin__hadoop-2.0.4-alpha.tar.gz -C /opt/app/ sudo chown -R hadoo ...