-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStrobogrammatic.java
More file actions
38 lines (38 loc) · 1.26 KB
/
Copy pathStrobogrammatic.java
File metadata and controls
38 lines (38 loc) · 1.26 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
package leetcode;
import java.util.*;
class Strobogrammatic{
HashMap<Character,Character> map;
Strobogrammatic(){
map = new HashMap<Character,Character>();
map.put('8','8');
map.put('0','0');
map.put('1','1');
map.put('6','9');
map.put('9','6');
}
public boolean isStrobogrammatic(String s){
if(s==null||s.length()==0) return false;
int i=0;
int y=s.length()-1;
char[] ss = s.toCharArray();
while(i<y){
if(!map.containsKey(ss[i]) || map.get(ss[i])!=ss[y]) return false;
i++;
y--;
}
if(i==y){
if(ss[i]=='0'||ss[i]=='8'||ss[i]=='1') return true;
else return false;
}
return true;
}
public static void main(String[] args) {
Strobogrammatic strobogrammatic = new Strobogrammatic();
System.out.println(strobogrammatic.isStrobogrammatic("0"));
System.out.println(strobogrammatic.isStrobogrammatic("818"));
System.out.println(strobogrammatic.isStrobogrammatic("88"));
System.out.println(strobogrammatic.isStrobogrammatic("69"));
System.out.println(strobogrammatic.isStrobogrammatic("8228"));
System.out.println(strobogrammatic.isStrobogrammatic("858"));
}
}