请将下面代码转为 java:class Solution: def wordBreak(self, s: str, wordDict: List[str]) -> bool: import functools @functools.lru_cache(None) def back_track(s): if(not s): return True res=False for i in range(1,len(s)+1): if(s[:i] in wordDict): res=back_track(s[i:]) or res return res return back_track(s)
时间: 2023-06-17 09:09:00 浏览: 133
以下是代码转化为 Java 的版本:
```
import java.util.*;
class Solution {
public boolean wordBreak(String s, List<String> wordDict) {
Map<String, Boolean> cache = new HashMap<>();
return backTrack(s, wordDict, cache);
}
private boolean backTrack(String s, List<String> wordDict, Map<String, Boolean> cache) {
if ("".equals(s)) {
return true;
}
if (cache.containsKey(s)) {
return cache.get(s);
}
boolean res = false;
for (int i = 1; i <= s.length(); i++) {
if (wordDict.contains(s.substring(0, i))) {
res = backTrack(s.substring(i), wordDict, cache) || res;
}
}
cache.put(s, res);
return res;
}
}
```
阅读全文