Java for LeetCode 065 Valid Number
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.
解题思路:
本题边界条件颇多"1.", ".34","1.1e+.1"也是正确的。解题方法是先trim一下,然后按照e进行划分,然后分别对e前后进行判断JAVA实现如下:
public boolean isNumber(String s) {
s = s.trim();
String[] splitArr = s.split("e");
if (s.length() == 0 || s.charAt(0) == 'e'
|| s.charAt(s.length() - 1) == 'e' || splitArr.length > 2)
return false;
for (int k = 0; k < splitArr.length; k++) {
String str = splitArr[k];
boolean isDecimal = false;
if (str.charAt(0) == '-' || str.charAt(0) == '+')
str = str.substring(1);
if (str.length() == 0)
return false;
for (int i = 0; i < str.length(); i++) {
if ('0' <= str.charAt(i) && str.charAt(i) <= '9')
continue;
else if (str.charAt(i) == '.' && !isDecimal) {
if (k == 0 && str.length() > 1)
isDecimal = true;
else
return false;
} else
return false;
}
}
return true;
}
Java for LeetCode 065 Valid Number的更多相关文章
- leetCode 65.Valid Number (有效数字)
Valid Number Validate if a given string is numeric. Some examples: "0" => true " ...
- 【leetcode】Valid Number
Valid Number Validate if a given string is numeric. Some examples:"0" => true" 0.1 ...
- [leetcode]65. Valid Number 有效数值
Validate if a given string can be interpreted as a decimal number. Some examples:"0" => ...
- [LeetCode] 65. Valid Number 验证数字
Validate if a given string can be interpreted as a decimal number. Some examples:"0" => ...
- Java for LeetCode 202 Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- Java for LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- Java for LeetCode 036 Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- LeetCode 65 Valid Number
(在队友怂恿下写了LeetCode上的一个水题) 传送门 Validate if a given string is numeric. Some examples: "0" =&g ...
- Java for LeetCode 137 Single Number II
Given an array of integers, every element appears three times except for one. Find that single one. ...
随机推荐
- C语言中常用的string.h的字符函数
strcmp 字符串比较函数 原型: int strcmp(char *str1, char *str2); 例子: ) printf("buffer 1 is greater than b ...
- javaScript基础练习题-下拉框制作(JQuery)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 【poj2186】 Popular Cows
http://poj.org/problem?id=2186 (题目链接) 题意 给出一个n个点m条边的有向图,求其中没有出度强连通分量所包含的点有几个 Solution 其实这道题的题解已经在“题意 ...
- 设置div居中
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- ubuntu安装spark on yarn
安装spark 安装hadoop 安装ssh,调试免密钥登录 配置hadoop 配置yarn 测试
- Android学习笔记02-Mac下编译java代码
在Mac OS上配置JDK 1.7. 一 下载 Mac版本的JDK1.7 从以下下载地址,下载Mac版本的JDk1.7 安装文件 jdk-7u79-macosx-x64.dmg. http://www ...
- 02 C语言指针
今天发帖记录自己学习C语言精髓的心理历程,人生就像是一次旅途,沿途总是能看到最美的风景,让我们的思想相逢在C语言中. 一 初识指针,指针的定义 指针是C语言中的一种类型,类似于整形,字符型等.既然C指 ...
- SCU 4424(求子集排列数)
A - A Time Limit:0MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Practice ...
- API与软件架构
http://blog.csdn.net/horkychen/article/details/46612899 从架构设计的角度来看(所谓的组成论),软件系统就是模块和接口. 模块(层次/组件)决定分 ...
- jquery插件实现上下滑动翻页效果
<!DOCTYPE > <meta charset="utf-8" /> <head> <title>测试jquery</ti ...