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.

原题链接:https://oj.leetcode.com/problems/valid-number/

推断字符串是否是数字。

规则:出现+, - 则必须是第一个。或前一个是e;有. 则是小数,之前不可有.e;有e。则前面要有.,不能有e,而且后面要有.

可用正則表達式来解答。

	public static boolean isNumber(String s) {
String reg = "[+-]?(\\d+\\.?|\\.\\d+)\\d*(e[+-]?\\d+)? ";
return s.trim().matches(reg);
}

LeetCode——Valid Number的更多相关文章

  1. LeetCode: Valid Number 解题报告

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

  2. [LeetCode] Valid Number 验证数字

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

  3. [leetcode]Valid Number @ Python

    原题地址:http://oj.leetcode.com/problems/valid-number/ 题意:判断输入的字符串是否是合法的数. 解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较 ...

  4. Leetcode Valid Number

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

  5. LeetCode Valid Number 有效数字(有限自动机)

    题意:判断一个字符串是否是一个合法的数字,包括正负浮点数和整形. 思路:有限自动机可以做,画个图再写程序就可以解决啦,只是实现起来代码的长短而已. 下面取巧来解决,分情况讨论: (1)整数 (2)浮点 ...

  6. leetcode - valid number 正则表达式解法

    import java.util.regex.Pattern; public class Solution { Pattern p = Pattern.compile("^[\\+\\-]? ...

  7. [LeetCode] Valid Number 确认是否为数值

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

  8. 【LeetCode】65. Valid Number

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...

  9. 【leetcode】Valid Number

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

随机推荐

  1. [转]Android监听ListView里Button事件

    本文转自:http://blog.csdn.net/lovediji/article/details/6753349 public View getView(int position, View co ...

  2. 如何让win32 c++窗口不出现在任务栏

    把窗口作为某一个窗口的子窗口,然后设置WS_POPUP就可以了.使用CreateWindow时的第三个参数设置为WS_CHILD|WS_POPUP.

  3. DOM对象之window

    window的属性 top:返回当前窗口的最顶层的先辈窗口 document:返回HTML文档对象 location:当前窗口的地址 self:返回对自身窗口的引用 parent:返回父窗口 如何引用 ...

  4. js的replace, 高亮

    ";console.log(str.replace(/\,/g, "")); //输出 123 ";console.log(str);//输出123 " ...

  5. 利用 CSS animation 和 CSS sprite 制作动画

    CSS3 大大强化了制作动画的能力,但是如果要做出图案比较复杂的动画,选择 GIF 依然是一个不错的选择.今天给大家介绍一个使用 CSS animation 配合雪碧图(CSS sprite)来制作动 ...

  6. 用python写一个百度翻译

    运行环境: python 3.6.0 今天处于练习的目的,就用 python 写了一个百度翻译,是如何做到的呢,其实呢就是拿到接口,通过这个接口去访问,不过中间确实是出现了点问题,不过都解决掉了 先晾 ...

  7. 模态框(layer)

    推荐一个好看的模态框(layer)   地址:http://layer.layui.com/ 相应列子及配置  全部来自于官网,可直接访问官网学习了解. //信息框-例1 layer.alert('见 ...

  8. css 样式 解释

    字体属性:(font) 大小 {font-size: x-large;}(特大) xx-small;(极小) 一般中文用不到,只要用数值就可以,单位:PX.PD 样式 {font-style: obl ...

  9. JavaScript day4(逻辑运算符)

    逻辑运算符 逻辑运算符用于测定变量或值之间的逻辑 逻辑与运算符:&&.同时满足(and). 通过if语句的嵌套来实现: if (num > 5) { if (num < 1 ...

  10. 阅读《JavaScript设计模式》第二章心得

    面向对象编程 面向对象编程就是将你的需求抽象成一个对象.然后针对这个对象分析其特征(属性)与动作(方法).这个对象我们称之为类.面向对象编程思想其中的一个特点就是封装. 1.私有属性.私有方法.特权方 ...