438. Find All Anagrams in a String
原题:
438. Find All Anagrams in a String
解题:
两个步骤
1)就是从s中逐步截取p长度的字符串
2)将截取出的字符串和p进行比较,比较可以用排序,或者字典比较(这两种方法提交后都超时了)
代码如下(提交超时):
class Solution {
public:
	vector<int> findAnagrams(string s, string p)
	{
		int lenp = p.length();
		int lens = s.length();
		int i=0,j,flag;
		vector <int> result;
		map <char,int> mapstr;
		map <char,int> maptemp;
		for(j =0; j < lenp;j++)
		{
			mapstr[p[j]]++;
		}
		while(i < lens)
		{
			maptemp = mapstr;
			flag = true;
			for(j = i; j < i +lenp;j++)
			{
				if(--maptemp[s[j]] < 0)
				{
					flag = false;
					break;
				}
			}
			if(flag)
			{
				result.push_back(i);
			}
			i++;
		}
		return result;
	}
};
提交AC:
class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        if (s.empty()) return {};
        vector<int> res, m(256, 0);
        int left = 0, right = 0, cnt = p.size(), n = s.size();
        for (char c : p) ++m[c];
        while (right < n) {
            if (m[s[right++]]-- >= 1) --cnt;
            if (cnt == 0) res.push_back(left);
            if (right - left == p.size() && m[s[left++]]++ >= 0) ++cnt;
        }
        return res;
    }
};
438. Find All Anagrams in a String的更多相关文章
- 【leetcode】438. Find All Anagrams in a String
		
problem 438. Find All Anagrams in a String solution1: class Solution { public: vector<int> fin ...
 - 438. Find All Anagrams in a String - LeetCode
		
Question 438. Find All Anagrams in a String Solution 题目大意:给两个字符串,s和p,求p在s中出现的位置,p串中的字符无序,ab=ba 思路:起初 ...
 - [LeetCode] 438. Find All Anagrams in a String 找出字符串中所有的变位词
		
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...
 - [leetcode]438. Find All Anagrams in a String找出所有变位词
		
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...
 - LeetCode 438. Find All Anagrams in a String (在字符串中找到所有的变位词)
		
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...
 - [LC] 438. Find All Anagrams in a String
		
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...
 - 【LeetCode】438. Find All Anagrams in a String 解题报告(Python)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 双指针 日期 题目地址:https://l ...
 - 【easy】438.Find All Anagrams in a String 找出字符串中所有的变位词
		
Input: s: "abab" p: "ab" Output: [0, 1, 2] Explanation: The substring with start ...
 - 438 Find All Anagrams in a String 找出字符串中所有的变位词
		
详见:https://leetcode.com/problems/find-all-anagrams-in-a-string/description/ C++: class Solution { pu ...
 
随机推荐
- iOS Runtime 实操练习
			
iOS Runtime 知识详解: http://yulingtianxia.com/blog/2014/11/05/objective-c-runtime/ 一般可以运行Runtime进行以下操作 ...
 - Day 2: ASP.NET and python trying
			
ASP.NET and Python/Javascript Many jQuery plugins that are designed and shared for free on the inter ...
 - rootfs 制作
			
最小 根文件系统 (1)/dev/console(终端控制台, 提供标准输入.标准输出以及标准错误) /dev/null (表示空设备终端, 所有写到这个文件中的数据都会被丢弃掉.) (2)init进 ...
 - shell中括号的特殊用法 linux if多条件判断
			
一. bash [ ] 单双括号 基本要素: Ø [ ] 两个符号左右都要有空格分隔 Ø 内部操作符与操作变量之间要有空格:如 [ “a” = “b” ] Ø 字符串比较中,&g ...
 - ipv4-only网络环境下访问ipv6站点
			
使用6plat.org+openVPN(无需资金投入)进入ipv6网络 这里我们主要使用的是6plat.org提供的“46模块——IPv4到IPv6”功能,需要配合openVPN这个软件,支持wind ...
 - CC攻击原理及防范方法
			
一. CC攻击的原理: CC攻击的原理就是攻击者控制某些主机不停地发大量数据包给对方服务器造成服务器资源耗尽,一直到宕机崩溃.CC主要是用来消耗服务器资源的,每个人都有这样的体验:当一个网页访问的人数 ...
 - php之异常处理
			
<?php declare(strict_types = 1); function demo(int $v):int{ return 1; } try{ demo("1"); ...
 - Centos6.5搭建Elasticsearch
			
ElasticSearch是基于Lucene的搜索服务.支持分布式多用户能力的全文搜索引擎,提供RESTful web接口.Elasticsearch是用Java开发的,Apache旗下开源项目,支持 ...
 - linux shell 指令搜索顺序
			
在linux shell 中输入一个命令,如果有多个同名指令,shell需要按照一定规则去取优先级高的一个执行,shell命令的搜索顺序为: 1.别名,使用alias创建的命令. 2.关键字,如if, ...
 - SQL-存储过程-010
			
什么是存储过程? 可以理解为数据库中的方法,与C#中的方法一样,具有参数和返回值: 存储过程的优点? 提高运行速度:存储过程在创造是进行编译,以后运行存储过程都不需要再进行编译,极大化的提高了数据库的 ...