【Lintcode】1154. Attendance Judgment

題目地址:

https://www.lintcode.com/problem/attendance-judgment/description

給定一個字符串,裏面只含有A,D,LA,D,L三種字母(也可以缺其中的某個或者某幾個)。問是否有連續的33LL出現或者是否DD出現了多於等於兩次,如果是則返回true,否則返回false。代碼如下:

public class Solution {
    /**
     * @param record: Attendance record.
     * @return: If the student should be punished return true, else return false.
     */
    public boolean judge(String record) {
        // Write your code here.
        if (record.contains("LLL")) {
            return true;
        }
        
        int D = 0;
        for (int i = 0; i < record.length(); i++) {
            D += record.charAt(i) == 'D' ? 1 : 0;
        }
        
        return D >= 2;
    }
}

時間複雜度O(n)O(n),空間O(1)O(1)

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