**LeetCode-Substring with Concatenation of All Words
2015-11-25 07:39
423 查看
首先记录一个map 数字典里面的word 每个出现几遍, 然后对于string 扫3遍 (假如word length是3)每次有一个offset开始
这样就能保证把每个情况错开的word都包含全
判断是否涵盖了dict里面所有词并且使用频率也对 是通过另一个map的每个词个数不可以超dict的map 并且总count等于dict里面词的个数
然后每次记录一个start 开始每个词假如另一个map 假如这个词属于dict 就加入map 加入这个词的数量没有超 count就加一
假如这个词的个数超了 就挪动开始的start 往后一个词一个词减去 直到这个词个数不超 注意其中count什么时候--
之后判断是否满足条件可以作为一个结果
注意每次碰到非字典词 或者一遍扫完 换一个offset的时候 map要清空
public class Solution {
public List<Integer> findSubstring(String s, String[] words) {
List <Integer> res = new ArrayList<Integer>();
if ( s == null || s.length() == 0 || words == null )
return res;
int wordLen = words[0].length();
int num = words.length;
HashMap<String, Integer> hist = new HashMap<String, Integer>();
for ( int i = 0; i < num; i ++ ){
if ( hist.containsKey ( words [ i ] ) )
hist.put ( words[i], hist.get ( words[ i ]) + 1 );
else
hist.put ( words [i], 1 );
}
HashMap<String, Integer> map = new HashMap<String, Integer>();
for ( int i = 0; i < wordLen; i ++ ){
int count = 0;
int start = i;
for ( int cur = i; cur <= s.length() - wordLen; cur += wordLen ){
String temp = s.substring ( cur, cur + wordLen );
if ( hist.containsKey( temp )){
if ( map.containsKey( temp ) )
map.put ( temp, map.get ( temp ) + 1 );
else
map.put ( temp, 1 );
if ( map.get ( temp ) <= hist.get ( temp ))
count ++;
else{
while ( map.get (temp) > hist.get (temp) ){
String str = s.substring( start, start + wordLen );
map.put ( str, map.get ( str ) - 1 );
if ( hist.get ( str ) > map.get ( str ) )
count --;
start += wordLen;
}
}
if ( count == num ){
res.add ( start );
}
}
else{
map.clear();
count = 0;
start = cur + wordLen;
}
}
map.clear();
}
return res;
}
}
这样就能保证把每个情况错开的word都包含全
判断是否涵盖了dict里面所有词并且使用频率也对 是通过另一个map的每个词个数不可以超dict的map 并且总count等于dict里面词的个数
然后每次记录一个start 开始每个词假如另一个map 假如这个词属于dict 就加入map 加入这个词的数量没有超 count就加一
假如这个词的个数超了 就挪动开始的start 往后一个词一个词减去 直到这个词个数不超 注意其中count什么时候--
之后判断是否满足条件可以作为一个结果
注意每次碰到非字典词 或者一遍扫完 换一个offset的时候 map要清空
public class Solution {
public List<Integer> findSubstring(String s, String[] words) {
List <Integer> res = new ArrayList<Integer>();
if ( s == null || s.length() == 0 || words == null )
return res;
int wordLen = words[0].length();
int num = words.length;
HashMap<String, Integer> hist = new HashMap<String, Integer>();
for ( int i = 0; i < num; i ++ ){
if ( hist.containsKey ( words [ i ] ) )
hist.put ( words[i], hist.get ( words[ i ]) + 1 );
else
hist.put ( words [i], 1 );
}
HashMap<String, Integer> map = new HashMap<String, Integer>();
for ( int i = 0; i < wordLen; i ++ ){
int count = 0;
int start = i;
for ( int cur = i; cur <= s.length() - wordLen; cur += wordLen ){
String temp = s.substring ( cur, cur + wordLen );
if ( hist.containsKey( temp )){
if ( map.containsKey( temp ) )
map.put ( temp, map.get ( temp ) + 1 );
else
map.put ( temp, 1 );
if ( map.get ( temp ) <= hist.get ( temp ))
count ++;
else{
while ( map.get (temp) > hist.get (temp) ){
String str = s.substring( start, start + wordLen );
map.put ( str, map.get ( str ) - 1 );
if ( hist.get ( str ) > map.get ( str ) )
count --;
start += wordLen;
}
}
if ( count == num ){
res.add ( start );
}
}
else{
map.clear();
count = 0;
start = cur + wordLen;
}
}
map.clear();
}
return res;
}
}
相关文章推荐
- 无法关闭的QT程序——思路开阔一下,原来这么简单!
- QT:窗口最小化时显示一个小浮标
- 如何打造一个赚钱的个人博客?
- 闲来瞎扯 -- 在vs2008下编写linux程序
- cocos2d-x: 常用组件默认参数
- 224. Basic Calculator
- DFS基础 (HDU 1010、HDU 1016、HDU 1072)
- QT:浮动的饼状统计图(自绘不规则窗口)
- 11月21日--11月30日(34小时,剩3279小时)
- 在QTableView中使用各种自定义委托
- Java EE 6 体系结构图
- QT:使用“状态模式”绘制界面
- Java EE 7 体系结构图
- C++ new和delete实现原理——new和delete最终调用malloc和free
- uboot 运行过程中的存储分布图解
- OSChina 周三乱弹 —— 如何判断程序员在做什么
- Qt中如何 编写插件 加载插件 卸载插件
- QT:用QSet储存自定义结构体的问题——QSet和STL的set是有本质区别的,QSet是基于哈希算法的,要求提供自定义==和qHash函数
- 树莓派安装kali后的简单配置
- PS5 MAC 破解补丁下载 以及激活方法