leetcode 1078 Occurrences After Bigram

lc1078 Occurrences After Bigram

trim().split()将原字符串转换成words数组

依次匹配first和second,若两者都能匹配上,则下一个单词为third,将其加入List<String> res

返回 res.toArray(new String[0])

 1 class Solution {
 2     public String[] findOcurrences(String text, String first, String second) {
 3         String[] textPlus = text.trim().split(" ");
 4         List<String> res = new ArrayList();
 5         
 6         if(textPlus.length < 3 || first.length() == 0 || second.length() == 0)
 7             return new String[0];
 8         
 9         for(int i=0; i<textPlus.length-2; i++){
10             if(textPlus[i].equals(first) && textPlus[i+1].equals(second)){
11                 res.add(textPlus[i+2]);
12             }
13         }
14         
15         return res.toArray(new String[0]);
16     }
17 }
原文地址:https://www.cnblogs.com/hwd9654/p/10996697.html