LeetCode|Substring with Concatenation of All Words(所有单词的串联的子字符串)
2015-06-24 12:10
459 查看
You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation
of each word in words exactly once and without any intervening characters.
For example, given:
s:
words:
You should return the indices:
(order does not matter).
都是C++代码。
思路:用两个map,第一个来记录字典里面的word以及出现的次数;第二个map用来实时记录更新给定字符串里面的情况。
我的思路就是然后从第一个字符向后遍历字典长度的子字符串。这种思路leetcode运行时间600ms左右。有点低效。但是很好想。
其实上述解法存在重复的问题。是否能将之前检测的信息加以利用呢。后来借助酷壳网陈老师的思路,运行时间仅为60ms。
就是从头遍历到尾部,step为字典里面word的长度。如果出现word的出现次数,大于字典里面这个word的次数,就把最开始记录的word扫描,直到减去一次这个word。
总结bug:之间出现一些小问题,runtimen的提示:结果是手误,把wordCount这个map的赋值,出现i的范围过大;
start变量的step是unit,别忘了。很多小笔误困扰了好久。
of each word in words exactly once and without any intervening characters.
For example, given:
s:
"barfoothefoobarman"
words:
["foo", "bar"]
You should return the indices:
[0,9].
(order does not matter).
都是C++代码。
思路:用两个map,第一个来记录字典里面的word以及出现的次数;第二个map用来实时记录更新给定字符串里面的情况。
我的思路就是然后从第一个字符向后遍历字典长度的子字符串。这种思路leetcode运行时间600ms左右。有点低效。但是很好想。
vector<int> findSubstring(string s, vector<string>& words) { vector<int> res; int n=s.size(); int num=words.size(); int unit=words[0].size(); if(words.size()==0||n<num*unit) return res; map<string,int> wordCount,check; for(int i=0;i<num;i++){ ++wordCount[words[i]]; } for(int i=0;i<=n-num*unit;i++){ int count=0;//和num进行比较,如果一致则正确。(在每个word的次数一样前提下) check.clear(); int start=i; string word=s.substr(i,unit); if(wordCount.find(word)==wordCount.end()) continue; for(int j=i;j<i+num*unit;j+=unit){ if(j!=i) word=s.substr(j,unit); check[word]++; count++; if(check[word]>wordCount[word]){ break; } if(count==num){ res.push_back(start); } } } return res; }
其实上述解法存在重复的问题。是否能将之前检测的信息加以利用呢。后来借助酷壳网陈老师的思路,运行时间仅为60ms。
就是从头遍历到尾部,step为字典里面word的长度。如果出现word的出现次数,大于字典里面这个word的次数,就把最开始记录的word扫描,直到减去一次这个word。
vector<int> findSubstring(string s, vector<string>& words) {//仅60ms时间,是我的十分之一。 vector<int> res; int n=s.size(); int num=words.size(); int unit=words[0].size(); if(words.size()==0||n<num*unit) return res; map<string,int> wordCount,check; for(int i=0;i<num;i++){ ++wordCount[words[i]]; } for(int i=0;i<unit;i++){ int count=0;//和num进行比较,如果一致则正确。(在每个word的次数一样前提下) check.clear(); int start=i; for(int j=i;j<n;j+=unit){ string word=s.substr(j,unit); if(wordCount.find(word)==wordCount.end()){ check.clear(); start=j+unit; count=0; continue; } check[word]++; count++; if(check[word]>wordCount[word]){ string tmp; do{ tmp=s.substr(start,unit); --check[tmp]; --count; start+=unit; }while(tmp!=word); } if(count==num){ res.push_back(start); string tmp=s.substr(start,unit); start+=unit; --check[tmp]; --count; } } } return res; }
总结bug:之间出现一些小问题,runtimen的提示:结果是手误,把wordCount这个map的赋值,出现i的范围过大;
start变量的step是unit,别忘了。很多小笔误困扰了好久。
相关文章推荐
- TCP/IP详解学习笔记(11)-TCP交互数据流,成块数据流
- Python中的下划线详解
- mysql安装时到start service时就不响应了如何解决?
- TCP/IP详解学习笔记(10)-TCP连接的建立与中止
- mybatis基于Interceptor开发审计的自动更新(创建用户,创建日期,更新用户,更新日期)
- Xshell无法连接Ubuntu的解决办法
- MySQL学习系列4:运算符
- 引用和指针的区别,数组和指针的区别
- 实验五操作系统之存储管理
- 每秒如何接收上百万数据包
- TCP/IP详解学习笔记(8)-DNS域名系统
- java 字节流与字符流的区别
- java Future用法和意义一句话击破 [转]
- TCP/IP详解学习笔记(9)-TCP协议概述
- Tagxedo在线云词成像制作工具
- python&pandas 与mysql 连接
- TCP/IP详解学习笔记(7)-广播和多播,IGMP协议
- professional cuda c programming--CUDA库简介
- Invert Binary Tree
- Trading Technologies公司:一家金融服务企业的云技术探索旅程