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;
    }
}


AG32VF407直流电流采集仪设计:从1uA到10A宽范围采样与FPGA加速方案 本文详细介绍了基于AG32VF407芯片的直流电流采集仪设计方案,实现了从1uA到10A的宽范围采样。方案采用低噪声电源、精密运放调理、高速比较器自动档位切换及高精度ADC同步采样,并重点阐述了利用AG32VF407片内FPGA实现硬件加速以提升性能的架构思路,为精密电流测量提供了完整的硬件与FPGA加速解决方案。 阅读详情

相关推荐

STM32中RS485方向控制引脚应用详解

深入解析STM32如何通过GPIO控制RS485通讯的方向引脚,实现半双工通信的精准切换。重点讲解硬件连接与软件时序配合,确保rs485通讯稳定可靠,适用于工业控制等实际场景。

weixin_42451850的博客 401

leetCode30题 串联所有单词的子串

题目描述: 给定一个字符串 s 和一些长度相同的单词 words。找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置。 注意子串要与 words 中的单词完全匹配,中间不能有其他字符,但不需要考虑 words 中单词串联的顺序。 class Solution { public List<Integer> findSubstring(String s, Str...

dabing_c的博客 349

用Bus Hound和MIDIOX抓包分析USB MIDI数据流:从硬件接口到软件解析的完整实战

本文详细介绍了如何使用Bus Hound和MIDIOX工具抓包分析USB MIDI数据流,从硬件接口到软件解析的完整实战过程。通过解析MIDI协议、捕获Note On/Off事件、控制变更消息等实例,帮助开发者深入理解音乐数字接口的底层通信机制,并提供了常见问题的解决方案和调试技巧。

weixin_42692935的博客 406

LeetCode第三十题-字符串中具有所有单词串联的子串

Substring with Concatenation of All Words 问题简介:给定获得一个字符串s以及一个字符串数组words,这个数组的每一个元素是一个单词,在s中查找由字符串数组words中所有单词串联组成的子字符串的索引,单词顺序没有要求,但只能出现一次 注: 1.words数组中所有单词都要出现,并只出现一次 2.所有单词长度相同 举例: 1. 输入: s = “barfo...

weixin_44147866的博客 314

百度地图街景图像批量获取

由于个人研究需要在最近试了一下使用API获取百度地图街景图片,在此介绍一下基于我个人需求的百度街景批量获取方法,本文以Python编码作为主要方法,如果对零编码工具有需要的可以关注一下UA城市分析小组刚刚发布的街景图片获取工具。 街景图像用作区域细粒度空间指标测度需要解决几个问题。 1. 首先就是调用额度的限制,现在百度对于个人认证开发者每天可用的街景图像调用额度为100张,在不考虑购买额度上限(1万张600元,其实有经费的前提下可以直接考虑钞能力)的情况下动辄几千上万张的爬取数量会大大增加需要爬取的时间周

boycetoon29的博客 1万+

ADB详细(从下载安装到灵活运用)

ADB(Android Debug Bridge)是一种允许模拟器或已连接的 Android 设备进行通信的命令行工具,它可为各种设备操作提供便利,如安装和调试应用,并提供对 Unix shell(可用来在模拟器或连接的设备上运行各种命令)的访问。可以在Android SDK/platform-tools中找到 adb 工具或下载ADB Kits。

LiuYong_01的博客 10万+

30. Substring with Concatenation of All Words找出串联所有词的子串

题目:https://leetcode.com/problems/substring-with-concatenation-of-all-words/description/ You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indic...

ZHO9504的博客 271

在VINS-FUSION上处理KITTI数据集(双目+GPS)

VINS-Fusion 是一个开源的实时多传感器状态估计库,主要由香港科技大学的沈邵劼教授领导的研究团队开发。它是 VINS-Mono(单目视觉惯性系统)的扩展,支持多种传感器组合,如双目、立体相机和IMU。

如果想成为中心,那么就到中心去吧。 1206

30. 与所有单词相关联的字串

给定一个字符串 s 和一些长度相同的单词 words。在 s 中找出可以恰好串联 words 中所有单词的子串的起始位置。 注意子串要与 words 中的单词完全匹配,中间不能有其他字符,但不需要考虑 words 中单词串联的顺序。 示例 1: 输入: s = "barfoothefoobarman", words = ["foo","bar"] 输出: [0,9] 解释: 从索...

zrh_优快云的博客 437

改进的U-Net在视网膜血管分割上的应用

为了提高算法在血管边界的分割能力,在编码部分采用全局卷积网络(GCN)和边界细化(BR)替换传统的卷积层;在跳跃连接部分引入改进的位置注意模块(PA)和通道注意模块(CA),目的是增加血管与背景之间的对比度,使网络更好地将血管与背景分割开;此外,为了提高网络的性能,在编码部分的最后一层使用密集卷积网络解决网络过拟合问题,同时为了在一定程度上解决梯度爆炸、梯度消失的问题,在解码部分的每一层使用卷积长短记忆网络提升网络获取特征信息的能力。替换传统的卷积操作来解决血管边界非血管像素误分为血管的问题。

weixin_70923796的博客 783

Open3D 基于法线的双边滤波

基于法线的双边滤波是一种用于点云平滑的技术,它结合了点与其邻域点之间的几何距离和法线方向的相似性,从而在去除噪声的同时保留几何特征。这种滤波方法广泛应用于点云处理领域,尤其是在需要保留表面细节和边缘的场景中。

纵马踏花向自由 2500

LeetCode刷题:30.串联所有单词的子串(C++)

心情:想扁人,这道题花了我一下午的时间,时间主要花在:第一,没把题目的各种情况考虑周全;第二,由于之前一直在用C语言编程,对C++的STL忘记了,重新大致的复习了一遍。心情复杂,虽然做出来了,但是效率不高,有待优化,先记录一下大致思路。 题目描述: 给定一个字符串 s 和一些长度相同的单词 words。找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置。 注意子串要与 w...

cirol1997的博客 413

Echarts - 去掉图表上的横线和竖线

效果图 默认效果: 变为如下图所示效果: 实现方法 官方配置文档:https://echarts.apache.org/zh/option.html#radar.splitLine 在 xAxis yAxis 配置中分别加入以下代码: splitLine: { show:false }

🏆 前端领域优质创作者 1万+

LeetCode Substring with Concatenation of All Words

现有一组长度相等的字符串words,要在原字符串中找出正好包含words中所有字符串的子字符串的起始位置。

DRFish 2379

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

LeetCode代码分析——30. Substring with Concatenation of All Words

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 starting indices of substring(s) in s that...

huanglong0438的专栏 385

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

LeetCode 30 Substring with Concatenation of All Words 解法为Hashmap

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 concaten

gx262091291的专栏 376

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

LeetCode 30. Substring with Concatenation of All Words

题目描述(困难难度) 给定一个字符串 s ,给定 n 个单词 word,找出所有子串的开始下标,使得子串包含了给定的所有单词,顺序可以不对应。如果有重复的单词,比如有 [ " foo " , " foo " ] 那么子串也必须含有两个 " foo ",也就是说个数必须相同。 解法一 参考 leetCode 里的 solution) 首先,最直接的思路,判断每个子串是否符合,符合就把下标保存起来,...

wind_liang的博客 960

leetcode 30 Substring with Concatenation of All Words

Substring with Concatenation of All Words

GadyPu的专栏 451

Leetcode #30. Substring with Concatenation of All Words 连续子串查找 解题报告

1 解题思想题目说给了一个原串,然后给了一组单词,让你从这个原串当中找一个区间,这个区间正好包含了这些所有的单词,不能交叉,不能有多余的值这道题是我在Leetcode 刷的最艰辛的一道题,我可能不适合这种风格的,最后我给的AC代码,有时候也可能会超时网上有说做法是用滑动窗口。。可能吧。。我看不懂所以我就是用了一个低级滑动窗口。。然后使用哈希加速,窗口期间按照顺序查看是否存在,是否满足条件,不满足的话

MebiuW的专栏 697

30. Substring with Concatenation of All Words

30. Substring with Concatenation of All Words方法1: 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 conca...

wilzxu的博客 247

30 Substring with Concatenation of All Words

题目链接:https://leetcode.com/problems/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 subs

再见了,过去的名字,新的奋斗开始啦! 734

leetcode 30. Substring with Concatenation of All Words Python3

一.问题描述 You are given a stringsand an array of stringswordsofthe same length. Returnall starting indices of substring(s) insthat is a concatenation of each word inwordsexactly once,in any order,andwithout any intervening characters. You can r...

CSerwangjun的博客 623

LeetCode-30. Substring with Concatenation of All Words

前言 突然一周没刷过题了,有点时间就刷一下题吧,顺便把解题思路记录下来,一个是自己做笔记,一个是给大家一点思路。前面的二十多道以后有时间再补吧,现在刷到了30题,就从这个开始吧! 问题描述 解法一 首先是一种暴力解法,直接遍历所有可能的list,然后看这个list是否满足要求,在验证该list是否满足要求 的时候需要使用到类似hash的数据结构,保证words中的每个元素都被用到过一次,由于其中...

Catcher 270

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 starting indices of substring(s) in s that is a concatenation of each word in words exactly once and w

指尖飞舞 3345

[leetcode] 30. Substring with Concatenation of All Words 解题报告

题目链接:https://leetcode.com/problems/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

小榕流光的专栏 1156
上一篇: UltraEdit取消自动备份(.bak)
下一篇: linux更换网卡网卡名称变化更改
weixin_34026484
博客等级 码龄11年 4642粉丝 152原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值