248.Strobogrammatic Number III

本文介绍了一种计算指定范围内回文数总数的方法。通过递归构造合法的回文字符串,并检查其是否符合区间条件,实现了高效查找。文章提供了一个具体的Java实现案例。
    /*
     * 248.Strobogrammatic Number III
     * 2016-6-18 by Mingyang
     * Write a function to count the total strobogrammatic numbers 
     * that exist in the range of low <= num <= high.
     * Given low = "50", high = "100", return 3. Because 69, 88, and 96 are three strobogrammatic numbers.
     * 大量的沿用了II的代码,非常不好!
     */
    public int strobogrammaticInRange(String low, String high) {
        if(low == null || high == null)
            return 0;
        int lo = low.length(), hi = high.length();
        int res = 0;     
        for(int i = lo; i <= hi; i++)
            res += findStrobogrammatic(i, low, high).size();       
        return res;
    }   
    private List<String> findStrobogrammatic(int n, String low, String high) {
        if(n < 1)
            return new ArrayList<String>();
        List<String> res = new ArrayList<String>();
        Map<Character, Character> map = new HashMap<Character, Character>();
        map.put('0', '0');
        map.put('1', '1');
        map.put('6', '9');
        map.put('8', '8');
        map.put('9', '6');     
        StringBuilder sb = new StringBuilder();
        int position = (n % 2 == 0) ? 0 : 1;
        findStrobogrammatic(res, sb, map, n, position, low, high);     
        return res;
    }   
    private void findStrobogrammatic(List<String> res, StringBuilder sb, Map<Character, Character> map, int n, int position, String low, String high) {
        if(sb.length() > n)
            return;
        if(sb.length() == n) {
            String s = sb.toString();
            if(firstStringEqualToOrSmaller(low, s) && firstStringEqualToOrSmaller(s, high))
                res.add(sb.toString());
            return;
        }     
        if(position == 1) {
            for(char c : map.keySet()) {
                if(c == '6' || c == '9')
                    continue;
                sb.append(c);
                findStrobogrammatic(res, sb, map, n, position + 1, low, high);
                sb.setLength(0);
            }
        } else {
            for(char c : map.keySet()) {
                if(n - sb.length() == 2 && c == '0')
                    continue;
                sb.insert(0, c);
                sb.append(map.get(c));
                findStrobogrammatic(res, sb, map, n, position + 2, low, high);
                sb.deleteCharAt(0);
                sb.deleteCharAt(sb.length() - 1);
            }    
        }
    }  
    private boolean firstStringEqualToOrSmaller(String s, String t) {
        if(s.length() < t.length())
            return true;
        else if(s.length() > t.length())
            return false;
        else {
            for(int i = 0; i < s.length(); i++)
                if(s.charAt(i) > t.charAt(i))
                    return false;
                else if(s.charAt(i) < t.charAt(i))
                    return true;
            return true;
        }
    }

 

转载于:https://www.cnblogs.com/zmyvszk/p/5599454.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值