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 语言中重要的数据类型,是内存中占用空间最大的一个对象,高效地使用字符串, ...
随机推荐
- Spring boot 源码分析(前言)
开坑达人 & 断更达人的我又回来了 翻译的坑还没填完,这次再开个新坑= = 嗯,spring boot的源码分析 本坑不打算教你怎么用spring boot = = 也不打算跟你讲这玩意多方便 ...
- 父网访问子网(校园网访问校园网IP路由器下的一台电脑)远程路由器下的电脑
网路由器添加转发规则,端口转发,本人仅使用Pandora Box路由器固件 当然设置了这些还不够,还需要设置其他的允许端口转发的东西,例如 然后远程桌面的话还需要设置某些相关设置,例如电脑允许使用远程 ...
- TeamCity安装
1 使用docker安装 安装手册:https://hub.docker.com/r/jetbrains/teamcity-server/2 安装包安装. docker run -it --name ...
- docker介绍
一.docker的定义 docker是一个平台,开发人员进行docker应用的开发和系统管理人员对docker应用部署和管理. 二.docker与Virtual Machine的区别 三.docker ...
- 编译原理 #03# 龙书中缀转后缀JS实现版
// 来自龙书第2章2.5小节-简单表达式的翻译器 笔记 既然是语法制导翻译(Syntax-directed translation),那么最重要的东西当然是描述该语言语法的文法,以下为中缀表达式文法 ...
- [十二省联考2019]骗分过样例 luoguP5285 loj#3050
不解释(因为蒟蒻太弱了,肝了一晚受不了了...现在省选退役,这有可能就是我做的最后一题了... #include<bits/stdc++.h> using namespace std; # ...
- redis的架构(一)
redis认证 redis的认证比较简单,这里简单来说明一下怎么设置redis的认证: redis的配置文件中有一个requirepass字段,在后面直接写上对应的密码即可.默认redis的不开启认证 ...
- zabbix_agentd重装后启动时IPC和共享内存段问题
zabbix_agentd不知为啥被干掉后重装了zabbix,zabbix用户组id也变了. 重装zabbix后导致zabbix_agentd无法启动,两个问题 问题1: zabbix_agentd ...
- pyinstaller
下载pyinstaller pip install pyinstaller 打包文件 pyinstaller -F run.py # 打包成单个文件
- WebService的调用
今天测试用代码如何去调用WebService,因为之前都是直接vs直接引用,网上搜索的很多也并没有讲清楚,搞得一直不得劲.其实基元对象(string/int等)的调用倒是挺简单的,根据方法名赋值传参就 ...