forked from jsjtzyy/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC246_StrobogrammaticNumber.java
More file actions
36 lines (35 loc) · 1014 Bytes
/
LC246_StrobogrammaticNumber.java
File metadata and controls
36 lines (35 loc) · 1014 Bytes
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
import java.util.*;
public class LC246_StrobogrammaticNumber {
public boolean isStrobogrammatic(String num) {
if(num == null || num.length() == 0) return true;
HashSet<Character> set = new HashSet<>();
set.add('1');
set.add('0');
set.add('8');
set.add('6');
set.add('9');
char ch1 = ' ', ch2 = ' ';
int left = 0, right = num.length() - 1;
while(left < right){
ch1 = num.charAt(left++);
ch2 = num.charAt(right--);
if(!set.contains(ch1) || !set.contains(ch2)) return false;
switch(ch1){
case '1':
case '0':
case '8': if(ch1 != ch2) return false;
break;
case '6': if(ch2 != '9') return false;
break;
case '9': if(ch2 != '6') return false;
break;
}
}
if(left == right){
ch1 = num.charAt(left);
if(!set.contains(ch1)) return false;
if(ch1 == '9' || ch1 == '6') return false;
}
return true;
}
}