当前位置:网站首页>剑指offer专项突击版 ---第 5 天
剑指offer专项突击版 ---第 5 天
2022-07-31 05:09:00 【米兰的小红黑】

class Solution {
public boolean checkInclusion(String s1, String s2) {
int m = s1.length();
int n = s2.length();
if(m > n){
return false;
}
int[] arr1 = new int[26];
int[] arr2 = new int[26];
for(int i = 0; i < m; i++){
++arr1[s1.charAt(i) - 'a'];
++arr2[s2.charAt(i) - 'a'];
}
if(Arrays.equals(arr1,arr2)){
return true;
}
for(int j = m; j < n; j++){
++arr2[s2.charAt(j) - 'a'];
--arr2[s2.charAt(j - m) - 'a'];
if(Arrays.equals(arr1,arr2)){
return true;
}
}
return false;
}
}

class Solution {
public List<Integer> findAnagrams(String s, String p) {
List<Integer> list = new ArrayList<>();
int m = s.length();
int n = p.length();
if(n > m){
return list;
}
int[] arr1 = new int[26];
int[] arr2 = new int[26];
for(int i = 0; i < n; i++){
++arr1[s.charAt(i) - 'a'];
++arr2[p.charAt(i) - 'a'];
}
if(Arrays.equals(arr1,arr2)){
list.add(0);
}
for(int j = n; j < m; j++){
++arr1[s.charAt(j) - 'a'];
--arr1[s.charAt(j - n) - 'a'];
if(Arrays.equals(arr1,arr2)){
list.add(j - n + 1);
}
}
return list;
}
}

class Solution {
public int lengthOfLongestSubstring(String s) {
HashMap<Character, Integer> calc = new HashMap<>();
int left = 0;
int ret = 0;
for(int i = 0; i < s.length(); i++){
if(calc.containsKey(s.charAt(i))){
left = Math.max(left, calc.get(s.charAt(i)) + 1);
}
calc.put(s.charAt(i),i);
ret = Math.max(ret,i -left + 1);
}
return ret;
}
}
边栏推荐
猜你喜欢

12 reasons for MySQL slow query

一文了解大厂的DDD领域驱动设计

MySQL transaction isolation level, rounding

sql statement - how to query data in another table based on the data in one table

Apache DButils使用注意事项--with modifiers “public“

A complete introduction to JSqlParse of Sql parsing and conversion

Distributed transaction processing solution big PK!

Moment Pool Cloud quickly installs packages such as torch-sparse and torch-geometric

sql语句-如何以一个表中的数据为条件据查询另一个表中的数据

Simple read operation of EasyExcel
随机推荐
MySQL8.0安装教程,在Linux环境安装MySQL8.0教程,最新教程 超详细
目标检测学习笔记
TOGAF之架构标准规范(一)
ABC D - Distinct Trio (Number of k-tuples
MySQL (updating)
Duplicate entry ‘XXX‘ for key ‘XXX.PRIMARY‘解决方案。
MySQL forgot password
matlab abel变换图片处理
运用flask框架发送短信验证码的流程及具体代码
一文了解大厂的DDD领域驱动设计
mysql uses on duplicate key update to update data in batches
About the problems encountered by Xiaobai installing nodejs (npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)
为什么要用Flink,怎么入门使用Flink?
Linux系统安装mysql(rpm方式安装)
MySQL8--Windows下使用压缩包安装的方法
ERROR 2003 (HY000) Can't connect to MySQL server on 'localhost3306' (10061)
110道 MySQL面试题及答案 (持续更新)
账号或密码多次输入错误,进行账号封禁
Distributed transaction processing solution big PK!
sql语句-如何以一个表中的数据为条件据查询另一个表中的数据