Java for LeetCode 065 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.

解题思路:

本题边界条件颇多"1.", ".34","1.1e+.1"也是正确的。解题方法是先trim一下,然后按照e进行划分,然后分别对e前后进行判断JAVA实现如下:

    public boolean isNumber(String s) {
		s = s.trim();
		String[] splitArr = s.split("e");
		if (s.length() == 0 || s.charAt(0) == ‘e‘
				|| s.charAt(s.length() - 1) == ‘e‘ || splitArr.length > 2)
			return false;
		for (int k = 0; k < splitArr.length; k++) {
			String str = splitArr[k];
			boolean isDecimal = false;
			if (str.charAt(0) == ‘-‘ || str.charAt(0) == ‘+‘)
				str = str.substring(1);
			if (str.length() == 0)
				return false;
			for (int i = 0; i < str.length(); i++) {
				if (‘0‘ <= str.charAt(i) && str.charAt(i) <= ‘9‘)
					continue;
				else if (str.charAt(i) == ‘.‘ && !isDecimal) {
					if (k == 0 && str.length() > 1)
						isDecimal = true;
					else
						return false;
				} else
					return false;
			}
		}
		return true;
	}
时间: 2024-10-10 07:50:44

Java for LeetCode 065 Valid Number的相关文章

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

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 (有效数字)

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][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】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] 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.

Java for LeetCode 179 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 instead of an i

Java for LeetCode 036 Valid Sudoku

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 '.'. 解题思路: 传说中的数独(九宫格)问题,老实遍历三个规则即可: JAVA实现: static public boolean isValidSudoku(cha

Java for LeetCode 202 Happy Number

Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the