leetcode65. Valid Number

xiaoxiao2021-02-27  470

public boolean isNumber(String s) { s = s.trim(); boolean pointSeen = false; boolean eSeen = false; boolean numberSeen = false; boolean numberAfterE = true; for(int i=0; i<s.length(); i++) { if('0' <= s.charAt(i) && s.charAt(i) <= '9') { numberSeen = true; numberAfterE = true; } else if(s.charAt(i) == '.') { if(eSeen || pointSeen) { return false; } pointSeen = true; } else if(s.charAt(i) == 'e') { if(eSeen || !numberSeen) { return false; } numberAfterE = false; eSeen = true; } else if(s.charAt(i) == '-' || s.charAt(i) == '+') { if(i != 0 && s.charAt(i-1) != 'e') { return false; } } else { return false; } } return numberSeen && numberAfterE; } https://zhuanlan.zhihu.com/p/20042325?columnSlug=simons
转载请注明原文地址: https://www.6miu.com/read-2872.html

最新回复(0)