[LeetCode 65] Valid Number (通过率最低的一题)

题目链接: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.
 *
 */

public class ValidNumber {

//	1481 / 1481 test cases passed.
//	Status: Accepted
//	Runtime: 214 ms
//	Submitted: 0 minutes ago

	//时间复杂度O(n) 空间复杂度O(1)
    public boolean isNumber(String s) {
		int i = 0;
		s = s.trim(); 				// 去除两端的空格
		boolean hasDecimal = false; // 标记是否已经出现了 小数点(.)
		boolean hasNum = false; 	// 标记之前的字符串是否是数值了
		boolean hasE = false; 		// 标记是否已经出现了 (e)字符
		if (s.length() == 0)
			return false;

		// 判断是否有正负号字符
		if (isSign(s.charAt(0))) {
			i++;
		}
		while (i < s.length()) {
			char c = s.charAt(i);

			if (isDigit(c)) {
				if (!hasNum) {
					hasNum = true;
				}
			} else if (isDecimal(c)) {
				if (hasDecimal || hasE) // 整个数值只能出现一个小数点(.); e的指数不能为小数;
					return false;
				hasDecimal = true;
			} else if (isE(c)) {
				// 如果之前的字符已经出现(e)字符,或者(e)出现在整个字符的最前或最后,则都不是一个有效的数值
				if (hasE || !hasNum || (++i) >= s.length())
					return false;
				// 判断指数是否有符号位
				if (!isSign(s.charAt(i)))
					i--;
				hasE = true;
				hasNum = false; // 需要重新寻找一个数值
			} else
				return false; // 如果有 (数字、小数点.、字符e)以外的字符
			i++;
		}
		return hasNum;
    }

    public boolean isSign(char c) {
    	return c == '+' || c == '-';
    }
    public boolean isDigit(char c) {
    	return (c - '0' >= 0 && c - '0' <= 9);
	}
    public boolean isDecimal(char c) {
    	return c == '.';
    }
    public boolean isE(char c) {
    	return c == 'e';
    }

	public static void main(String[] args) {
		System.out.println(new ValidNumber().isNumber("0"));
		System.out.println(new ValidNumber().isNumber(" 0.1 "));
		System.out.println(new ValidNumber().isNumber("abc"));
		System.out.println(new ValidNumber().isNumber("1 a"));
		System.out.println(new ValidNumber().isNumber("2e10"));
		System.out.println(new ValidNumber().isNumber(".1"));
		System.out.println(new ValidNumber().isNumber("6e6.5"));

	}

}
时间: 2024-11-05 22:46:42

[LeetCode 65] Valid Number (通过率最低的一题)的相关文章

LeetCode --- 65. Valid Number

题目链接: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 statemen

Leetcode 65. Valid Number 验证数字 解题报告

1 解题思想 更新下:这道突然就很多访问,想起来好像是lt提交通过率最低的,嗯,我写的也不是特别详细,如有问题可以新浪微博@MebiuW交流~~ 这道题条条框框是在太多了,各种情况..不过简略来说,正确的做法应该是: 1.熟悉数字的表述规则(可以看网上的,也可以看我代码的),这道题关键是要懂所有的数字规则. 2.对输入的数字首先进行必要的检测,是否有abc或者中间空格等非法字符 3.将e前面和e后面分开计算!e前面允许有小数点形式的,e后面不允许有小数点形式的 4.数字的形式一般是 可以有正负号

leetCode 65.Valid Number (有效数字)

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

[LeetCode] 65. Valid Number Java

题目: 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.

LeetCode:Valid Number - 判断字符串中内容是否为数字

1.题目名称 Valid Number(判断字符串中内容是否为数字) 2.题目地址 https://leetcode.com/problems/valid-number/ 3.题目内容 英文:Validate if a given string is numeric. 中文:给出一个字符串,检查这个字符串中内容是否是一个数字 例如:"0"." 0.1"."2e10"是数字,"abc"."1 a"不是数字 4

No.65 Valid Number

No.65 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

【leetcode】Valid Number

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 am

[LeetCode][JavaScript]Valid Number

https://leetcode.com/problems/valid-number/ 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

[LeetCode][Java] 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 ambigu