leetcode_Valid Number
来源:互联网 发布:海关数据怎么查 编辑:程序博客网 时间:2024/05/19 18:16
描述:
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.
思路:
这道题目还是挺复杂的,复杂的原因是题目并没有给出有效数字的定义,比如说 .1 ,2.1e10.2 ,2e3.2 和2.1e5 有效,但10.1.1非法而这些数字的判断又是很麻烦的
代码:
public boolean isNumber(String s) {s = s.trim();if (s.startsWith("+"))s = s.substring(1);else if (s.startsWith("-"))s = s.substring(1);boolean isInterger = false;boolean isFloater = false;boolean isSciencer = false;if (isInt(s))isInterger = true;int indexFloat = -1;int indexScience1 = -1;int indexScience2 = -1;indexFloat = s.indexOf('.');indexScience1 = s.indexOf('e');indexScience2 = s.indexOf('E');String nums[] = null;if (indexFloat != -1) {int index1=s.indexOf('.');int index2=s.lastIndexOf('.');if(index1!=index2)isFloater=false;else isFloater = isFloat(s);}if (indexScience1 + indexScience2 != -2) {if (indexScience1 != -1){int index1=s.indexOf('e');int index2=s.lastIndexOf('e');if(index1!=index2)isSciencer=false;else{nums = s.split("e");isSciencer = isScientific(nums);}}if (indexScience2 != -1){int index1=s.indexOf('E');int index2=s.lastIndexOf('E');if(index1!=index2)isSciencer=false;else{nums = s.split("E");isSciencer = isScientific(nums);}}}if (isFloater || isInterger || isSciencer)return true;return false;}public boolean isScientific(String num[]) // 是否为有效的科学计数{if(num.length<2)return false;if (num[1].startsWith("+") || num[1].startsWith("-"))num[1] = num[1].substring(1);boolean flag1 = num[0].contains(".");if (flag1)flag1 = isFloat(num[0]);elseflag1 = isInt(num[0]);boolean flag2 = isInt(num[1]);if (flag1 && flag2)return true;return false;}public boolean isFloatic(String str[]) //是否为有效的浮点数{if (isInt(str[0]) && isInt(str[1]))return true;return false;}public boolean isFloat(String s) //是否为浮点数{boolean isTrue = false;String nums[] = null;if (s.startsWith("."))isTrue = isInt(s.substring(1));else if(s.endsWith("."))isTrue = isInt(s.substring(0,s.length()-1));else {nums = s.split("\\.");if (nums.length != 2)return false;isTrue = isInt(nums[0]) && isInt(nums[1]);}return isTrue;}public boolean isInt(String str) //是否为整数{if (str == null || str.length() == 0)return false;boolean isLeg = true;char ch;for (int i = 0; i < str.length(); i++) {ch = str.charAt(i);if (ch > '9' || ch < '0') {isLeg = false;break;}}return isLeg;}
0 0
- leetcode_Valid Number
- leetCode_Valid Number
- Leetcode_valid-number(updated c++ and python version)
- Leetcode_valid-parentheses
- Leetcode_valid-palindrome
- Leetcode_valid-sudoku
- Leetcode_Valid Palindrome
- LeetCode_Valid Sudoku
- leetcode_Valid Palindrome
- leetcode_Valid Parentheses
- leetcode_Valid Sudoku
- leetcode_Valid Anagram
- leetcode_Valid Anagram_easy
- LeetCode_Valid Parentheses
- LeetCode_valid-anagram
- leetcode_Valid Perfect Square
- leetcode_Valid Sudoku and Sudoku Solver (数独游戏) _easy
- Number
- 自定义数据类型,处理手机上网数据
- POJ 3273 Monthly Expense(二分)
- Linux网络编程——广播
- 了解云计算七(基于虚拟化的服务部署)
- Linux - 线程属性控制
- leetcode_Valid Number
- 支付宝即时到账接口使用历险记
- ubuntu 下 intellij idea 配置android 开发环境
- 人在江湖走,谁能不忘事
- 第11周 程序阅读——继承和派生(1)
- 《JVM故障诊断指南》之4 —— Java 8:从持久代到metaspace
- 泛型初步,泛型类
- 自定义数据类型,实现数据排序
- 各种实用的webservice