Java实现 LeetCode 65 有效数字
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 有效数字的更多相关文章
- C#版 - Leetcode  65. 有效数字 - 题解
		版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. Leetcod ... 
- [LeetCode] 65. 有效数字
		题目链接 : https://leetcode-cn.com/problems/valid-number/ 题目描述: 验证给定的字符串是否可以解释为十进制数字. 例如: "0"` ... 
- 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 ... 
- 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. ... 
- 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 ... 
- 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 ... 
- 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 ... 
- 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 ... 
- 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 ... 
随机推荐
- [coj 1353 Guessing the Number]kmp,字符串最小表示法
			题意:给一个字符串,求它的最小子串,使得原串是通过它重复得到的字符串的一个子串. 思路:先求最小长度,最小循环长度可以利用kmp的next数组快速得到,求出长度后然后利用字符串最小表示法求循环节的最小 ... 
- [hdu5319]二进制表示,简单模拟
			题意:给一个矩形,矩形里面画了4种符号,'.'表示没画线,'R'表示画了红线,'B'表示画了蓝线,'G'表示红线和蓝线同时画了,并且矩形主对角线上只能画红线,副对角线上只能画蓝线,问最少画多少条线才能 ... 
- 写ssm项目的注意点
			注意事项: 输出台乱码 a链接以post提交 表单提交前验证 onsubmit 属性在提交表单时触发. onsubmit 属性只在 中使用. <form action="/demo/d ... 
- 🏃♀️点亮你的Vue技术栈,万字Nuxt.js实践笔记来了~
			前言 作为一位 Vuer(vue开发者),如果还不会这个框架,那么你的 Vue 技术栈还没被点亮. Nuxt.js 是什么 Nuxt.js 官方介绍: Nuxt.js 是一个基于 Vue.js 的通用 ... 
- python实现登录密码重置简易操作
			需求: 1.用户输入密码正确登录 2.用户输入密码错误退出并调用函数继续输入 3.用户输入密码符合原先给定的一个值时,允许用户重置密码,并且可以用新密码登录 4.输入三次后禁止输入 虽然贴别的简单,但 ... 
- LinkedList详解-源码分析
			LinkedList详解-源码分析 LinkedList是List接口的第二个具体的实现类,第一个是ArrayList,前面一篇文章已经总结过了,下面我们来结合源码,学习LinkedList. 基于双 ... 
- Visual Studio 2019 Professional 激活
			Visual Studio 2019 Professional下载地址>https://visualstudio.microsoft.com/zh-hant/thank-you-download ... 
- 剑指offer——数据结构
			技术面重点:数组.字符串.链表.树.栈以及队列. 
- [Python进阶]002.装饰器(1)
			装饰器(1) 介绍 HelloWorld 需求 使用函数式编程 加入装饰器 解析 介绍 Python的装饰器叫Decorator,就是对一个模块做装饰. 作用: 为已存在的对象添加额外功能. 与Jav ... 
- [PHP学习教程 - 系统]004.通过ini_set()来设置系统属性(ini_set Method)
			PHP原意:ini_set — 为一个系统配置项设置值 基本信息: string ini_set ( string $varname , string $newvalue). (说明:设置指定配置选项 ... 
