【问题标题】:Would there be an approach without using a regular expression?会有不使用正则表达式的方法吗?
【发布时间】:2015-01-23 08:11:18
【问题描述】:

这是一个来自 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?找到它) 有没有不使用正则表达式的另一种方法来解决这个问题?

【问题讨论】:

  • this site?的问题
  • 好电话,我也把它贴在他们身上
  • 另外,您的方法效率低下。您可以在一次迭代中完成此操作而无需拆分。
  • 我投票决定将此问题作为题外话结束,因为最好在http://codereview.stackexchange.com/提问
  • 如果面试官不知道他/她的头顶\\s+,我建议你跑,不要回头。 ;)

标签: java regex algorithm data-structures arraylist


【解决方案1】:

试试:

    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;
    }

【讨论】:

  • 这会导致单词之间有很多空格,但它确实会产生正确的单词
  • @Jens - 因为 subString 创建了太多 subStrings.. :) 和 indexOf() 在最坏的情况下必须做 n-k 检查.. 我不是说你答案是错误的(比使用 split 更好)。我只是说可能有更好的答案。 :)
  • @TheLostMind 但split 仅适用于正则表达式,OP 希望有一个不使用正则表达式的解决方案。
  • @Jens - 对。我是说你的答案比使用split() 更好。 :)
  • @Jens - 检查我的答案 :) .. 还没有尝试过所有输入,但你会明白我想说的话..
【解决方案2】:

您可以使用StringTokenizer

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

【讨论】:

    【解决方案3】:

    嗯,你可以试试这样的: 不会创建很多 subStrings() 只是创建“匹配”字符串的子字符串

    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
    }
    

    【讨论】:

      猜你喜欢
      • 2011-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-26
      相关资源
      最近更新 更多