Skip to content

Latest commit

 

History

History
27 lines (22 loc) · 722 Bytes

1078.Bigram分词.md

File metadata and controls

27 lines (22 loc) · 722 Bytes

方法一:模拟

class Solution {
    public String[] findOcurrences(String text, String first, String second) {
        List<String> list = new ArrayList<>();
        String[] str = text.split(" ");
        for (int i = 0; i < str.length; i++) {
            if (str[i].equals(first) && (i + 1) < str.length && str[i + 1].equals(second) && (i + 2) < str.length) {
                list.add(str[i + 2]);
            }
        }
        
        /*
         这部分可改写成:return list.toArray(new String[list.size()]);
        */
        String[] res = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            res[i] = list.get(i);
        }
        return res;
    }
}