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中全部子串相连) 解题思路和方法的更多相关文章

  1. LeetCode - 30. Substring with Concatenation of All Words

    30. Substring with Concatenation of All Words Problem's Link --------------------------------------- ...

  2. [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 ...

  3. [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 ...

  4. 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 ...

  5. [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 ...

  6. LeetCode 30 Substring with Concatenation of All Words(确定包含所有子串的起始下标)

    题目链接: https://leetcode.com/problems/substring-with-concatenation-of-all-words/?tab=Description   在字符 ...

  7. [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 ...

  8. 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 ...

  9. leetCode 94.Binary Tree Inorder Traversal(二叉树中序遍历) 解题思路和方法

    Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...

随机推荐

  1. 推荐下载CentOS下各种组件的网址

    http://vault.centos.org/ ftp://mirrors.sohu.com/ http://mirror.webtatic.com/

  2. CentOS下MySQL主从复制,读写分离

    1.环境:所有系统都是CentOS5.5 mysql-5.6.31-2.el5,MySQL中都没有数据 主服务器IP为192.168.128.230 从服务器IP为192.168.128.235 代理 ...

  3. 21、Django实战第21天:课程章节信息

    在课程详情页中,点击"开始学习",就进入到这课程章节信息,这里面包含了两个页面:"章节"和评论 1.把course-video.html(章节).course- ...

  4. 【莫队算法】【权值分块】bzoj3339 Rmq Problem

    如题. #include<cstdio> #include<algorithm> #include<cmath> using namespace std; #def ...

  5. 网络编程-UDP

    代码部分--UDP传输 * 1.发送Send * 创建DatagramSocket, 随机端口号 * 创建DatagramPacket, 指定数据, 长度, 地址, 端口 * 使用DatagramSo ...

  6. 冒泡排序--注意flag变量的设置

    代码: #include<stdio.h> void BubbleSort(int a[],int n){ int i,j; int temp; ; // 此处flag变量的设置可以提高算 ...

  7. 常见的 HTTP 状态代码及原因

    代码 说明 备注 200 确定 IIS 7.0.IIS 7.5 和 IIS 8.0 成功处理了请求. 304 未修改 客户端浏览器请求已处于缓存中的文档,并且自从该文档被缓存后,未修改此文档.客户端浏 ...

  8. TSQLDBServerHttpApi使用工作线程池

    TSQLDBServerHttpApi使用工作线程池 TSQLDBServerHttpApi创建时,默认是使用单线程模式,且只使用一个数据库连接,服务端要应对众多的客户端只靠一个工作线程(主线程)和一 ...

  9. 最简单的PHP socket

    服务端 <?phperror_reporting(E_ALL);set_time_limit(0); $ip = "127.0.0.1";$port = 1935; func ...

  10. Java笔记3:Eclipse添加jar包

    本文以jedis包为例,演示Eclipse如何添加和使用jar包.   1 建立一个名为ImportJarDemo的JavaProject.在该工程下建立一个libs的文件夹. 2 将下载的jedis ...