注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
抱歉,在线题忘光光了。发个电面题及我的解法:
import java.io.*;
import java.util.*;
/*
* To execute Java, please define "static void main" on a class
* named Solution.
*
* If you need more classes, simply define them inline.
*
* Given a flat file of book metadata, write a Library class that parses the book data and provides an API that lets you search for all books containing a word.
*
* API:
*
* Library
* - <constructor>(input) -> returns a Library object
* - search(word) -> returns all books that contain the word anywhere in the title, author, or description fields. Only matches *whole* words.
* E.g. Searching for "My" or "book" would match a book containing "My book", but searching for "My b" or "boo" would *not* match.
*/
class Book {
String title;
String author;
String desc;
@Override
public String toString() {
return title + ":::" + author + ":::" + desc;
}
}
class Library {
Map<String, List<String>> keyToTitle = new HashMap<String,List<String>>();
public Library(String lib) {
String[] books = lib.split("\n\n");
for(String book:books) {
Book curBook = initBook(book);
buildIndex(curBook);
}
}
private Book initBook(String book) {
Book curBook = new Book();
String[] keyFields = book.split("\n");
boolean descStarted = false;
StringBuilder descSb = new StringBuilder();
for(String keyField: keyFields) {
if(keyField.toLowerCase().startsWith("title")) {
curBook.title = keyField.substring(keyFieldannisters, the emergence of the\n" +
"White Walkers, the arrival of barbarian hordes, and other threats.\n");
List<String> first_results = library.search("Arrakis");
System.out.println(first_results.contains("Dune"));
List<String> second_results = library.search("winter");
System.out.println(second_results.contains("A Song Of Ice And Fire Series"));
List<String> third_results = library.search("demolished");
System.out.println(third_results.contains("Hitchhiker's Guide to the Galaxy"));
List<String> fourth_results = library.search("the");
System.out.println(fourth_results.size() == 3);
System.out.println(fourth_results.contains("Dune"));
System.out.println(fourth_results.contains("A Song Of Ice And Fire Series"));
System.out.println(fourth_results.contains("Hitchhiker's Guide to the Galaxy"));
}
}
|