首先找到那几种是可以Strobogrammatic的
然后一个walker,一个runner,然后walker <= runner的时候比较是否是一组一组的
要等于因为2就并不是Strobogrammatic
1 public boolean isStrobogrammatic(String num) { 2 Map<Character, Character> map = new HashMap<Character, Character>(); 3 map.put(‘6‘, ‘9‘); 4 map.put(‘9‘, ‘6‘); 5 map.put(‘1‘, ‘1‘); 6 map.put(‘8‘, ‘8‘); 7 map.put(‘0‘, ‘0‘); 8 int walker = 0; 9 int runner = num.length() - 1; 10 while(walker <= runner) { 11 if(map.containsKey(num.charAt(walker)) && map.get(num.charAt(walker)) == num.charAt(runner)) { 12 walker++; 13 runner--; 14 } else { 15 return false; 16 } 17 } 18 return true; 19 }
时间: 2024-10-13 04:12:37