LeetCode No.65 ValidNumber
来源:互联网 发布:成都工业设计软件培训 编辑:程序博客网 时间:2024/05/22 06:48
/**Validate if a given string is numeric.Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => trueNote: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.*//**题意:判断一个字符串是否是计算机合法数字。思路:把字符串分三段(小数点前、小数点与e/E之间部分、e/E之后),这三段都必须为纯数字组成的字符串。注意,第一部分和第三部分可以带符号(如+12.34e-56),第一部分和第二部分可以有一部分为空(如“.2"或"2.")。去掉首尾多余的空格;去掉开头的正负号;看有没有e或E,如果有那么e/E后面只能是整数;再看e前面的部分有没有小数点,小数点前后两部分都必须为整数。*/public class Solution { public boolean isNumber(String s) { s = s.trim(); if (s.length() == 0) return false; if (s.charAt(0) == '+' || s.charAt(0) == '-') { s = s.substring(1); } int posee = 0; int pose = s.indexOf("e"); int posE = s.indexOf("E"); if((pose>=0)&&(posE>=0)) return false; else if((pose>=0)&&(posE==-1)) posee = pose; else if((pose==-1)&&(posE>=0)) posee = posE; if (posee != 0) { String poste = s.substring(posee + 1); if (poste.length() == 0) return false; if (poste.charAt(0) == '+' || poste.charAt(0) == '-') { poste = poste.substring(1); } if (!isPureDigit(poste)) return false; s = s.substring(0, posee); } int posdot = s.indexOf("."); if (posdot >= 0) { String predot = s.substring(0, posdot); String postdot = s.substring(posdot + 1); if (predot.isEmpty()) return isPureDigit(postdot); if (postdot.isEmpty()) return isPureDigit(predot); return isPureDigit(predot) && isPureDigit(postdot); } return isPureDigit(s); } public boolean isPureDigit(String s) { if (s.isEmpty()) return false; for (int i = 0; i < s.length(); i++) { if (!Character.isDigit(s.charAt(i))) return false; } return true; } }
0 0
- LeetCode No.65 ValidNumber
- 3.9 ValidNumber
- ValidNumber.ashx 验证码
- Leetcode NO.65 Valid Number
- LeetCode No.11 ~No.12
- Leetcode NO.78 Subsets
- Leetcode NO.77 Combinations
- Leetcode NO.46 Permutations
- 【leetcode】No. 235 LCABST
- LeetCode no. 35
- Leetcode no. 37
- Leetcode no. 279
- Leetcode no. 335
- Leetcode no. 283
- Leetcode no. 181 (sql)
- Leetcode no. 40
- Leetcode no. 238
- Leetcode no. 26
- LeetCode 110. Balanced Binary Tree 题解
- C#笔记(一)抽象方法、虚方法、接口
- Android 方形布局
- Java并发编程:线程池的使用(非常棒,通俗易懂)
- statement添加操作
- LeetCode No.65 ValidNumber
- 六自由度机械臂的驱动
- Spring学习总结(一)——Spring实现IoC的多种方式
- linux 下vi 命令使用
- python写网络爬虫:爬取糗事百科上的段子
- Webpack简单介绍
- Ret2Libc 攻击技术
- 如何在Activity中获取和设置控件的宽高
- Mysql开发规范