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 ...
随机推荐
- Xamarin 2017.11.9更新
Xamarin 2017.11.9更新 本次更新主要针对Xamarin.iOS,适配了iOS 11.1和Xcode 9.1.Visual Studio 2017升级到15.4.3获得新功能.Visu ...
- 捕获程序异常之tryCatch
一.try catch语法try…catch…finally…语法中除了try以外,catch和finally都是可选的(两者必须要有一个),也就是说try…catch…finally…语法有以下三种 ...
- 【tarjan+SPFA】BZOJ1179-[Apio2009]Atm
[题目大意] 给出一张有点权的有向图,已知起点和可以作为终点的一些点,问由起点出发,每条边和每个点可以经过任意多次,经过点的权值总和最大为多少. [思路] 由于可以走任意多次,显然强连通分量可以缩点. ...
- 深入理解指针—>指针函数与函数指针的区别
一. 在学习过程中发现这"指针函数"与"函数指针"容易搞错,所以今天,我自己想一次把它搞清楚,找了一些资料,首先它们之间的定义: 1.指针函数是指带指针的函数, ...
- STM32F4 Timer External Clock TI2 Both Edges Demo
#define CLK_FREQ ( 10000 ) #define CORE_FREQ ( 168000000 ) static void TIM_GPIO_Config( void ) { GPI ...
- STM32输入捕获模式设置并用DMA接收数据
参考: STM32的PWM输入模式设置并用DMA接收数据 Input capture mode The input stage samples the corresponding TIx input ...
- STM32F4 -- How to use the DMA burst feature
Bits 15:13 Reserved, must be kept at reset value. Bits 12:8 DBL[4:0]: DMA burst length This 5-bit ve ...
- Mui --- app与服务器之间的交互原理、mui ajax使用
1.APP与服务器之间的交互原理 app端(客户端)与服务端的交互其实理解起来和容易,客户端想服务器端发送请求,服务器端进行数据运算后返回最终结果.结果可以是多种格式: 1.text 文本格式 2.x ...
- RDMA over TCP的协议栈工作过程浅析
http://blog.chinaunix.net/uid-140205-id-2849342.html
- wordpress入门
安装bitnami wordpress. 打开仪表盘:开始菜单--Bitnami Wordpress协议栈 Manager Tool -- Go to Appllication -- Access W ...