zoukankan      html  css  js  c++  java
  • 剑指offer 53.字符串 表示数值的字符串

    题目描述

    请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100","5e2","-123","3.1416"和"-1E-16"都表示数值。 但是"12e","1a3.14","1.2.3","+-5"和"12e+4.3"都不是。
     

    解题思路

    利用正则式
    //正则表达式解法
    public class Solution {
        public boolean isNumeric(char[] str) {
            String string = String.valueOf(str);
            return string.matches("[\+\-]?\d*(\.\d+)?([eE][\+\-]?\d+)?");
        }
    }
    /*
    以下对正则进行解释:
    [\+\-]?            -> 正或负符号出现与否
    \d*                 -> 整数部分是否出现,如-.34 或 +3.34均符合
    (\.\d+)?           -> 如果出现小数点,那么小数点后面必须有数字;
                            否则一起不出现
    ([eE][\+\-]?\d+)? -> 如果存在指数部分,那么e或E肯定出现,+或-可以不出现,
                            紧接着必须跟着整数;或者整个部分都不出现
    */
     

     解法二

    //参见剑指offer
    public class Solution {
        private int index = 0;
      
        public boolean isNumeric(char[] str) {
            if (str.length < 1)
                return false;
             
            boolean flag = scanInteger(str);
             
            if (index < str.length && str[index] == '.') {
                index++;
                flag = scanUnsignedInteger(str) || flag;
            }
             
            if (index < str.length && (str[index] == 'E' || str[index] == 'e')) {
                index++;
                flag = flag && scanInteger(str);
            }
             
            return flag && index == str.length;
             
        }
         
        private boolean scanInteger(char[] str) {
            if (index < str.length && (str[index] == '+' || str[index] == '-') )
                index++;
            return scanUnsignedInteger(str);
             
        }
         
        private boolean scanUnsignedInteger(char[] str) {
            int start = index;
            while (index < str.length && str[index] >= '0' && str[index] <= '9')
                index++;
            return start < index; //是否存在整数
        }
    }
  • 相关阅读:
    使用Property+使用构造+构建自定义类+使用List<自定义类>(项目)
    几乎全面的食品英文总结
    网络资源深入剖析Binding(学习)
    WPF入门知识(学习)
    Get children gameobjects array and assign the script inside
    Unity3D + C#: Cloning the Content of a Serializable Class
    Use Web Fonts and the @fontface CSS Method
    Good XAML Patterns
    Coroutines & Yield
    How to make a mini map for your scene in Unity3d
  • 原文地址:https://www.cnblogs.com/Transkai/p/11398602.html
Copyright © 2011-2022 走看看