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次的。最后还是没有全然答对。

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

  1. public class Solution {
  2. public boolean isNumber(String s) {
  3. // Start typing your Java solution below
  4. // DO NOT write main() function
  5. /*
  6.  
  7. "0" => true
  8. " 0.1 " => true
  9. "abc" => false
  10. "1 a" => false
  11. "+-2e10" => true
  12. //*/
  13. //check input.
  14. if(s==null || s.length()==0) return false;
  15. int sz = s.length();
  16. int i=0;
  17.  
  18. while(i<sz && s.charAt(i)==' ') ++i;
  19.  
  20. boolean space = false;
  21. boolean exp = false;
  22. boolean dot = false;
  23. boolean number = false;
  24. boolean neg = false;
  25.  
  26. for(; i<sz; i++) {
  27. char c = s.charAt(i);
  28. if(c==' ') {
  29. space = true;
  30. } else if(space==true) {
  31. return false;
  32. } else if( (c=='e' || c=='E') && exp==false && number==true) {
  33. exp = true;
  34. number = false;
  35. dot = true;
  36. neg = false;
  37. } else if( c=='.' && dot==false) {
  38. dot = true;
  39. neg = true;
  40. // number = false;
  41. } else if( c>='0' && c<='9') {
  42. number = true;
  43. } else if((c=='+' || c=='-') && neg==false && number==false ) {
  44. neg = true;
  45. } else {
  46. return false;
  47. }
  48. }
  49. return number;
  50. }
  51. }

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. Vue.js—单元测试

    Vue.js--测试 这里采用的是Vue官方工具(Vue-CLI)搭建出来的项目,在这个搭建工具中推荐的两种测试分别是 端到端的测试 E2E 单元测试 Unit Test 端到端的测试(E2E) E2 ...

  2. js 类型之间的相互转化

    设置元素对象属性 var img = document.querySelector("img") img.setAttribute("src","ht ...

  3. wordcloud的安装报错 error: Microsoft Visual C++ 10.0 is required. Get it with "Microsoft Windows SDK 7.1"解决办法

    cmd中使用pip install wordcloud失败,没看懂报错的原因…… 而在pycharm中添加也报错 解决方法: 1. 下载wordcloud-1.4.1.tar.gz,解压缩 cmd c ...

  4. keypoint && DMatch

    下面单独介绍KEYPOINT 与DMatch的内在联系 std::vector<cv::Point2f> points1, points2; for (std::vector<cv: ...

  5. LeetCode(96) Unique Binary Search Trees

    题目 Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For exam ...

  6. CentOS下配置LVM和RAID

    1.CentOS配置LVM http://www.cnblogs.com/mchina/p/linux-centos-logical-volume-manager-lvm.html http://ww ...

  7. Spring 结构

    Spring框架主要由7大模块组成,它们提供了企业级开发需要的所有功能,而且每个模块都可以单独使用,也可以和其它模块组合使用,灵活且方便的部署可以使开发的程序更加简单灵活. 核心模块 Spring C ...

  8. [转]pickle python数据存储

    python的pickle模块实现了基本的数据序列和反序列化.通过pickle模块的序列化操作我们能够将程序中运行的对象信息保存到文件中去,永久存储:通过pickle模块的反序列化操作,我们能够从文件 ...

  9. tp 路径表示

    本文实例分析了thinkphp常见路径用法.分享给大家供大家参考.具体如下: 这里介绍的标签主要有: __root__ __self__ __action__ __url__ __app__ __pu ...

  10. Linux cp复制

    复制指定目录下的全部文件到另一个目录中文件及目录的复制是经常要用到的.linux下进行复制的命令为cp.假设复制源目录 为 dir1 ,目标目录为dir2.怎样才能将dir1下所有文件复制到dir2下 ...