Valid Number leetcode java
题目:
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;
}
Valid Number leetcode java的更多相关文章
- Letter Combinations of a Phone Number leetcode java
题目: Given a digit string, return all possible letter combinations that the number could represent. A ...
- Ugly Number leetcode java
问题描述: Write a program to check whether a given number is an ugly number. Ugly numbers are positive n ...
- Single Number leetcode java
问题描述: Given an array of integers, every element appears twice except for one. Find that single one. ...
- Palindrome Number leetcode java
题目: Determine whether an integer is a palindrome. Do this without extra space. click to show spoiler ...
- Valid Sudoku leetcode java
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- Valid Palindrome leetcode java
题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...
- Longest Valid Parentheses leetcode java
题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...
- Valid Parentheses leetcode java
题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
- 【LeetCode】65. Valid Number
Difficulty: Hard More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...
随机推荐
- Centos 7.4下 部署openstack Queens 计算节点qemu高版本问题
sed -i 's/$contentdir/centos/g' /etc/yum.repos.d/CentOS-QEMU-EV.repo 这样既可正常安装compute服务
- [CC-XXOR]Chef and Easy Problem
[CC-XXOR]Chef and Easy Problem 题目大意: 给你一个长度为\(n(n\le10^5)\)的序列\(A(A_i<2^{31})\).\(m(m\le10^5)\)次询 ...
- 吴恩达-coursera-机器学习-week8
十三.聚类(Clustering) 13.1 无监督学习:简介 13.2 K-均值算法 13.3 优化目标 13.4 随机初始化 13.5 选择聚类数 十四.降维(Dimensionality Red ...
- Lua打印table树形结构
--这是quick中的工具,作用就是打印Lua中强大的table的结构, 当table的嵌套层级比较多的时候,这个工具非常方便,开发中必备的工具.--具体使用方法:local debug = requ ...
- Ural 2045. Richness of words 打表找规律
2045. Richness of words 题目连接: http://acm.timus.ru/problem.aspx?space=1&num=2045 Description For ...
- IBM MR10i阵列卡配置Raid0/Raid1/Raid5(转)
RAID5配置: 其实RAID0/RAID1都基本一致,只是选择的类型不同. 1. 开机看到ctrl+h的提示按下相应的键,等ServerRaid 10-i卡初始化完成则进入WebBIOS 配置界面: ...
- Redis源代码分析(三十五)--- redis.c服务端的实现分析(2)
在Redis服务端的代码量真的是比較大,假设一个一个API的学习怎么实现,无疑是一种效率非常低的做法,所以我今天对服务端的实现代码的学习,重在他的运行流程上.而对于他的模块设计在上一篇中我已经分析过了 ...
- NSURLRequest with UserAgent
关于iOS上的http请求还在不断学习,从早先的时候发现原来iOS的http请求可以自动保存cookie到后来的,发现ASIHttpRequest会有User-Agent,到现在发现竟然NSURLRe ...
- 【Devops】【docker】【CI/CD】Jenkins自动安装JDK需要提供Oracle的账号密码,否则报错:Unable ro auto-install JDK until the license is accepted
Jenkins自动安装JDK需要提供Oracle的账号密码,否则报错:Unable ro auto-install JDK until the license is accepted 解决方法: ...
- WordPress主题开发:开启侧边栏小工具功能
步骤一:在主题的functions.php中,添加一段代码,开启侧边栏功能,代码如下: <?php //参数 $args = array( 'name' => __( '主侧边栏'), ' ...