m******3 发帖数: 346 | 1 在local没问题,code如下,可能是什么原因呢?
vector findSubstring(string s, vector& words) {
int wordLen = words[0].size();
map expectedCount;
map actualCount;
for (int i=0; i
expectedCount[words[i]] = expectedCount[words[i]]++;
}
vector res;
for (int i=0; i
int left = i;
int count = 0;
for (int j=i; j<=(int)s.length()-wordLen;j=j+... 阅读全帖 |
|
c*****m 发帖数: 271 | 2 之前写过一次,错了无数次才对。后来看了别人的优化code,今天再写一次,还是漏了
母串子串比较长度的corner case.
class Solution {
public:
vector findSubstring(string S, vector &L) {
vector result;
if (L.size() == 0) {
return result;
}
map expected;
for (int i = 0; i < L.size(); i++) {
expected[L[i]]++;
}
int wordLen = L[0].length();
//length compare
if (wordLen > S.length()) {
return result;
}
... 阅读全帖 |
|
s*******e 发帖数: 35 | 3 用HASHMAP 找出每个WORD 所在的位置 occurrence map,在做一个反向的由所在位置(
position)到WORD 的reverse occurrence map。 吧这些position 排序,然后递归搜索。
package smartnose;
import java.util.Arrays;
import java.util.*;
public class SubWordSeq {
public static void main(String [] args){
List L = new LinkedList();
L.add("fooo");L.add("barr");L.add("wing");L.add("ding");L.add("wing"
);
String S= "lingmindraboofooowingdingbarrwingmonkeypoundcake";
HashMap<... 阅读全帖 |
|
s*******e 发帖数: 35 | 4 用HASHMAP 找出每个WORD 所在的位置 occurrence map,在做一个反向的由所在位置(
position)到WORD 的reverse occurrence map。 吧这些position 排序,然后递归搜索。
package smartnose;
import java.util.Arrays;
import java.util.*;
public class SubWordSeq {
public static void main(String [] args){
List L = new LinkedList();
L.add("fooo");L.add("barr");L.add("wing");L.add("ding");L.add("wing"
);
String S= "lingmindraboofooowingdingbarrwingmonkeypoundcake";
HashMap<... 阅读全帖 |
|
q********c 发帖数: 1774 | 5 这是我的code, 更简单些:
int lengthOfLastWord(const char *s) {
int len = strlen(s), wordLen = 0;
const char *p = s+len-1;
while(isspace(*p) && p>=s)p--;
while(isalpha(*p) && p>=s) {
p--;
wordLen++;
}
return wordLen;
} |
|
z*********e 发帖数: 10149 | 6 现在会超时,哪个地方应该优化一下?用的BFS
本机上一秒之内就出结果了,应该没有严重的算法问题吧
public List> findLadders(String start, String end, Set
dict) {
List> lastPaths = new ArrayList<>();
if(start == null || end == null) return lastPaths;
dict.add(end);
List top = new ArrayList<>();
top.add(start);
lastPaths.add(top);
if(start.equals(end)) return lastPaths;
int wordLen = start.length();
while(!dict.isEmpty()){ // same as ... 阅读全帖 |
|
f**********r 发帖数: 2137 | 7 photography: instagram, flickr, picplz
social: facebook, twitter, qq, weico, kik, 微信,meebo
music: douban.fm, pandora, xiami
and wordlens, starbucks, chipotle, yelp, kayak... |
|