My Concise Iterative Java Code


  • 5
    P
    public List<String> findStrobogrammatic(int n) {
    	Map<Character, Character> map = buildMap();
    	List<String> ret = n % 2 == 0 ? Arrays.asList("") : Arrays.asList("1", "8", "0");
    
    	for (int i = n % 2 == 0 ? 1 : 2; i < n; i += 2) {
    		List<String> cur = new ArrayList<>();
    		for (char c : map.keySet()) {
    			for (String s : ret) {
                    // don't add leading 0s!
    				if (i != n - 1 || c != '0')
    					cur.add(c + s + map.get(c));
    			}
    		}
    		ret = cur;
    	}
    
    	return ret;
    }
    
    private Map<Character, Character> buildMap() {
    	Map<Character, Character> map = new HashMap<>();
    	map.put('1', '1');
    	map.put('6', '9');
    	map.put('8', '8');
    	map.put('9', '6');
    	map.put('0', '0');
    	return map;
    }

  • 0
    J

    I am wondering whether there is any concise way to avoid string concatenation - use char[] or stringbuilder

    • and no need to convert them to list<string> in the end

Log in to reply
 

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