Simple Java solution


  • 0
    H

    """

     public static int countStro(String low, String high){
    	int lown = low.length();
    	int highn = high.length();
    	int count = 0;
    	if (lown == highn)
    		count = isStronum(lown, low, high);
    	else{
    		count += isStronum(lown, low, high);
    		count += isStronum(highn, low, high);
    	}
    	return count;
    }
    
    
    public static int isStronum(int n, String low, String high){
    	List<String> res;
    	int lownum = Integer.valueOf(low);
    	int highnum = Integer.valueOf(high);
    	
    	if ((n & 1) == 1)  // odd number case
    		res = new ArrayList<String>(Arrays.asList("0", "1", "9"));
    	else  // even number case
    		res = new ArrayList<String>(Arrays.asList(""));
    	String[] table = {"00", "11", "88", "69", "96"}; // match table
    	
    	int count = 0;
    	while (n > 1){
    		n = n - 2;  // n / 2 is the remaining number of digit pair needed to append 
    		for (int i = ((n >> 1) == 0)? 1 : 0; i < 5; i ++){
    			String pair = table[i];
    			for (String cur : res){
    				int curnum = Integer.valueOf(pair.substring(0, 1) + cur + pair.substring(1));
    				if (curnum >= lownum && curnum <= highnum){
    					System.out.println(curnum);
    					count ++;
    				}
    					
    			}
    		}
    	}
    	return count;		
    }
    

    """


Log in to reply
 

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