Check Fail or Not
题目:
解题思路:
这道题主要就是用指针操作,当看到之后就移动指针。
代码:
一个string,o代表去上课了,L代表上课迟到了,A代表没去上课,如果连续三次L直接fail,大于
两次A也fail,遍历这个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
Post a Comment