注:这道题之前跳过了,现在补回来


一天一道LeetCode系列

(一)题目

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 wordsexactly 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).

(二)解题

本题需要在s中找到一串子串,子串是words中的单词按一定顺序排列组成的。返回这个子串在s中的起始位置。

主要思路:由于要考虑words中的重复单词,需要用到multiset,加快查找速度

1、首先用multiset存放words中的所有单词

2、遍历s,如果找到words中的某个单词,就在multiset中删除,直到multiset为空,如果没有找到则继续往后查找

3、返回下标的集合

  1. class Solution {
  2. public:
  3. vector<int> findSubstring(string s, vector<string>& words) {
  4. multiset<string> cnt;//用来存放words中的单词
  5. for (int i = 0; i < words.size(); i++)
  6. {
  7. cnt.insert(words[i]);//初始化
  8. }
  9. vector<int> ret;
  10. int lenw = words[0].length();
  11. int total = words.size() * lenw;
  12. int i, j;
  13. if (s.length() < total) return ret;
  14. for (i = 0; i <= s.length() - total;i++)
  15. {
  16. multiset<string> tempcnt = cnt;//拷贝一个副本
  17. for (j = i; j < s.length(); j += lenw)
  18. {
  19. string temp = s.substr(j, lenw);
  20. auto iter = tempcnt.find(temp);
  21. if (iter != tempcnt.end())//找到了
  22. {
  23. tempcnt.erase(iter);//先删除该元素
  24. if (tempcnt.empty()) {//为空代表找到了words中的所有单词按一定顺序排列的子串
  25. ret.push_back(i);
  26. break;
  27. }
  28. }
  29. else {//没找到
  30. break;
  31. }
  32. }
  33. }
  34. return ret;
  35. }
  36. };

【一天一道LeetCode】#30. Substring with Concatenation of All 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 解题思路 - Java

    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 (words中全部子串相连) 解题思路和方法

    Substring with Concatenation of All Words You are given a string, s, and a list of words, words, tha ...

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

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

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

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

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

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

  9. [Leetcode][Python]30: Substring with Concatenation of All Words

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 30: Substring with Concatenation of All ...

随机推荐

  1. swing JTable

    JTable 实例 import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayo ...

  2. Docker配置 DNS

    Docker 没有为每个容器专门定制镜像,那么怎么自定义配置容器的主机名和 DNS 配置呢? 秘诀就是它利用虚拟文件来挂载到来容器的 3 个相关配置文件. 在容器中使用 mount 命令可以看到挂载信 ...

  3. NDK编程的一个坑—Arm平台下的类型转换

    最近在做DNN定点化相关的工作,DNN定点化就是把float表示的模型压缩成char表示,虽然会损失精度,但是由于DNN训练的模型值比较接近且范围较小,实际上带来的性能损失非常小.DNN定点化的好处是 ...

  4. springMVC源码分析--RequestParamMethodArgumentResolver参数解析器(三)

    之前两篇博客springMVC源码分析--HandlerMethodArgumentResolver参数解析器(一)和springMVC源码解析--HandlerMethodArgumentResol ...

  5. hadoop入门级总结三:hive

    认识hive  Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供完整的SQL查询功能,可以将SQL语句转换为MapReduce任务运行  Hive是建立在 ...

  6. ejabberd mod_echo 解析

    ejabberd mod_echo 解析(金庆的专栏 2016.8)按开发入门的说明,mod_echo是最简单的模块之一.https://docs.ejabberd.im/developer/当然 m ...

  7. Hazelcast集群原理分析

    简介 hazelcast其中一个很重要的应用就是可以将多个应用服务器组成一个分布式环境的应用,形成一个cluster.这个cluster可以选举出一个master来对外工作.而cluster中的各台服 ...

  8. Android View框架总结(二)View焦点

    请尊重分享成果,转载请注明出处: http://blog.csdn.net/hejjunlin/article/details/52263256 前言:View框架写到第六篇,发现前面第二篇竟然没有, ...

  9. (SQL Server)有关T-SQL的10个好习惯

    转自 http://www.cnblogs.com/CareySon/archive/2012/10/11/2719598.html 1.在生产环境中不要出现Select * 这一点我想大家已经是比较 ...

  10. Mybatis代码自动生成插件使用

    1.配置pom.xml 添加mybatis-generator-maven-plugin到pom.xml. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1 ...