65. 有效数字

验证给定的字符串是否可以解释为十进制数字。

例如:

“0” => true

" 0.1 " => true

“abc” => false

“1 a” => false

“2e10” => true

" -90e3 " => true

" 1e" => false

“e3” => false

" 6e-1" => true

" 99e2.5 " => false

“53.5e93” => true

" --6 " => false

“-+3” => false

“95a54e53” => false

说明: 我们有意将问题陈述地比较模糊。在实现代码之前,你应当事先思考所有可能的情况。这里给出一份可能存在于有效十进制数字中的字符列表:

数字 0-9

指数 - “e”

正/负号 - “+”/"-"

小数点 - “.”

当然,在输入中,这些字符的上下文也很重要。

更新于 2015-02-10:

C++函数的形式已经更新了。如果你仍然看见你的函数接收 const char * 类型的参数,请点击重载按钮重置你的代码。

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/valid-number

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

class Solution {
char[] chars;
boolean point = false;//是否有小数部分
boolean exponent = false;//是否有指数部分
public boolean isNumber(String s) {
s = s.trim();//去空格
int length = s.length();
if(length == 0){
return false;
}
chars = s.toCharArray();//转字符数组
String[] ss = s.split("e");//以e分隔数组为两部分
if(ss.length == 0){//只有e 错误
return false;
}
if(ss[0].length() == 0) return false;//如果e之前的部分为空 错误
if(ss[0].length() < length) exponent = true;//如果前面部分字符长小于字符串长度,说明有指数部分
if(ss[0].length() == length -1){
return false;
}
String[] pre = ss[0].split("\\.");//以小数点分隔 if(pre.length == 0){//如果只有小数点 错误
return false;
}
if(pre[0].length() < ss[0].length()) point = true;
//如果分隔后前面部分小于原来的长度,说明有小数部分 boolean result = pre(0, pre[0].length());
//整数部分是否正确
result = result && middle(pre[0].length()+1, ss[0].length());
//中间部分是否正确
if(exponent){//如果有指数部分
result = result && is(ss[0].length() +1, length);
//指数部分是否正确
}
return result;
}
public boolean pre(int i, int length){//判断整数部分是否正确
if(i >= length){
//如果整数部分为空 由于.1也是正确的,所以先返回正确
return true;
}
//第一个字符是加减号,i+1
if(chars[i] == '+' || chars[i] == '-') {
i++;
}
if(i == length && !point){
//如果没有小数部分,但是只有正负,返回错误
return false;
}
for(; i < length; i++){
//遍历整数部分
if(chars[i] < '0' || chars[i] > '9'){
//不是0-9就返回错误
return false;
} }
//到这,整数部分就是正确的了
return true;
}
public boolean middle(int i, int length){//小数部分
if(i >= length && point ){
//如果有小数点,但是小数部分为空
if(chars[i - 2] >= '0' && chars[i - 2] <= '9') {
//如果小数点之前有数字返回正确
return true;
}
//没有返回错误
return false;
}
for(; i < length; i++){//遍历中间部分
if(chars[i] < '0' || chars[i] > '9'){
//不是0-9就返回错误
return false;
} }
return true;
}
public boolean is(int i, int length){//指数部分
if(i == 1){
//在进来之前已经判断有指数部分,如果e前面为空,返回错误
return false;
}
//指数部分也可能有正负
if(chars[i] == '+' || chars[i] == '-') {
i++;
}
//之后正负号,返回错误
if( i == length){
return false;
}
for(; i < length; i++){
//遍历指数部分
if(chars[i] < '0' || chars[i] > '9'){
//不是0-9返回错误
return false;
} }
return true;
}
}

Java实现 LeetCode 65 有效数字的更多相关文章

  1. C#版 - Leetcode 65. 有效数字 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. Leetcod ...

  2. [LeetCode] 65. 有效数字

    题目链接 : https://leetcode-cn.com/problems/valid-number/ 题目描述: 验证给定的字符串是否可以解释为十进制数字. 例如: "0"` ...

  3. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  4. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  5. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  6. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  7. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  8. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  9. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

随机推荐

  1. Offset等一些类似属性的使用

    1.offset系列 // offset 系列 var father = document.querySelector('.father'); var son = document.querySele ...

  2. 一句话+两张图搞定JDK1.7HashMap,剩下凑字数

    JDK1.7 HashMap一探究竟 HashMap很简单,原理一看散列表,实际数组+链表;Hash找索引.索引若为null,while下一个.Hash对对碰,链表依次查.加载因子.75,剩下无脑扩数 ...

  3. springDataJPQL实现增删改查及分页,原生sql查询,根据方法命名规则实现查询以及Specification查询

    一.使用方法 1.在dao中定义开一个方法,使用方法的参数设置jpql,并且使用方法的返回值接受查询结果,在方法上添加@query注解,在注解中写jpql语句进行增删改查,测试 2.使用原生的sql语 ...

  4. Linux系统如何安装qt-desinger

    前言:最近想在python3.7.3下玩下PyQt5,写UI有两种方式一种是使用手写,第二个就是使用Qt Designer工具来写,所以首先就是安装PyQt5和pyqt5-tools工具了. 一.安装 ...

  5. Excel 数据导入至Sqlserver 数据库中 ltrim() 、rtrim() 、replace() 函数 依次空格无效问题

    今天导一些数据从Excel中至Sqlserver 数据库中,在做数据合并去重的时候发现,有两条数据一模一样,竟然没有进行合并: 最后发现有一条后面有个“空格”,正是因为这个“空格”让我抓狂许久,因为它 ...

  6. promise对象里resolve和reject状态讲解及Promise.all()的使用

    首先来说下同步异步与阻塞非阻塞的概念,同步异步与阻塞非阻塞并没有关系.同步异步主要是事情做完以后,如何进行处理.或者说关注的是一种消息通信机制. 同步的情况下,是由处理消息者自己去等待消息是否被触发: ...

  7. zabbix配置主动式监控的步骤(原创)

    步骤如下: 1.克隆模板.命名新的模板名,并点击"监控项",全选,批量更新时第一个“类型”打勾,客户端改为主动式: 2.添加客户端或更改原有的模板为新模板(服务器端添加客户端时的配 ...

  8. F. Machine Learning 带修端点莫队

    F. Machine Learning time limit per test 4 seconds memory limit per test 512 megabytes input standard ...

  9. BZOJ 1050并查集+贪心

    1050: [HAOI2006]旅行comf Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3333  Solved: 1851[Submit][St ...

  10. 基于java的雷电游戏

    基于java的雷电游戏基本功能包括:敌方飞机随机飞行.我方飞机手动控制飞行,射击比拼,游戏闯关等.本系统结构如下: (1)雷电游戏状态调整功能: 在游戏启动时,游戏会自动进行初始化的验证. 若初始化成 ...