日期:2014-05-20  浏览次数:20714 次

分享 趋势科技的一道程序题(武汉地区考题)
废话不多说,先上图片

不知道大家能否看清题目。注意算法的效率

------解决方案--------------------
Java code

public static void main(String[] args) {
        String str = "xabcdaef";
        Set<String> list=new HashSet<String>();
        for (int i = 0; i < str.length(); i++) {
            for (int j = str.length(); j > 0; j--) {
                if(i>=j){
                    break;
                }
                String temp = str.substring(i, j);
                Set<Character> set = new LinkedHashSet<Character>();
                for (char c : temp.toCharArray())
                    set.add(c);
                if (temp.length() == set.size()) {
                    list.add(temp);
                }
            }
        }
        System.out.println(list);
    }

------解决方案--------------------
Java code

public static void maxSubString(String str) {
        int begin = 0;
        int end = 0;
        int maxLength = 1;
        int tempLength = 0;
        int k = 0;
        for (int i = 0, j = 1; j < str.length(); j++) {
            String source = str.substring(i, j);
            System.out.println(source);
            k = source.indexOf(str.charAt(j));
            if (k > -1) {
                i = k + 1;
                tempLength = j - i + 1;
            } else {
                tempLength += 1;
                if (tempLength > maxLength) {
                    maxLength = tempLength;
                    begin = i;
                    end = j;
                }
            }
        }
        System.out.println("max substring: " + str.substring(begin, end + 1));
    }