有没有使用正则表达式的方法?

时间:2015-01-23 08:11:19

标签: java regex algorithm data-structures arraylist

这是一个面试问题 http://www.glassdoor.com/Interview/Indeed-Software-Engineer-Interview-Questions-EI_IE100561.0,6_KO7,24.htm,特别是问题

“问我一个方法,它接受一个字符串并返回最大长度的单词。可能有任何数量的空格使它有点棘手”

这是我的解决方案(带测试用例)

public class MaxLength {
public static void main(String[] args) {
    List<String> allWords = maxWords("Jasmine has no love  for chris", 2);
    for(String word: allWords){
        System.out.println(word);
    }
}
public static List<String> maxWords(String sentence, int length) {
    String[] words = sentence.trim().split("\\s+");
    List<String> list = new ArrayList<String>();
    for(String word: words) {
        if(word.length() <= length) {
            list.add(word);
        }
    }
    return list;
}

测试运行良好,我得到了我预期的输出 - 没有。然而,在实际的面试中,我认为面试官并不期望你能够从头脑中知道这个正则表达式(我不必从How do I split a string with any whitespace chars as delimiters?找到它) 如果不使用正则表达式,是否有另一种解决此问题的方法?

3 个答案:

答案 0 :(得分:0)

您可以使用StringTokenizer

另一种可能更冗长的方法是迭代字符串并使用Character类(Character.isWhiteSpace(char c))提供的方法并相应地中断字符串。

答案 1 :(得分:0)

尝试:

    public static List<String> maxWords(String sentence, int length) {
        List<String> list = new ArrayList<String>();
        String tmp = sentence;
        while (tmp.indexOf(" ") != -1) {
            String word = tmp.substring(0,tmp.indexOf(" "));
            tmp = tmp.substring(tmp.indexOf(" ")+1);
            if(word.length() <= length) {
                list.add(word);
            }
        }
        return list;
    }

答案 2 :(得分:0)

嗯,你可以尝试这样的事情: 没有创建很多subStrings()只创建&#34;匹配&#34;的子串。字符串

public class Test {

public static void main(String[] args) {
    String s = "Jasmine has no love  for chris";
    s=s.trim();
    List<String> ls = getMaxLength(s, 3);
    for (String str : ls) {
        System.out.println(str);
    }
}

static List<String> getMaxLength(String s, int length) {
    List<String> ls = new ArrayList<String>();
    int i = 0;
    if (s.charAt(i + length) == ' ' || i + length == s.length()) { // search if first word matches your criterion.
        for (i = 1; i < length; i++) { // check for whitespace between 0 and length

            if (s.charAt(i) == ' ') {
                i = length;
                break;
            }
            if (i == length - 1)
                ls.add(s.substring(0, length));
        }
    }

    for (i = length; i < s.length(); i++) {// start search from second word..
        if (s.charAt(i - 1) == ' ' && i + length < s.length() // if char at  length is space or end of String, make sure the char before the current char is a space.  
                && (s.charAt(i + length) == ' ' || i + length == s.length())) {
            for (int j = i; j < i + length; j++) {
                if (s.charAt(j) == ' ') {
                    i = i + length;
                    break;
                }
                if (j == i + length - 1) {
                    ls.add(s.substring(i, i + length));
                }
            }
        }

    }
    return ls;
} // end of method
}