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利用滑动窗口法,逐步移动窗口,判断当前窗口内字符串是否是s1的全排列之一。
容易想到,窗口内的字符对应的出现次数要-1。所以,当字符从窗口左端脱离窗口时,要+1;当字符从窗口右端进入窗口时,要-1。
class Solution
{
public:
bool allZero(vector<int> count) // 检查count是否全零
{
for(int i : count)
{
if( i != )
return false;
}
return true;
} bool checkInclusion(string s1, string s2)
{
int len1 = s1.size();
int len2 = s2.size();
// if s1 is longer than s2, the answer is definitely false
if(len1 > len2)
return false; // 用于保存26个字母的使用情况
vector<int> count(, );
for(int i=; i<len1; i++)
{
// 将s1的各个字母使用次数加 1
count[s1[i] - 'a']++;
// 此语句实际上为滑动窗口的初始化
count[s2[i] - 'a']--;
}
// 判断第一个滑动窗口
if(allZero(count))
return true; // 移动滑动窗口
for(int i=len1; i<len2; i++)
{
//
count[s2[i] - 'a']--;
count[s2[i-len1] - 'a']++;
// 检查是否全零。全零代表当前滑动窗口为s1的全排列之一
if(allZero(count))
return true;
} return false;
}
};
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. I ...
- [CareerCup] 1.3 Permutation String 字符串的排列
1.3 Given two strings, write a method to decide if one is a permutation of the other. 这道题给定我们两个字符串,让 ...
- 567. Permutation in String
Problem statement: Given two strings s1 and s2, write a function to return true if s2 contains the p ...
- 567. Permutation in String判断某字符串中是否存在另一个字符串的Permutation
[抄题]: Given two strings s1 and s2, write a function to return true if s2 contains the permutation of ...
- C#的StringBuilder 以及string字符串拼接的效率对照
今天公司一个做Unity3d的人在说字符串拼接的一个效率问题,他觉得string拼接会产生新的一个内存空间,假设不及时回收会产生大量的碎片,特别是在Unity3d这样一个Updata环境下,由于每一帧 ...
- [LeetCode] Permutation in String 字符串中的全排列
Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...
- 【LeetCode】567. Permutation in String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/permutati ...
- 567. Permutation in String【滑动窗口】
Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...
- String字符串性能优化的探究
一.背景 String 对象是我们使用最频繁的一个对象类型,但它的性能问题却是最容易被忽略的.String 对象作为 Java 语言中重要的数据类型,是内存中占用空间最大的一个对象,高效地使用字符串, ...
随机推荐
- Delphi下MSMQ(Mircosoft Message Queue)实例(私有队列)
网上关于消息队列技术原理说明的详细文档很多,但涉及到Delphi的具体实现很少,这是我从网上找了一上午的资料,自己整合和尝试的能运行的程序. 打开控制面板->程序->添加组件,添加消息队列 ...
- Sitecore8.2 GeoIP - 在8.2的引擎盖下发生了什么?
访客互动 - 访客会话的开始 访问者访问Sitecore网站,这被视为一种新的互动.Sitecore对交互的定义是“......联系人与品牌联系的任何一点,无论是在线还是离线”.在我们的例子中,这是网 ...
- World is Exploding (容斥 + 统计)
题意:满足题目中的式子,a < b && c < d && Va < Vb && Vc > Vd 思路:先求不讨论位置重合的情况 ...
- IAR Embedded Workbench for ARM 8.22.1 基础使用教程
面向尚未熟悉且初次使用该软件的新手(比如我...) 1.建立新工作区 File-->>New Workspace 2.建立新项目 1) Project-->>Create Ne ...
- C#winform窗体利用系统抓取关闭按钮事件
const int WM_SYSCOMMAND = 0x112; const int SC_CLOSE = 0xF060; const int SC_MINIMIZE = ...
- vim基本命令总结
编辑模式下i 从光标所在位置前开始插入文本I 将光标移动到当前行行首,然后在其前插入文本a 用于在光标当前所在位置之后追加新文本A 将光标移动到所在行行尾,在那里插入新文本o 在光标所在行的下面新开一 ...
- pom的maven仓库的配置
这里简单记录一下问题 本人配置了nexus的私人仓库,配置阿里云的远程仓库(http://182.92.29.40/nexus/content/groups/public/)和正规的2个库(http: ...
- .NET平台常用的开发组件(csdn)
.NET平台常用的开发组件 原创 2017年02月24日 09:20:04 工欲善其事,必先利其器.学习.NET也10年有余,其优雅的编程风格,高效率的开发速度,极度简单的可扩展性,足够强大开发类库, ...
- CH 6201 走廊泼水节题解
题目链接:CH6201 当时在海亮考试的第一题: 心得:其实一个算法是要真正理解这个思路和过程,而并不是单单知道它是用来写什么题的: 思路:n个节点有n-1条边,把这n-1条边按照权值从小到大排序,有 ...
- Bugku-CTF之flag.php(点了login咋没反应)
Day20 flag.php 地址:http://123.206.87.240:8002/flagphp/ 点了login咋没反应 提示:hint