Valid Number leetcode java
2024-09-15 16:36:22
题目:
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.
题解:
正则表达式。
本文代码引用自:http://blog.csdn.net/fightforyourdream/article/details/12900751
代码:
1 public boolean isNumber(String s) {
2 if(s.trim().isEmpty()){
3 return false;
4 }
5 String regex = "[-+]?(\\d+\\.?|\\.\\d+)\\d*(e[-+]?\\d+)?";
6 if(s.trim().matches(regex)){
7 return true;
8 }else{
9 return false;
}
}
如果按照判断的方法可以如下:
1 public static boolean isNumber(String s) {
2 int i = 0;
3 while(s.charAt(i) == ' '){ // 移除前导whitespace
4 i++;
5 if(i >= s.length()){
6 return false;
7 }
8 }
9 if(s.charAt(i)=='+' || s.charAt(i)=='-'){ // 忽略符号位
i++;
}
int j = s.length()-1;
while(s.charAt(j) == ' '){ // 移除后缀whitespace
j--;
}
if(i <= j){
s = s.substring(i, j+1);
}else{
return false;
}
int dot = -1; // 记录点的位置
int ee = -1; // 记录e的位置
for(i=0; i<s.length(); i++){
if(dot==-1 && s.charAt(i)=='.'){
dot = i;
}else if(ee==-1 && s.charAt(i)=='e'){
ee = i;
if(i+1<s.length() && (s.charAt(i+1)=='-' || s.charAt(i+1)=='+')){
i++;
}
}else{
if(Character.isDigit(s.charAt(i))){
continue;
}else{
return false;
}
}
}
//xxx.xxexx
String startStr, midStr, lastStr;
if(dot==-1 && ee==-1){ //xxx
startStr = s; // xxx
if(startStr.length()<1){
return false;
}
}else if(dot!=-1 && ee==-1){ //xxx.yyy
startStr = s.substring(0, dot); // xxx
midStr = s.substring(dot+1); // yyy
if(startStr.length()<1 && midStr.length()<1){
return false;
}
}else if(dot==-1 && ee!=-1){ // xxxeyyy
startStr = s.substring(0, ee); // xxx
if(startStr.length()<1){
return false;
}
if(ee+1<s.length() && (s.charAt(ee+1)=='-' || s.charAt(ee+1)=='+')){ // xxxe-zz
lastStr = s.substring(ee+2); // zz
}else{
lastStr = s.substring(ee+1);
}
if(lastStr.length() < 1){
return false;
}
}else{ //xxx.yyezz
if(dot>ee){ // 位置不对
return false;
}
startStr = s.substring(0, dot); // xxx
midStr = s.substring(dot+1, ee); // yy
if(startStr.length()<1 && midStr.length()<1){
return false;
}
if(ee+1<s.length() && (s.charAt(ee+1)=='-' || s.charAt(ee+1)=='+')){
lastStr = s.substring(ee+2); // zz
}else{
lastStr = s.substring(ee+1);
}
if(lastStr.length() < 1){
return false;
}
}
return true;
}
最新文章
- “我爱背单词”beta版发布与使用说明
- validate插件深入学习-01 小白从看透一个插件开始
- 配置ConvenientBanner时出现的问题
- js map
- PE渲染引擎 三
- php 错误 Strict Standards: PHP Strict Standards: Declaration of .... should be compatible with that of 解决办法
- Mac osx 下配置ANT
- 以中断方式实现1s定时
- (转载)delphi 把图片存入数据库
- [Audio processing] FFMPEG转音频格式和采样率
- VirtualBox 复制vdi文件和修改vdi的uuid
- Dockerfile Volume指令与docker -v的区别
- saltstack高效运维
- Python 3.6安装yaml时报";AttributeError: module &#39;pip&#39; has no attribute &#39;main&#39;";和“Non-zero exit code”错误
- 基于物理的渲染—HDR Tone Mapping
- LabVIEW(十四):VI属性
- DNS(bind)服务器安装和配置
- 20172308《Java软件结构与数据结构》第四周学习总结
- yii2 restful api——app接口编程实例
- Java Web 从入门到精通(明日科技)
热门文章
- 整理c# 不常用但有用代码
- CSS动画简介
- 让Win2008+IIS7+ASP.NET支持10万并发请求
- .net中实现RSS方法
- PHP 依赖注入(DI) 和 控制反转(IoC)
- .Net Discovery系列之十-深入理解平台机制与性能影响(上)
- Android:Attribute is missing the Android namespace prefix
- iPhone上将短信内容发送到指定邮箱的方法
- 【mybatis】mybatis进行批量更新,报错:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right
- 【MySQL】EXPLAIN命令详解