剩女的产生类似于资本论中相对性商品生产过剩 (转载)# Joke - 肚皮舞运动
a*f
1 楼
leetcode上ValidNumber这道题有点烦,因为它的需求不明确
试了很多次,下面的代码通过了测试,供参考
public class Solution {
enum Token {Empty, Sign, Float, Int, IntWithSign, Invalid};
int findFirst(char[] s, char c, int p1, int p2) {
for (int i = p1; i <= p2; i ++)
if (s[i] == c) return i;
return -1;
}
int findLast(char[] s, char c, int p1, int p2) {
for (int i = p2; i >= p1; i --)
if (s[i] == c) return i;
return -1;
}
Token tokenize(char[] s, int p1, int p2) {
if (p1 > p2) return Token.Empty;
int dot = findFirst(s, '.', p1, p2), dot2 = findLast(s, '.', p1, p2);
// Float, int.int, allow 0012.1200 such format
if (dot >= 0) {
if (dot2 != dot) return Token.Invalid; // More than one '.',
invalid
Token beforeDot = tokenize(s, p1, dot - 1);
Token afterDot = tokenize(s, dot + 1, p2);
if (beforeDot == Token.Invalid || afterDot == Token.Invalid)
return Token.Invalid;
// After . must be pure int or empty
if (afterDot != Token.Int && afterDot != Token.Empty) return
Token.Invalid;
// int before and/or after dot
if (beforeDot == Token.Int || beforeDot == Token.IntWithSign ||
afterDot == Token.Int) return Token.Float;
else return Token.Invalid;
}
// Int, only +- and digits, allow 0011 such format
else {
Token t = Token.Empty;
for (int i = p1; i <= p2; i ++) {
char c = s[i];
// + or - can only appear at the very beginning
if (c == '+' || c == '-') {if (t == Token.Empty) t = Token.
Sign; else return Token.Invalid;}
else if (c >= '0' && c <= '9') {
if (t == Token.Sign) t = Token.IntWithSign;
else if (t == Token.Empty) t = Token.Int;
else;
}
else return Token.Invalid;
}
return t;
}
}
public boolean isNumber(String s) {
char[] cc = s.trim().toCharArray();
int lastIndex = cc.length - 1;
int firstE = findFirst(cc, 'e', 0, lastIndex);
if (firstE < 0) { // No 'e', just float or integer number
Token t = tokenize(cc, 0, lastIndex);
return (t == Token.Float || t == Token.Int || t == Token.
IntWithSign);
}
else { // Scientific number such as 1e6
int lastE = findLast(cc, 'e', 0, lastIndex);
if (firstE != lastE) return false; // More than one 'e', invalid
Token beforeE = tokenize(cc, 0, firstE - 1);
Token afterE = tokenize(cc, firstE + 1, lastIndex);
if (beforeE == Token.Invalid || afterE == Token.Invalid) return
false;
// Power can only be int.
return (beforeE == Token.Float || beforeE == Token.Int ||
beforeE == Token.IntWithSign) && (afterE == Token.Int || afterE == Token.
IntWithSign);
}
}
}
试了很多次,下面的代码通过了测试,供参考
public class Solution {
enum Token {Empty, Sign, Float, Int, IntWithSign, Invalid};
int findFirst(char[] s, char c, int p1, int p2) {
for (int i = p1; i <= p2; i ++)
if (s[i] == c) return i;
return -1;
}
int findLast(char[] s, char c, int p1, int p2) {
for (int i = p2; i >= p1; i --)
if (s[i] == c) return i;
return -1;
}
Token tokenize(char[] s, int p1, int p2) {
if (p1 > p2) return Token.Empty;
int dot = findFirst(s, '.', p1, p2), dot2 = findLast(s, '.', p1, p2);
// Float, int.int, allow 0012.1200 such format
if (dot >= 0) {
if (dot2 != dot) return Token.Invalid; // More than one '.',
invalid
Token beforeDot = tokenize(s, p1, dot - 1);
Token afterDot = tokenize(s, dot + 1, p2);
if (beforeDot == Token.Invalid || afterDot == Token.Invalid)
return Token.Invalid;
// After . must be pure int or empty
if (afterDot != Token.Int && afterDot != Token.Empty) return
Token.Invalid;
// int before and/or after dot
if (beforeDot == Token.Int || beforeDot == Token.IntWithSign ||
afterDot == Token.Int) return Token.Float;
else return Token.Invalid;
}
// Int, only +- and digits, allow 0011 such format
else {
Token t = Token.Empty;
for (int i = p1; i <= p2; i ++) {
char c = s[i];
// + or - can only appear at the very beginning
if (c == '+' || c == '-') {if (t == Token.Empty) t = Token.
Sign; else return Token.Invalid;}
else if (c >= '0' && c <= '9') {
if (t == Token.Sign) t = Token.IntWithSign;
else if (t == Token.Empty) t = Token.Int;
else;
}
else return Token.Invalid;
}
return t;
}
}
public boolean isNumber(String s) {
char[] cc = s.trim().toCharArray();
int lastIndex = cc.length - 1;
int firstE = findFirst(cc, 'e', 0, lastIndex);
if (firstE < 0) { // No 'e', just float or integer number
Token t = tokenize(cc, 0, lastIndex);
return (t == Token.Float || t == Token.Int || t == Token.
IntWithSign);
}
else { // Scientific number such as 1e6
int lastE = findLast(cc, 'e', 0, lastIndex);
if (firstE != lastE) return false; // More than one 'e', invalid
Token beforeE = tokenize(cc, 0, firstE - 1);
Token afterE = tokenize(cc, firstE + 1, lastIndex);
if (beforeE == Token.Invalid || afterE == Token.Invalid) return
false;
// Power can only be int.
return (beforeE == Token.Float || beforeE == Token.Int ||
beforeE == Token.IntWithSign) && (afterE == Token.Int || afterE == Token.
IntWithSign);
}
}
}