【LeetCode】Valid Number 解题报告

【题目】

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.

【解析】

题意:判断一个字符串是否是计算机合法数字。

思路:把字符串分三段(小数点前、小数点与e/E之间部分、e/E之后),这三段都必须为纯数字组成的字符串。注意,第一部分和第三部分可以带符号(如+12.34e-56),第一部分和第二部分可以有一部分为空(如“.2"或"2.")。

  1. 去掉首尾多余的空格;
  2. 去掉开头的正负号;
  3. 看有没有e或E,如果有那么e/E后面只能是整数;
  4. 再看e前面的部分有没有小数点,小数点前后两部分都必须为整数。

【Java代码】

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 pose = s.indexOf("e") >= 0 ? s.indexOf("e") : s.indexOf("E");
        if (pose >= 0) {
            String poste = s.substring(pose + 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, pose);
        }

        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;
    }
}
时间: 2024-10-22 10:17:57

【LeetCode】Valid Number 解题报告的相关文章

LeetCode: Valid Number 解题报告

Valid NumberValidate 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 ambi

LeetCode: Valid Parentheses 解题报告

Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" a

LeetCode: Valid Sudoku 解题报告

Valid SudokuDetermine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note:A valid Sudoku board

LeetCode: Valid Palindrome 解题报告

Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you

LeetCode: Largest Number 解题报告

Largest Number Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so you need to return a string

[leetcode]Ugly Number 解题报告 C语言

[题目] Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7. Note that 1

[leetcode]Valid Sudoku 解题报告 C 语言

[题目] Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. Note: A valid Sudoku board (partially filled) is not necessarily solvable.

[leetcode]Valid Anagram解题报告 C语言

[题目] Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = "anagram", t = "nagaram", return true. s = "rat", t = "car", return false. Note: You may assume the string cont

[LeetCode]Longest Valid Parentheses, 解题报告

题目 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example i