打開后發(fā)現這個申請過程有點意思,要先寫一段java代碼完成題目。
這個題目的大概意思是:一個String類型數組,填充的內容是一組映射關系,一個String作為校驗String數組的依據(英文太爛不知道對不對)。
它給了一個例子
Example input:
String[] config: { "/", "MainServlet", "/nav", "NavigationServlet" }
String requestUri: "/nav/test"
Correct result: "NavigationServlet"
In this example, the configuration contains a mapping of "/" to "MainServlet" and "/nav" to "NavigationServlet". In the case of an incoming URI "/nav/test.nav", "NavigationServlet" is the correct choice because its pattern is longer than that of "MainServlet".
這段話的意思是"/", "MainServlet"是映射關系,"/nav", "NavigationServlet"同上,那么requestUri: "/nav/test",則是對"/", "/nav"的評判。當滿足條件后選擇較長的字符串作為結果返回。
咋一看這題目意思挺明確的,不就是輸出長的字符串嗎?Coding

















結果一貼上就提示error,問題出在了哪里?原來是沒有看完題目。
As the principal engineer of an HTTP web server, you are responsible for implementing the request processing subsystem of the server.
An incoming request for a specific resource, identified by an URI, must be dispatched to the appropriate handler according to the server configuration which maps URIs to request handlers. 'HandlerFactory.getHandler' must be implemented:
public class HandlerFactory
{
public String getHandler(String[] config, String requestUri)
{
}
}
The string array 'config' contains URI patterns and handler names. Two consecutive values form a key-value pair comprised of URI pattern and handler. 'requestUri' represents an incoming request, the URI to match against the configured handlers. 'getHandler' must return the correct handler for a given URI as a string value.
An URI pattern never contains wildcards and represents the start of an URI string, a prefix. Matching must be implemented accordingly. The handler with the longest matching URI pattern wins if more than one pattern matches. If no handler can be found, "UlFuW0" must be returned.
關鍵是這一段交代了這個方法的意圖,條件是這樣的一個順序,String數組中充當key值與requestUri的關系是,key作為前綴給requestUri校驗的。只有當校驗通過時,value才能作為結果返回,當多個key值通過校驗后,返回length最大的value。
于是加了個條件。Line:9

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

當然這個是最初級的代碼,如果考慮的更加細致一點。代碼可以更加完善。