網易內推2017最長01子串

import java.util.Scanner;

/**
 * Created by Administrator on 2017/8/12.
 * 如果一個01串任意兩個相鄰位置的字符都不一樣,稱爲01串
 * 輸出最長的子串長度
 * 解法:最笨的o(n^2);
 */
public class wangyiFirst {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str;
        int max = 0;
        //while (sc.hasNext()) {

        //}
        str = sc.next();
        max = sovle(str);
        System.out.print(max);
    }

    private static int sovle(String str) {
        int max = 0;
        for (int i = 0; i < str.length(); i++) {
            int sum = 0;
            int index = 0;
            char q = str.charAt(i);
            for (int j = i + 1; j < str.length(); j++) {
                if (str.charAt(j) == q) {
                    index = j - i;
                    break;
                }else {
                    q = str.charAt(j);
                    index = j - i + 1;
                }
            }
            max = Math.max(max, index);
        }
        return max;
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章