当前位置:网站首页>剑指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;
}
}
边栏推荐
- SQL statement to range query time field
- MySQL8.0.26安装配置教程(windows 64位)
- <urlopen error [Errno 11001] getaddrinfo failed>的解决、isinstance()函数初略介绍
- 太厉害了,终于有人能把文件上传漏洞讲的明明白白了
- Mysql应用安装后找不到my.ini文件
- Linux系统安装mysql(rpm方式安装)
- 关于superset集成到自己的项目中
- The interviewer asked me TCP three handshake and four wave, I really
- 面试Redis 高可靠性|主从模式、哨兵模式、Cluster集群模式
- 有了MVC,为什么还要DDD?
猜你喜欢

Three oj questions on leetcode

With MVC, why DDD?

运用flask框架发送短信验证码的流程及具体代码

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

Lock wait timeout exceeded解决方案

Centos7 install mysql5.7 steps (graphical version)

Unity resources management series: Unity framework how to resource management

MySQL优化之慢日志查询

mysql uses on duplicate key update to update data in batches

centos7安装mysql5.7
随机推荐
Pytorch教程Introduction中的神经网络实现示例
矩池云快速安装torch-sparse、torch-geometric等包
Centos7 install mysql5.7 steps (graphical version)
Duplicate entry ‘XXX‘ for key ‘XXX.PRIMARY‘解决方案。
MySQL-如何分库分表?一看就懂
运用flask框架发送短信验证码的流程及具体代码
Shell重油常压塔模拟仿真与控制
Information System Project Manager Core Test Site (55) Configuration Manager (CMO) Work
为什么要用Flink,怎么入门使用Flink?
精解四大集合框架:List 核心知识总结
DVWA shooting range environment construction
Summary of MySQL common interview questions (recommended collection!!!)
账号或密码多次输入错误,进行账号封禁
面试官问我TCP三次握手和四次挥手,我真的是
Apache DButils使用注意事项--with modifiers “public“
Redis Advanced - Cache Issues: Consistency, Penetration, Penetration, Avalanche, Pollution, etc.
[mysql improves query efficiency] Mysql database query is slow to solve the problem
【JS面试题】面试官:“[1,2,3].map(parseInt)“ 输出结果是什么?答上来就算你通过面试
快速掌握并发编程 --- 基础篇
Mysql应用安装后找不到my.ini文件