码迷,mamicode.com
首页 > 其他好文 > 详细

找出字符串中最长不重复子串

时间:2015-10-19 12:43:47      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

public class AE {
    public static void main(String[] args) {
        //String s = "abacdecfgab";
        String s = "abacdbe";
        System.out.println(noDuplicate(s));
    }

    public static String noDuplicate(String str) {
        String sub = "";
        String result = "";
        for (int i = 0; i < str.length(); i++) {
            String c = "" + str.charAt(i);
            if (sub.contains(c)) {
                if (sub.length() > result.length()) {
                    result = sub;
                }
                sub = sub.substring(sub.indexOf(c)+1);
            }
            sub += c;
        }
        if (sub.length() > result.length()) {
            result = sub;
        }
        return result;
    }
}

 

找出字符串中最长不重复子串

标签:

原文地址:http://www.cnblogs.com/hkzws/p/4891314.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!