Check Fail or Not

题目:

一个stringo代表去上了,L代表上课迟到了,A代表没去上,如果连续三次L直接fail,大于 两次Afail,遍历这string判断是否fail


解题思路:
这道题主要就是用指针操作,当看到之后就移动指针。

代码:

public class CheckFailorNot {

    // ooollaoooallaaoo    // true means fail
    public boolean check(String s){
        if(s == null || s.length() <= 1){
            return false;
        }
        int i = 0;
        while(i <= s.length() - 1){
            if(s.charAt(i) == 'o'){
                i++;
                continue;
            }
            else if(s.charAt(i) == 'l'){
                int count = 0;
                while(i <= s.length() - 1 && s.charAt(i) == 'l'){
                    count++;
                    i++;
                    if(count == 3){
                        return true;
                    }
                }
            }
            else {
                int count = 0;
                while(i <= s.length() - 1 && s.charAt(i) == 'a'){
                    count++;
                    i++;
                    if(count == 2){
                        return true;
                    }
                }
            }
        }
        return false;
    }

    public static void main(String[] args){
        CheckFailorNot checkFailorNot = new CheckFailorNot();
        String s = "ooollaoooallaaoo";
        boolean rst = checkFailorNot.check(s);
        System.out.println(rst);
    }

}

Comments

Popular Posts