歪解字符串中連續出現次數最多問題

最近的學習中遇到一個比較簡單的題目:「一個字符串由01組成,求字符串中連續出現1的次數」,例如:字符串「0110011001111000」中,連續「1」出現的最大次數是「4」。請用Java實現這個功能。

正經解

應該比較好理解,我也第一時間也只想到了一個方法,就是遍歷。代碼如下:

/*
 *一個字符串由01組成,求字符串中連續出現1的次數
 */

public class FunTester extends SourceCode {

    public static void main(String[] args) {
        String str = "01011001011111101110101010110110";

        int max = 0;//最大連續

        int concurrent = 0;//當前最大連續

        int still = 0//0默認,2斷開
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            char n = i == str.length() - 1 ? '0' : str.charAt(i + 1);
            if (c == '1') {
                concurrent++;
                if (n == '0') {
                    still = 2;
                }
                if (still == 2) {
                    max = concurrent > max ? concurrent : max;
                    still = 0;
                    concurrent = 0;
                }
            }
       }
        output(max);
  }

}

歪解

在看到一串「01」組成的字符串時候,突然想到可以用「正則表達式」搞定這個,有兩個思路。

spit()

通過spit()方法分隔之後,遍歷分隔後的數組中String對象的長度,即可得到答案。具體代碼如下:


    public static void main(String[] args) {
        String str = "01011001011111101110101010110110";

        int max = 0;//最大連續

        String[] split = str.split("0+");

        for (int i = 0; i < split.length; i++) {
            int length = split[i].length();
            max = length > max ? length : max;
        }
        output(max);
    }

正則匹配

這個方法跟spit()類似,通過正則匹配將所有連續「1」組成的字符串提取出來,這樣也可以得到一個完全由「1」組成的字符串組成的數組。代碼如下:

 public static void main(String[] args) {
        String str = "01011001011111101110101010110110";

        int max = 0;//最大連續

        List<String> strings = Regex.regexAll(str, "1+");

        for (int i = 0; i < strings.size(); i++) {
            int length = strings.get(i).length();
            max = length > max ? length : max;
        }
        output(max);
    }

其中regexAll()方法是我自己封裝的Regex正則工具類中的,代碼如下:

    /**
     * 返回所有匹配項
     *
     * @param text  需要匹配的文本
     * @param regex 正則表達式
     * @return
     */

    public static List<String> regexAll(String text, String regex) {
        Matcher matcher = matcher(text, regex);
        List<String> result = new ArrayList<>();
        while (matcher.find()) {
            result.add(matcher.group());
        }
        return result;
    }

升級版

藉助當前思路,我們完全可以解決其他類似問題,如果一個字符串並不是由「0」「1」構成,依然可以採取相同的思路。假設某個字符串由「1」和其他的字符構成,求解相同的問題。依然可以通過正則的思路解決,當然第二個匹配思路依然是可行的。我下面分享一下,第一個spit分割思路的代碼。

    public static void main(String[] args) {
        String str = "0dsa10fds1fdsa10010111111fd014341104310fdasfds10fsdafdsfdsa101101gfd10";

        int max = 0;//最大連續

        String[] split = str.split("((?!1).)+");

        for (int i = 0; i < split.length; i++) {
            int length = split[i].length();
            max = length > max ? length : max;
        }
        output(max);
    }

「FunTester」騰訊雲年度作者,優秀講師 | 騰訊雲+社區權威認證,非著名測試開發,歡迎關注。

點擊閱讀原文,查看公衆號歷史文章


本文分享自微信公衆號 - FunTester(NuclearTester)。
如有侵權,請聯繫 [email protected] 刪除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。

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