Using two pointers in Java


  • 0

    public boolean isStrobogrammatic(String num) {

    if(num.length() == 0) return false;
    
    //Map<String, String> hm = new HashMap<String, String>();
    
    String num2 = new String(num);
    int count = 0;
    int a;
    int b;
    
    for(int i = 0; i < num.length(); i++){
    	a = i;
    	b = num.length()-i-1;
    	if(num.charAt(a) == '1' && num2.charAt(b) == '1') count++;
    	if(num.charAt(a) == '8' && num2.charAt(b) == '8') count++;
    	if(num.charAt(a) == '0' && num2.charAt(b) == '0') count++;
    	if(num.charAt(a) == '6' && num2.charAt(b) == '9') count++;
    	if(num.charAt(a) == '9' && num2.charAt(b) == '6') count++;
    }
    
    if(count == num.length()) return true;
    else return false;
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.