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的更多相关文章

  1. leetCode 65.Valid Number (有效数字)

    Valid Number  Validate if a given string is numeric. Some examples: "0" => true " ...

  2. 【leetcode】Valid Number

    Valid Number Validate if a given string is numeric. Some examples:"0" => true" 0.1 ...

  3. [leetcode]65. Valid Number 有效数值

    Validate if a given string can be interpreted as a decimal number. Some examples:"0" => ...

  4. [LeetCode] 65. Valid Number 验证数字

    Validate if a given string can be interpreted as a decimal number. Some examples:"0" => ...

  5. Java for LeetCode 202 Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  6. Java for LeetCode 179 Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  7. Java for LeetCode 036 Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  8. LeetCode 65 Valid Number

    (在队友怂恿下写了LeetCode上的一个水题) 传送门 Validate if a given string is numeric. Some examples: "0" =&g ...

  9. Java for LeetCode 137 Single Number II

    Given an array of integers, every element appears three times except for one. Find that single one. ...

随机推荐

  1. spring 第一篇(1-1):让java开发变得更简单(下)转

    spring 第一篇(1-1):让java开发变得更简单(下) 这个波主虽然只发了几篇,但是写的很好 上面一篇文章写的很好,其中提及到了Spring的jdbcTemplate,templet方式我之前 ...

  2. C#获取局域网中的所有正在使用的IP地址

    方法不是很好. using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

  3. ubuntu 14.04 vim install youcompleteme

    sudo apt-get install vim ; sudo apt-get install vim-youcompleteme ; sudo apt-get install vim-addon-m ...

  4. threading使用心得

    需求:监控多个重要网站(多线程),出现访问异常重试2次,第三次开始告警. 日志模块 import logging import logging.config # 日志配置 LOGCONF_FILENA ...

  5. 嵌入式实时操作系统μCOS原理与实践任务控制与时间的解析

    /*************************************************************************************************** ...

  6. Hibernate动态更新

    Hibernate一级缓存: Hibernate有一级和二级缓存.一级缓存就是Session. 在Hibernate启动的时候,SessionFactory解析映射文件的时候,会根据映射文件中的字段生 ...

  7. Java及Android开发环境搭建

    前言 自从接触java以来,配置环境变量折腾了好几次,也几次被搞得晕头转向,后来常常是上网查阅相关资料才解决.但是过一段时间后一些细节就会记不清了,当要在其他机子上配置时又得上网查或者查阅相关书籍,如 ...

  8. 终端terminal配色

    切换到~/.bash_profile,无此.bash_profile文件可创建 在文件内粘入以下代码: #enables colorin the terminal bash shell export ...

  9. UILable点击事件

    UILabel *lLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 19, 105, 1)]; lLabel.backgroundColor ...

  10. Linux SSH安全策略限制IP登录方法(转)

    本文介绍了Linux SSH安全策略限制IP登录的两种方法.具体如下: 方法一: 首先需要限制登录的ip(或者如果需要自己本地登录,查看最后登录ip即可) Vim /etc/hosts.allow 输 ...