LeetCode 466

https://leetcode-cn.com/problems/count-the-repetitions/

每日一题系列。

暴力解即可完成,我自己傻逼了开了个stringbuilder去存完整的s1字符串,导致最后炸了。

后来优化后就过了,打败31%的人 算是过了吧。

class Solution {
    public int getMaxRepetitions(String s1, int n1, String s2, int n2) {
        char[] str1 = s1.toCharArray();
        char[] str2 = s2.toCharArray();
        int count1 = 0;
        int count2 = 0;
        int j = 0;
        while(count1 < n1){
            for (char c : str1) {
                if (c == str2[j]) {
                    j++;
                    if (j == str2.length) {
                        count2++;
                        j = 0;
                    }
                }
            }
            count1++;
        }
        return count2/n2;
    }
}

 

posted @ 2020-04-19 11:20  ZJPang  阅读(112)  评论(0编辑  收藏  举报