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 ...
随机推荐
- Linux设备驱动之USB
Linux驱动框架分析(一) 事实上,Linux的设备驱动都遵循一个惯例——表征驱动程序(用driver更贴切一些,应该称为驱动器比较好吧)的结构体,结构体里面应该包含了驱动程序所需要的 ...
- python简单实现队列和栈push、pop操作
栈: # -*- coding: utf-8 -*- #定义序列 lst=[] def pop(): if(len(lst)==0): print"栈为空","无法出栈& ...
- HDU 5744 Keep On Movin 贪心
Keep On Movin 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5744 Description Professor Zhang has k ...
- 内核调试神器SystemTap 转摘
http://blog.csdn.net/zhangskd/article/details/25708441 https://sourceware.org/systemtap/wiki/WarStor ...
- Delphi2010 RTTI + Attribute 简单实现ORM实例
1.支持ORM,最基础的两个信息是表的信息和字段信息.这两个信息,如果用Attribute 来辅助,代码更简洁和可读性更好.可以把属性名当做真实字段名,也可以将特性里的属性当成真实姓名,再加上字段标题 ...
- myeclipse 2014 专业版 安装 svn插件
团队合作的项目肯定少不了版本控制,那么现在就看看myeclispe中是如何使用的吧. 开发环境:myeclipse 2014 java 8 tomcate 8 试了网上说的几种方法,都没有成功,最 ...
- 【python】安装Python 的IDE--PyCharm
[百度网盘-技术-pycharm破解需要的有安装包和破解jar] ================================================== 安装Tensorflow开发环境 ...
- windows下PHP不能开启pgsql扩展的解决方法
Tip: 环境 windows8.1 64位 + xampp1.8.1 + postgresql 9.3.6-2 第一步: php.ini中开启pgsql扩展 extension=php_pgsql ...
- VisualStudio:如何监控 ADO.NET?
背景 很多场景下我们都需要监控 ADO.NET,如:查看某些框架(ORM)生成的 SQL.如何在不能使用 SQL Profile 的情况下监控 SQL 呢?VS 为我们提供了一个工具,本文做一些介绍! ...
- 设置TOMCAT SESSIONID 字符长度和生成算法
修改TOMCAT 默认的生成SESSION ID的算法和字符长度非常简单,只需修改context.xml中的<Manager>标签值,比如: <Manager sessionIdLe ...