当前位置:网站首页>剑指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;
}
}
边栏推荐
- tf.keras.utils.pad_sequences()
- Mysql application cannot find my.ini file after installation
- ES source code API call link source code analysis
- 面试官,不要再问我三次握手和四次挥手
- Go language study notes - dealing with timeout problems - Context usage | Go language from scratch
- Interview Redis High Reliability | Master-Slave Mode, Sentinel Mode, Cluster Cluster Mode
- centos7安装mysql5.7
- MySQL开窗函数
- Distributed Transactions - Introduction to Distributed Transactions, Distributed Transaction Framework Seata (AT Mode, Tcc Mode, Tcc Vs AT), Distributed Transactions - MQ
- 12个MySQL慢查询的原因分析
猜你喜欢

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

The monitoring of Doris study notes

再见了繁琐的Excel,掌握数据分析处理技术就靠它了

mysql uses on duplicate key update to update data in batches

On-line monitoring system for urban waterlogging and water accumulation in bridges and tunnels

MySQL_关于JSON数据的查询

Three oj questions on leetcode

分布式事务处理方案大 PK!

【LeetCode-SQL每日一练】——2. 第二高的薪水

Interview Redis High Reliability | Master-Slave Mode, Sentinel Mode, Cluster Cluster Mode
随机推荐
为什么要用Flink,怎么入门使用Flink?
With MVC, why DDD?
pytorch中的一维、二维、三维卷积操作
关于superset集成到自己的项目中
面试官,不要再问我三次握手和四次挥手
TOGAF之架构标准规范(一)
工作流编排引擎-Temporal
C语言如何分辨大小端
MySQL optimization: from ten seconds to three hundred milliseconds
Unity Framework Design Series: How Unity Designs Network Frameworks
On-line monitoring system for urban waterlogging and water accumulation in bridges and tunnels
[Cloud Native] DevOps (5): Integrating Harbor
如何将项目部署到服务器上(全套教程)
一文了解大厂的DDD领域驱动设计
基于web3.0使用钱包Metamask的三方登陆
[MQ I can speak for an hour]
CentOS7 - yum install mysql
面试官竟然问我怎么分库分表?幸亏我总结了一套八股文
Minio上传文件ssl证书不受信任
SQL row-column conversion