leetCode 30.Substring with Concatenation of All Words (words中全部子串相连) 解题思路和方法
Substring with Concatenation of All Words
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: "barfoothefoobarman"
words: ["foo", "bar"]
You should return the indices: [0,9].
(order does not matter).
思路:leetcode 上有些题通过率低,并不见得是算法难,我认为非常大一部分原因是题目描写叙述不清晰。导致规则理解不透彻,所以编程的时候就会发生规则理解偏差的问题。
本题也是一样,刚一開始对规则理解偏差比較多,以为wors中字符出如今子串中出现一次就可以,无论反复还是不反复,可是后面提交只是时,看case全然理解错了规则,仅仅能又一次改写代码,非常麻烦。
怨言非常大,规则制定和说明也是非常重要的一点。
代码例如以下:
public class Solution {
public List<Integer> findSubstring(String s, String[] words) {
List<Integer> list = new ArrayList<Integer>();
if(words.length == 0 || s.length() == 0){
return list;
}
Map<String,Integer> map = new HashMap<String,Integer>();//保存个数以及值
for(int i = 0; i < words.length; i++){
if(map.get(words[i]) == null){
map.put(words[i],1);//将word保存
}else{
map.put(words[i],map.get(words[i])+1);//将word保存的数值+1
}
}
Map<String,Integer> mapValue = new HashMap<String,Integer>(map);//保存反复的个数,方便又一次赋值
int len = words.length;//去除反复之后的数组长度
int wordLen = words[0].length();
String temp = "";
int count = 0;//每一个单词出现一次,就记录一次
for(int i = 0; i <= s.length() - len*wordLen;i++){
count = 0;//初始化
for(int j = 0; j < len;j++){
temp = s.substring(i + j * wordLen,i + (j+1) * wordLen);//截取wordLen长的字符串
if(map.get(temp) != null && map.get(temp) != 0){//假设map还有多于0个
map.put(temp,map.get(temp)-1);//map中数值减去1
count++;//记录数+1
}else{
break;
}
}
if(count == len){//假设记录数与len相等。则说明符合要求
list.add(i);
}
//HashMap又一次初始化
for(String key:map.keySet()){//这样更高速
map.put(key,mapValue.get(key));
}
}
return list;
}
}
leetCode 30.Substring with Concatenation of All Words (words中全部子串相连) 解题思路和方法的更多相关文章
- LeetCode - 30. Substring with Concatenation of All Words
30. Substring with Concatenation of All Words Problem's Link --------------------------------------- ...
- [LeetCode] 30. Substring with Concatenation of All Words 串联所有单词的子串
You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...
- [LeetCode] 30. Substring with Concatenation of All Words 解题思路 - Java
You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...
- Java [leetcode 30]Substring with Concatenation of All Words
题目描述: You are given a string, s, and a list of words, words, that are all of the same length. Find a ...
- [leetcode]30. Substring with Concatenation of All Words由所有单词连成的子串
You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...
- LeetCode 30 Substring with Concatenation of All Words(确定包含所有子串的起始下标)
题目链接: https://leetcode.com/problems/substring-with-concatenation-of-all-words/?tab=Description 在字符 ...
- [LeetCode] 30. Substring with Concatenation of All Words ☆☆☆
You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...
- leetCode 95.Unique Binary Search Trees II (唯一二叉搜索树) 解题思路和方法
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- leetCode 94.Binary Tree Inorder Traversal(二叉树中序遍历) 解题思路和方法
Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...
随机推荐
- dutacm.club 1094: 等差区间(RMQ区间最大、最小值,区间GCD)
1094: 等差区间 Time Limit:5000/3000 MS (Java/Others) Memory Limit:163840/131072 KB (Java/Others)Total ...
- 19、Flask实战第19天:CSRF攻击与防御
CSRF攻击原理 网站是通过cookie来实现登录功能的.而cookie只要存在浏览器中,那么浏览器在访问这个cookie的服务器的时候,就会自动的携带cookie信息到服务器上去.那么这时候就存在一 ...
- POJ 2559 Largest Rectangle in a Histogram(单调栈)
[题目链接] http://poj.org/problem?id=2559 [题目大意] 给出一些宽度为1的长方形下段对其后横向排列得到的图形,现在给你他们的高度, 求里面包含的最大长方形的面积 [题 ...
- iOS开发技巧——关闭Autoresizing开启Autolayout
在使用Autolayout时有时候会有莫名其妙的错误,是因为UIView是默认同时开启Autoresizing和Autolayout的. 但我们的Autolayout很容易和Autoresizing冲 ...
- mysql-启动、关闭与重启
启动 service mysqld start mysql.server start 停止 service mysqld stop mysql.server stop 重启 mysql.server ...
- Zookeeper api增删改查节点
Exists - 检查Znode的存在 ZooKeeper类提供了 exists 方法来检查znode的存在.如果指定的znode存在,则返回一个znode的元数据.exists方法的签名如下: ex ...
- docker 实现redis集群搭建
摘要:接触docker以来,似乎养成了一种习惯,安装什么应用软件都想往docker方向做,今天就想来尝试下使用docker搭建redis集群. 首先,我们需要理论知识:Redis Cluster是Re ...
- css活用,半星星的效果
1.首先下载要用到星星字体 http://www.w3cplus.com/w3cplusDemo/demos/webFontIcon.html 2.css .cleanfloat::after{dis ...
- mysql 中查询一个字段是否为null的sql
查询mysql数据库表中字段为null的记录: select * 表名 where 字段名 is null 查询mysql数据库表中字段不为null的记录: select * 表名 where 字段名 ...
- C#之Raw Socket网络封包监视源码
大家可以建立一个Windows Form应用程序,在下面的各个文件中添加对应的源码: //RawSocket.csnamespace ReceiveAll{ using System; using S ...