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. poj1733Parity game

    Parity game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7288   Accepted: 2833 Descr ...

  2. Weka算法Classifier-tree-J48源代码分析(一个)基本数据结构和算法

    大约一年,我没有照顾的博客,再次拿起笔不知从何写上,想来想去手从最近使用Weka要正确书写. Weka为一个Java基础上的机器学习工具.上手简单,并提供图形化界面.提供如分类.聚类.频繁项挖掘等工具 ...

  3. ppa安装php版本

    如果你想安装PHP的特定版本,那么这篇文章可以帮助你.这篇文章将帮助您安装PHP 5.4和PHP 5.5 PHP 5.6,通过使用PPA在Ubuntu 15.10 LTS,14.04或12.04 LT ...

  4. 动态下载 Yahoo 网络数据存入 Microsoft SQL Server 再 Matlab 调用的一个完整例子

    % 编程环境: Matlab 2014a, win7 32bit, Microsoft SQL Server 2008r2 %% % 清屏 clc; clear all; close all; %% ...

  5. Struts2中基于Annotation的细粒度权限控制

    Struts2中基于Annotation的细粒度权限控制 2009-10-19 14:25:53|  分类: Struts2 |  标签: |字号大中小 订阅     权限控制是保护系统安全运行很重要 ...

  6. 前端框架react研究

    摘要: 最近公司要做一个嵌套在app中的应用,考虑着用Facebook的react来开发view,所以就研究了下.下面是我在开发中遇到的坑,希望能给你帮助. 项目地址:https://github.c ...

  7. HTML 标准属性 和 事件属性

    HTML的公共属性 HTML 和 XHTML 标签支持的标准属性 核心属性 (Core Attributes) 以下标签不提供下面的属性:base.head.html.meta.param.scrip ...

  8. XML做下拉列表

    5-18X.php主页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http: ...

  9. 自定义 array_map() 对应的递归函数 array_map_recursive()

    array_walk 有个原生递归函数 array_walk_recursive($arr, 'function', 'words'),但是 array_map 却没有对应的递归函数 array_ma ...

  10. HttpWatch详解

    一 概述: HttpWatch强大的网页数据分析工具.集成在Internet Explorer工具栏.包括网页摘要.Cookies管理.缓存管理.消息头发送/接受.字符查询.POST 数据和目录管理功 ...