B*****7 发帖数: 137 | 1 这是问题的链接:
http://oj.leetcode.com/problems/word-break/
我的solution是:
public class Solution {
private HashMap cache = new HashMap(
);
public boolean wordBreak(String s, Set dict) {
if( s.length() == 0 ) return false;
if( cache.containsKey( s ) ) return cache.get( s );
Boolean result = false;
for( int i = 0; i < s.length(); i++ ) {
String head = s.substring( 0, i+1 );
String rest = (i < s.length()-1 ) ? s.substring( i+1 ) : "";
if( dict.contains( head ) ) {
if( rest.length() == 0) {
result = true;
} else if( wordBreak( rest, dict ) ) {
result = true;
break;
}
}
}
cache.put( s, result );
return result;
}
}
有一个case,s="a", dict=["a"]. 我在ide里得到正确的解true,但在leetcode确得到
false。我看了半天也没弄明白,难道是leetcode有bug吗?先谢谢啦:-) | z*********8 发帖数: 2070 | 2 The instance will only be initialized ONCE for all test cases... | B*****7 发帖数: 137 | 3 Thanks for the tip. I got it working now.
【在 z*********8 的大作中提到】 : The instance will only be initialized ONCE for all test cases...
|
|