Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita0d592a

Browse files
author
applewjg
committed
Valid Number
Change-Id: I57832fea70a05dda1efa6df055a3fdd4190a4fe5
1 parent831e0a6 commita0d592a

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

‎ValidNumber.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Author: Andy, nkuwjg@gmail.com
3+
Date: Jan 25, 2015
4+
Problem: Valid Number
5+
Difficulty: Hard
6+
Source: https://oj.leetcode.com/problems/valid-number/
7+
Notes:
8+
Validate if a given string is numeric.
9+
Some examples:
10+
"0" => true
11+
" 0.1 " => true
12+
"abc" => false
13+
"1 a" => false
14+
"2e10" => true
15+
Note: It is intended for the problem statement to be ambiguous. You should gather all
16+
requirements up front before implementing one.
17+
18+
Solution: This finite-state machine solution. Learn from fuwutu & snakeDling.
19+
*/
20+
21+
publicclassSolution {
22+
publicbooleanisNumber(Strings) {
23+
intstart =0,end =s.length() -1;
24+
booleandot =false,exp =false,digit =false;
25+
while (start <=end && (s.charAt(start) ==' ')) ++start;
26+
while (start <=end && (s.charAt(end) ==' ')) --end;
27+
if (start <=end && (s.charAt(start) =='+' ||s.charAt(start) =='-')) ++start;
28+
if (start >end)returnfalse;
29+
for ( ;start <=end; ++start) {
30+
if (Character.isDigit(s.charAt(start)))digit =true;
31+
elseif (s.charAt(start) =='e' ||s.charAt(start) =='E') {
32+
if (exp ==true ||digit ==false ||start ==end)returnfalse;
33+
exp =true;
34+
}elseif (s.charAt(start) =='.') {
35+
if (dot ==true ||exp ==true)returnfalse;
36+
if (digit ==false &&start ==end)returnfalse;
37+
dot =true;
38+
}elseif (s.charAt(start) =='+' ||s.charAt(start) =='-') {
39+
if (start ==end)returnfalse;
40+
if (s.charAt(start-1) !='e' &&s.charAt(start-1) !='E')returnfalse;
41+
}elsereturnfalse;
42+
}
43+
returntrue;
44+
}
45+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp