【字符串全排列】LeetCode 567. Permutation in String

2S磷酸铁锂电池被动均衡方案:基于MP2642GR-Z与STM32F417ZG的实战 储能系统和备用电源中,串联电池组的容量失配是影响系统可用容量的核心问题,其本质是能量木桶效应:最弱电芯决定了整组输出能力。被动均衡作为最基础也最可靠的均衡方式,通过电阻耗散高SOC电芯的多余能量,在浮充为主的场景下具有硬件简单、成本低、可靠性高的突出优势。均衡系统的设计难点不在均衡动作本身,而在于采样精度、判据策略与散热工程三个维度:毫伏级电压失配的准确感知依赖合理的分压滤波链路与基准源设计,均衡开启与关闭的滞环判据决定了系统是否会反复震荡,均衡电阻的功率余量与温度反馈则直接影响长期运行安全。本文以2S磷酸 阅读详情

【字符串全排列】LeetCode 567. Permutation in String

Solution1:【超时,未能AC】
得到s1的所有全排列组合,然后在s2中查找s1的全排列集合
因为超时,未能AC,只是记录一下:
【注意】此题中针对字符串的全排列代码非常之经典!应当牢记!

class Solution {
public:
    bool checkInclusion(string s1, string s2) {
        vector<string> s1_per = permute(s1);
        for(auto a : s1_per) {
            if(s2.find(a) != string::npos)
                return true;
        }
        return false;
    }

    vector<string> permute(string& str) {
        if (str.empty()) return vector<string>(1, "");
        vector<string> res;
        string first(1, str[0]);
        str.erase(0, 1);
        vector<string> words = permute(str);
        for (auto &a : words) {
            for (int i = 0; i <= a.size(); ++i) {
                a.insert(i, first);
                res.push_back(a);
                a.erase(i, 1);
            }
        }   
        return res;
    }
};

Solution2:
我们先来看使用两个哈希表来做的情况,我们先来分别统计s1和s2中前n1个字符串中各个字符出现的次数,其中n1为字符串s1的长度,这样如果二者字符出现次数的情况完全相同,说明s1和s2中前n1的字符互为全排列关系,那么符合题意了,直接返回true。如果不是的话,那么我们遍历s2之后的字符,对于遍历到的字符,对应的次数加1,由于窗口的大小限定为了n1,所以每在窗口右侧加一个新字符的同时就要在窗口左侧去掉一个字符,每次都比较一下两个哈希表的情况,如果相等,说明存在,参见代码如下:
代码:
不得不说,算法真是牛逼啊。。

class Solution {
public:
    bool checkInclusion(string s1, string s2) {
        int n1 = s1.size(), n2 = s2.size();
        vector<int> m1(128), m2(128);
        for (int i = 0; i < n1; ++i) {
            ++m1[s1[i]]; ++m2[s2[i]];
        }
        if (m1 == m2) return true;
        for (int i = n1; i < n2; ++i) {
            ++m2[s2[i]];
            --m2[s2[i - n1]];
            if (m1 == m2) return true;
        }
        return false;
    }
};

20180708更:
可以!

class Solution {
public:
    bool checkInclusion(string s1, string s2) {
        int n1 = s1.size(), n2 = s2.size();
        if (n1 > n2) return false;
        vector<int> hash_s1(256, 0), hash_s2(256, 0);
        for (int i = 0; i < n1; i++) {
            hash_s1[s1[i]]++;
            hash_s2[s2[i]]++;
        }
        if (hash_s1 == hash_s2) return true;
        for (int i = n1; i < n2; i++) {
            hash_s2[s2[i]]++;
            hash_s2[s2[i-n1]]--;
            if (hash_s1 == hash_s2)
                return true;
        }
        return false;
    }
};
R语言笔记十:独立性检验函数 1.独立性检验函数        独立性检验是根据频数信息判断两类因子彼此相关或相互独立的假设检验。所谓独立性就是指变量之间是独立的,没有关系。 独立性检验算法:卡方检验、Fisher检验、Cochran-Mantel-Haenszel检验 2.假设检验 假设检验(Hypothesis Testing)是数理统计学中根据一定假设条件由样本推断总体的一种方法。 原假设——没有发生 备择假设——发生了 具体做法:根据问题的需要对所研究的总体作 阅读详情

相关推荐

树莓派4B+Ubuntu22.04+思岚RPLIDAR C1激光雷达应用

树莓派+Ubuntu22.04+思岚RPLIDAR C1激光雷达简单应用

techfuture的博客 3723

Leetcode——字符串的排列(全排列)

1. 题目 2. 题解 class Solution { /** 类似于 全排列2,本题使用set来去除重复元素 除了使用set去重外,还可以对数组进行排序,使用visited数组进行剪枝! */ Set<String> res = new HashSet(); public String[] permutation(String s) { backtrack(s.toCharArray(),ne

Yawn 795

Android 13/14/15 默认授权应用权限的实现方法

Android版本修改方式实现原理13/14修改Permission.java将所有权限标记为Normal权限15修改DeviceIdleController.java + DefaultPermissionGrantPolicy.java监听应用安装并主动授权。

m0_60898338的博客 2934

【LeetCode】每日一题 38. 字符串的排列【全排列】

1.题目链接 点击这里 2.解题思路 每日一水,直接使用next_permutation,但注意两个地方:第一,next_permutation的可接收参数不含string类型,所以要先变成char数组;第二,next_permutation是按照字典序的全排列,即如果给出的字符串不是字典升序,则全排列函数就会从当前字符串开始排列,而不会枚举之前的那些字符串,所以在使用前要先对字符串进行排序。 3.代码 class Solution { public: vector<string> pe

kingdom365的博客 327

用Python搞定超效率SBM模型:从数据清洗到结果可视化全流程指南

本文详细介绍了如何使用Python实现超效率SBM模型,从数据清洗到结果可视化的全流程指南。超效率SBM模型作为传统DEA的进化版本,能有效处理非期望产出(如环境污染)并对高效单元进行精细排序。通过实战案例和代码示例,帮助读者掌握这一先进模型的Python实现技巧,提升效率评估的准确性和可视化效果。

silver的专栏 666

制作3D地形图的方法-使用GlobalMapper和3ds Max软件

打开GlobalMapper软件,在菜单栏中选择“文件”>“打开数据文件”,然后选择相应的高程数据文件。在菜单栏中选择“渲染”>“渲染设置”,根据需要进行设置,如分辨率和输出格式。选择地形对象,点击菜单栏中的“渲染”>“材质编辑器”,然后根据需要调整材质属性,如颜色、纹理和光照等。在菜单栏中选择“工具”>“高程栅格和网格”>“创建地形网格”。在菜单栏中选择“文件”>“导出”>“导出栅格格式”。打开3ds Max软件,在菜单栏中选择“文件”>“导入”,然后选择之前导出的地形数据文件。步骤3:导出地形网格。

JqlScala的博客 1544

LeetCode567. 字符串的排列(C++版)

LeetCode567. 字符串的排列(C++版) 给你两个字符串 s1 和 s2 ,写一个函数来判断 s2 是否包含 s1 的排列。如果是,返回 true ;否则,返回 false 。 换句话说,s1 的排列之一是 s2 的 子串 。 示例 1: 输入:s1 = "ab" s2 = "eidbaooo" 输出:true 解释:s2 包含 s1 的排列之一 ("ba"). 示例 2: 输入:s1= "ab" s2 = "eidboaoo" 输出:false 提示: 1 <= s1.length,

weixin_44971689的博客 425

【PG高可用】一 PostgreSQL高可用方案repmgr部署(非常详细)

repmgr安装部署

DBA之路的博客 7063

【leetcode100】全排列

给定一个不含重复数字的数组nums,返回其所有可能的全排列。

qq_65509025的博客 641

【100分】【等和子数组最小和】

逆序排序,这样能够使得在分组时优先选取较大的元素,以便将元素分配到较小的子集中,从而使得每个子集的和相对均衡。给定一个数组nums,将元素分为若干个组,使得每组和相等,求出满足条件的所有分组中,组内元素和的最小值。分成若干个组,使得每组的和相等,并求出满足条件的所有分组中,组内元素和的最小值。在递归的过程中,我们需要注意一个优化点,即避免将相同和的桶重复计算。4 个子集(5),(1,4),(2,3),(2,3)2 个子集(5, 1, 4),(2,3, 2,3)个子集,则返回子集和的最小值,如果不能,则将。

weixin_54707168的博客 1178

【数据集】3D-GloBFP:全球首个三维建筑轮廓数据集

【数据集】3D-GloBFP:全球首个三维建筑轮廓数据集

WW、forever的博客 1743

[leetcode] 567. Permutation in String

Description Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. In other words, one of the first string’s permutations is the substring of the second str...

Learning from the mistakes 337

Error response from daemon: Get “https://index.docker.io/v1/search?q=nginx&n=25“:

在使用docker seach 命令搜索镜像的时候遇到这个错误,docker pull是可以正常下载镜像的,关于这个问题,本文提供解决方案,完全可行。

猿小白的博客 1万+

[LeetCode] 567. Permutation in String 字符串中的全排列

Given two stringss1ands2, write a function to return true ifs2contains the permutation ofs1. In other words, one of the first string's permutations is thesubstringof the second string. Exampl...

weixin_30802171的博客 179

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

LeetCode 567 Permutation in String

LeetCode 567 Permutation in String 传送门 题目分析 Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. In other words, one of the first string’s p...

博客 470

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

leetcode567. Permutation in String

**题目:Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. In other words, one of the first string’s permutations is the substring of the second string. Example 1: Input: s1 = “ab” s2 = “eidbaooo” Output: True E

qq_27736687的博客 314

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

【LeetCode】567. Permutation in String

567. Permutation in String

夏至亦韵 731

567. Permutation in String字符串的排列(效率待提高)

网址:https://leetcode.com/problems/permutation-in-string/ 参考:https://leetcode.com/problems/permutation-in-string/discuss/102588/Java-Solution-Sliding-Window 把s1的各个字母的出现次数记录下来,对s2利用滑动窗口法,逐步移动窗口,判断当前窗口内...

weixin_33843409的博客 80

[leetcode]567. Permutation in String

题目链接:https://leetcode.com/problems/permutation-in-string/#/description Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. In other words, one of

xiaocong1990的博客 864

【LeetCode】567. Permutation in String 解题报告(Python)

作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/permutation-in-string/description/ 题目描述: Given two strings s1 and s2, write a function to return true i...

负雪明烛 3122

LeetCode 567. Permutation in String C++

567. Permutation in String Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. In other words, one of the first string’s permutations is the substring o...

WXM 335

567. Permutation in String判断某字符串中是否存在另一个字符串的Permutation

[抄题]: Given two stringss1ands2, write a function to return true ifs2contains the permutation ofs1. In other words, one of the first string's permutations is thesubstringof the second string. ...

weixin_30906671的博客 158

[leetcode-567-Permutation in String]

Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. In other words, one of the first string's permutations is the substring of the second string.Example ...

weixin_34370347的博客 96

[Leetcode]567. Permutation in String

最开始的思路是求全排列,然后find一下: class Solution { void swap(char &amp;a, char &amp;b){ char c=a; a=b; b=c; } bool Permutation(string &amp;s){ if(s.size()==0 ) return...

bug++ 169

567. Permutation in String

Problem statement: Given two stringss1ands2, write a function to return true ifs2contains the permutation ofs1. In other words, one of the first string's permutations is thesubstringof the se...

weixin_30505225的博客 88

LeetCode第567题,字符串的排列,Java,滑动窗口

问题: 思路: 用HashMap的方法 构想了好久,虽然解决了,但是性能不很好 我一开始的思路就是,因为只有窗口中的字母的个数与s1相同,才有希望,所以就从一开始就再s2中看s1.length()个字符,先用HashMap记录s1各个字母出现的个数,如果窗口中的情况与s1的相同,那就返回true。 然后一个个进出,进出中查看两边指针的字母,然后对HashMap进行更改。 但是HashMap 很显然更改其中字符很麻烦,并且,每个窗口,都要检查一次就很...

qq_46485137的博客 315
上一篇: 【数字全排列】LeetCode 47. Permutations II
下一篇: LeetCode 36. Valid Sudoku
Allenlzcoder
博客等级 码龄10年 925粉丝 512原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址