Share my java solution


  • 0
    M
    public boolean isStrobogrammatic(String num) {
    	// build a map : hold its match when it has one otherwise -1
    	int[] map = new int[10];
    	
    	Arrays.fill(map, -1);
    	map[0] = 0;
    	map[1] = 1;
    	map[6] = 9;
    	map[8] = 8;
    	map[9] = 6;
    	
    	int start = 0, end = num.length()-1;
    	
    	while(start <= end){
    		int match = map[num.charAt(end)-'0'];
    		if(match<0) return false; // no mapping exists
    		if(match != num.charAt(start)-'0') return false;
    		start++;end--;
    	}
    	return true;        
    }

  • 0
    H

    Arrays.fill(map, -1);

    if(match<0) return false;

    These lines are quite unnecessary as the code after will handle such condition.


  • 0
    W

    @Hao.W

    if remove those 2 lines, "02" will be true. the test cases does not cover that... though we can remove the second line.


Log in to reply
 

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