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.



Update (2015-02-10):

The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button  to reset your code definition.

思路:此题是真的有些难度的,并且最重要的是我根本就不清楚一个有效数字的规则。。

一个连规则都不知道的人怎么能玩好游戏呢,所以果断的挂了10次8次的。最后还是没有全然答对。

临时把别人的代码拿出来放在以下。有时间再好好研究一下:

public class Solution {
public boolean isNumber(String s) {
// Start typing your Java solution below
// DO NOT write main() function
/* "0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"+-2e10" => true
//*/
//check input.
if(s==null || s.length()==0) return false;
int sz = s.length();
int i=0; while(i<sz && s.charAt(i)==' ') ++i; boolean space = false;
boolean exp = false;
boolean dot = false;
boolean number = false;
boolean neg = false; for(; i<sz; i++) {
char c = s.charAt(i);
if(c==' ') {
space = true;
} else if(space==true) {
return false;
} else if( (c=='e' || c=='E') && exp==false && number==true) {
exp = true;
number = false;
dot = true;
neg = false;
} else if( c=='.' && dot==false) {
dot = true;
neg = true;
// number = false;
} else if( c>='0' && c<='9') {
number = true;
} else if((c=='+' || c=='-') && neg==false && number==false ) {
neg = true;
} else {
return false;
}
}
return number;
}
}

leetCode 65.Valid Number (有效数字)的更多相关文章

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

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

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

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

  3. LeetCode 65 Valid Number

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

  4. Leetcode 65 Valid Number 字符串处理

    由于老是更新简单题,我已经醉了,所以今天直接上一道通过率最低的题. 题意:判断字符串是否是一个合法的数字 定义有符号的数字是(n),无符号的数字是(un),有符号的兼容无符号的 合法的数字只有下列几种 ...

  5. [LeetCode] 65. Valid Number(多个标志位)

    [思路]该题题干不是很明确,只能根据用例来理解什么样的字符串才是符合题意的,本题关键在于几个标志位的设立,将字符串分为几个部分,代码如下: class Solution { public: strin ...

  6. 【LeetCode】65. Valid Number

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

  7. 【leetcode】Valid Number

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

  8. 【一天一道LeetCode】#65. Valid Number

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Validat ...

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

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

随机推荐

  1. 面向对象之多态,property

    多态: 同一种事物有多种状态 多态性: 在不考虑对象具体类型的前提下直接调用对象下的方法 静态多态性和动态多态性 静态多态性:都可以进行+操作 动态多态性:不考虑对象具体类型调用方法 多态的好处: ① ...

  2. 链式链表的C风格实现

    头文件: #ifndef _LINKLIST_H_ #define _LINKLIST_H_ typedef void LinkList; //将数据的类型分离,相当于句柄 //只是一个小节点 包含着 ...

  3. tomcat 修改默认端口8080 为 80端口

    首先,找到你的安装目录,如图: 打开server.xml文件,找到8080,如图: 将 8080  改成你想要的端口,如 80 即可.改完后,记得要重启tomcat! 将端口改成 80 后,访问就不需 ...

  4. Httpclient 和 HtmlUnit 对比

    unit相比于client更接近浏览器,模拟浏览器访问状态,两者都是将网页封装成了一个对象,不同是,client能更好地操作网页元素. but 官方unit已经很老了,08年的,已经不更新了.

  5. x86 保护方式 简介 一

    80386   三种工作方式   实模式    保护模式和虚拟86模式   只有在保护方式下  全部32条地址线才有效   可以寻址高达4g字节的物理地址空间 超过1m的内存空间  被成为扩展的内存空 ...

  6. CSAPP学习笔记—虚拟内存

    CSAPP学习笔记—虚拟内存 符号说明 虚拟内存地址寻址 图9-12展示了MMU如何利用页表来实现这种映射.CPU中的一个控制寄存器,页表基址寄存器(Page Table Base Register, ...

  7. MHA脚本master_ip_failover.pl(三)

    #!/usr/bin/env perl use strict;use warnings FATAL => 'all'; use Getopt::Long; my ( $command, $ssh ...

  8. BZOJ 4004 [JLOI2015]装备购买 ——线性基

    [题目分析] 题目很简单,就是要维护一个实数域上的线性基. 仿照异或空间的线性基的方法,排序之后每次加入一个数即可. 卡精度,开long double 和 1e-6就轻松水过了. [代码] #incl ...

  9. poj 2318 向量的叉积二分查找

    TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9350   Accepted: 4451 Description ...

  10. java面

    常被问到的十个 Java 面试题 每周 10 道 Java 面试题 : 面向对象, 类加载器, JDBC, Spring 基础概念 Java 面试题问与答:编译时与运行时 java面试基础1 java ...