-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumTest.java
More file actions
215 lines (196 loc) · 6.37 KB
/
Copy pathNumTest.java
File metadata and controls
215 lines (196 loc) · 6.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package AlgorithmTest;
import org.omg.CORBA.INTERNAL;
import java.util.ArrayList;
import java.util.Arrays;
/*
* 题目一:reverse 旋转数字
* 题目二:字符串转整数
* 题目三:判断是不是数字(本解法限制了不可以出现 "+" "-")
*
* */
public class NumTest {
public static void main(String[] args) {
System.out.println(isNumber("."));
}
public static int reverse(int x) {
boolean flag= false;
if(x <0) flag =true;
if(new Integer(x)>Integer.MAX_VALUE ||new Integer(x)<Integer.MIN_VALUE){
return 0;
}
x=Math.abs(x);
char [] aimArray=new String(""+x).toCharArray();
String result="";
for (int i = aimArray.length-1; i >=0; i--) {
result+=aimArray[i];
}
if(new Integer(result)>Integer.MAX_VALUE ||new Integer(result)<Integer.MIN_VALUE){
return 0;
}
int resultI=flag? -new Integer(result):new Integer(result);
System.out.println(resultI);
return resultI;
}
public static int myAtoi(String s) {
if (s.equals("")){
return 0;
}
char [] aimArray=s.toCharArray();
char flag=' ';
boolean Is=false;
boolean begin=false;
boolean Ischange=false;
String result="";
for (int i = 0; i <aimArray.length ; i++) {
if(!begin){
if (aimArray[i]==' ') {
continue;
}else{
try{
Integer temp=Integer.valueOf(aimArray[i]+"");
if (temp<=9&&temp>=0){
result+=aimArray[i];
begin=true;
Ischange=true;
}else{
break;
}
}catch (NumberFormatException e){
if (aimArray[i]=='+'||aimArray[i]=='-'){
flag=aimArray[i];
Is=aimArray[i]=='+'? false:true;
begin=true;
Ischange=true;
continue;
}
return 0;
}
}
}else{
if (aimArray[i]=='+'||aimArray[i]=='-'){
if (Ischange){
break;
}else{
flag=aimArray[i];
Is=aimArray[i]=='+'? false:true;
}
}else {
try{
Integer temp=Integer.valueOf(aimArray[i]+"");
if (temp<=9&&temp>=0){
result+=aimArray[i];
}else{
break;
}
}catch (NumberFormatException e){
if (result.length()==0){
return 0;
}else{
break;
}
}
}
}
}
try{
Integer _result=new Integer(result);
return !Is?_result: -_result;
}catch (NumberFormatException e){
if(result==""){
return 0;
}else{
return Is?-(Integer.MAX_VALUE+1):Integer.MAX_VALUE;
}
}
}
public static boolean isNumber(String s){
s=s.trim(); //删除两边的空格
if (s.length()==0){
return false;
}
int eN=0;
int qN=0;
char [] array=s.toCharArray();
for (int i = 0; i < array.length; i++) {
if (array[i]==' '){
return false;
}
try{
Integer temp =Integer.valueOf(array[i]+"");
}catch (NumberFormatException e){
if(array[i]=='.'){
if(i+1<=array.length-1){
if(array[i+1]=='e'){
return false;
}
}
if(qN==0){
qN=1;
if (array.length==1){
return false;
}
continue;
}else{
return false;
}
}else{
if (array[i]=='e'){
if(i+1<=array.length-1){
if(array[i+1]=='.'){
return false;
}
}
if(eN==0){
eN=1;
if (array.length==1||(i-1)<0||(array[0]=='0'&&(i+1)>array.length-1)||(i+1)>(array.length-1)){
return false;
}else{
continue;
}
}else{
return false;
}
}else{
return false;
}
}
}
}
return true;
}
public static boolean isNumberT(String s) {
s = s.trim();
int len = s.length();
if (0 == len) return false;
boolean hasE = false, hasDot = false, hasDigit = false, hasFirst = false;
for (int i = 0; i < len; i++)
{
char c = s.charAt(i);
if (c >= '0' && c <= '9') {
hasFirst = hasDigit = true;
continue;
}
switch (c) {
case 'e':
if (hasE || !hasDigit) return false;
hasE = true;
hasDot = true;
hasFirst = hasDigit = false;
break;
case '.':
if (hasDot) return false;
hasDot = true;
hasFirst = true;
break;
case '+':
case '-':
if (hasFirst) return false;
hasFirst = true;
break;
default:
return false;
}
}
return hasDigit;
}
}