LeetCode065——有效数字

版权声明:版权所有,转载请注明原网址链接。 https://blog.csdn.net/qq_41231926/article/details/82834913

我的LeetCode代码仓:https://github.com/617076674/LeetCode

原题链接:https://leetcode-cn.com/problems/valid-number/description/

题目描述:

知识点:字符串

思路:考虑全面所有可能的情况

考虑清楚全部情况比较困难,需按如下顺序一步步考虑。

(1)去掉两端的空格。如果字符串中全是空格,直接返回false

(2)去掉空格后,如果s的第一个字符是'+'或者'-',后续步骤中不考虑该字符。

(3)遍历s中的每一个字符串,记录'.'字符出现的次数以及出现的位置,记录'e'和'E'字符出现的次数以及出现的位置。

a.除了e的后面一个字符可以包含'+'或'-'外,其余地方如果出现除了'0' ~ '9'、'e'、'E'和'.'之外的字符,直接返回false。

b.如果e后面的字符是'+'或'-',且e是s中倒数第二个字符,对应"32e+"这种情形,直接返回false。

c.如果'.'出现的次数大于等于2次,直接返回false。如果'e'和'E'出现的次数大于等于2次,直接返回false。

(4)如果s的第一个字符是'.'

a.s的长度为1,即s是".",直接返回false

b.s中'.'后面的一个字符是'e'或'E',对应".e32"这种情形,直接返回false。

(5)如果e或'E'出现在s中第一个字符或者最后一个字符的位置,对应"e32"和"32e"这两种情形,直接返回false。

(6)如果'.'出现的位置大于'e'或'E'出现的位置,对应"32e3.2"这种情况,直接返回false。

(7)排除以上几种情况后,我们才可以返回true。

整个实现过程,我们只遍历了一次字符串s,因此时间复杂度是O(n)级别的,n为字符串s的长度。空间复杂度是O(1)。

JAVA代码:

public class Solution {

	public boolean isNumber(String s) {
		//remove the space on the border
		int left = 0;
		while(s.charAt(left) == ' ') {
			left++;
			//if s only contains ' ', return false
			if(left == s.length()) {
				return false;
			}
		}
		int right = s.length() - 1;
		while(s.charAt(right) == ' ') {
			right--;
		}
		s = s.substring(left, right + 1);
		if(s.charAt(0) == '-' || s.charAt(0) == '+') {
			s = s.substring(1);
		}

		Set<Character> validCharacter = new HashSet<>();
		validCharacter.add('.');
		validCharacter.add('e');
		validCharacter.add('E');
		for (int i = 0; i <= 9; i++) {
			validCharacter.add((char) ('0' + i));
		}
		int countPoint = 0;
		int countE = 0;
		int indexPoint = -1;
		int indexE = -1;
		for (int i = 0; i < s.length(); i++) {
			//if s contains character except '.', 'e', '0' ~ '9', return false
			if(!validCharacter.contains(s.charAt(i))) {
				return false;
			}
			if(s.charAt(i) == '.') {
				indexPoint = i;
				countPoint++;
				//if s contains 2 or more '.', return false
				if(countPoint >= 2) {
					return false;
				}
			}
			if(s.charAt(i) == 'e' || s.charAt(i) == 'E') {
				indexE = i;
				if(indexE < s.length() - 1) {
					if(s.charAt(indexE + 1) == '-' || s.charAt(indexE + 1) == '+') {
						if(indexE + 1 == s.length() - 1) {
							return false;
						}
						i++;
					}
				}
				//if s contains 2 or more 'e' and 'E', return false
				countE++;
				if(countE >= 2) {
					return false;
				}
			}
		}
		if(indexPoint == 0) {
			//if s contains '.' and s.length() == 1, return false
			if(s.length() == 1) {
				return false;
			}
			//if '.' is followed by 'e' or 'E', return false
			if(indexPoint + 1 == indexE) {
				return false;
			}
		}
		//if 'e' or 'E' in the first or last character of s, return false
		if(indexE == 0 || indexE == s.length() - 1) {
			return false;
		}
		//if index of '.' is greater than index of 'e' or 'E', return false
		if(indexPoint != -1 && indexE != -1) {
			if(indexPoint > indexE) {
				return false;
			}
		}
		return true;
	}
}

LeetCode解题报告:

猜你喜欢

转载自blog.csdn.net/qq_41231926/article/details/82834913