[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 " = ...
随机推荐
- Java_集合_ArrayLish Comparator比较排序 小笔记
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; class Teacher ...
- Java面向对象 第4节 类的多态性
一.多态的概念 在面向对象语言中,多态是指在一棵继承树中的类中可以有多个同名但不同方法体及不同形参的方法.通常有两种途径实现多态:方法的重载和覆盖. 多态性允许以统一的风格处理已存在的变量及相关的类. ...
- Postgresql导出数据报版本不对
zabbix使用得数据库是Postgresql,最近zabbix4.0版本出来了,准备把zabbix升级,得先把数据库备份,但是一直报错,如下: 查找服务器上是否有10的版本,也一直没找到 ...
- 搭建自己的Docker registry(五)
弄了一天,在网上查了很多资料,感觉都好复杂好复杂,一步一步踩坑踩出来就好了. 服务器:阿里云(香港) 环境:CentOS Linux release 7.4.1708 (Core) Docker:1. ...
- spark submit参数调优
在开发完Spark作业之后,就该为作业配置合适的资源了.Spark的资源参数,基本都可以在spark-submit命令中作为参数设置.很多Spark初学者,通常不知道该设置哪些必要的参数,以及如何设置 ...
- ClientDataSet
TField对象的SetText和GetText事件处理函数 使用TField对象的SetText和GetText事件处理函数可方便的解决字段的代码与代码所对应值的显示问题 TSimpleDatase ...
- Vista的MBR磁盘签名(Disk Signature) (转帖)
原帖:Vista的MBR磁盘签名(Disk Signature)_存梦_新浪博客 http://blog.sina.com.cn/s/blog_6fed14220100qq71.html 存梦发表于( ...
- django 多线程下载图片
example1: from multiprocessing.dummy import Pool as ThreadPool #多线程 import time import urllib2 urls ...
- docker 批量删除
杀死所有正在运行的容器docker kill $(docker ps -a -q) 删除所有已经停止的容器docker rm $(docker ps -a -q) 删除所有未打 dangling 标签 ...
- centos6.5 yum安装redis
1.yum添加epel源 yum install epel-release 2.安装yum yum install redis 3.Redis 服务端配置——Could not connect to ...