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 ...
随机推荐
- 获取Ueditor里面的图片列表,地址绝对化
/** * 内容中图片地址转成绝对地址 * @param $content * @return mixed */ private function imgUrl( ...
- Python 进程池的同步方法
import time from multiprocessing import Process,Pool def f1(n): time.sleep(1) #print(n) return n*n i ...
- Java连接数据库的driver和url写法
oracle driver="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@localhost:1521 ...
- lua经典问题
lua是一门比较简单的脚本语言,但是有些问题经常碰到,在这里总结一下: 1 lua 传参,如果参数是table,则相当于传引用 2 lua中只有nil和false返回假 3 lua and 和 or ...
- Javascript内置对象、原生对象、宿主对象关系
https://blog.csdn.net/foamflower/article/details/9165691
- HTTP 各状态码大全
基本涵盖了所有问题 HTTP 400 – 请求无效 HTTP 401.1 – 未授权:登录失败 HTTP 401.2 – 未授权:服务器配置问题导致登录失败 HTTP 401.3 – ACL 禁止访问 ...
- 如何避免提交页面,信息未填写完善 就出现注册成功提示 基于js
基于bootstrip做好一个页面后,出现如下效果图 这个页面是未经过任何后端处理的页面,如果直接填写一个用户名 或者不填写任何东西都可以注册成功的,先来演示只输入一个用户名 就要可以注册成功的. 点 ...
- Java线程及线程池状态
一.Java线程的六种状态 如上图1,JDK定义线程状态是不存在“运行中”状态,但为方便描述过程有些图中会画出运行中的状态. Java线程创建后调用start方法进入就绪状态,被OS调度选中后运行,运 ...
- 我发起了一个 操作系统 GUI 和 Tcp / IP 包 的 开源项目 DeviceOS
操作系统 如果 不需要 处理 复杂多样 的 硬件 兼容性, 其实 并不算 大项目, 可以算 毕业设计 . 但是, GUI 和 Tcp / IP 这两个 部分 的 实现逻辑 很多 很复杂, 这 2 ...
- SpringBoot事务注解@Transactional
SpringBoot提供了非常方便的事务操作,通过注解就可以实现事务的回滚,非常方便快捷,下面我们就说一下如何进行事务操作. 1. 事务说明 在Spring中,事务有两种实现方式,分别是编程式事务管理 ...