当前位置:网站首页>剑指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语句中对时间字段进行区间查询
- 关于小白安装nodejs遇到的问题(npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)
- 12 reasons for MySQL slow query
- Why use Flink and how to get started with Flink?
- Distributed Transactions - Introduction to Distributed Transactions, Distributed Transaction Framework Seata (AT Mode, Tcc Mode, Tcc Vs AT), Distributed Transactions - MQ
- Mysql application cannot find my.ini file after installation
- The interviewer asked me TCP three handshake and four wave, I really
- Typec手机有线网卡网线转网口转接口快充方案
- On-line monitoring system for urban waterlogging and water accumulation in bridges and tunnels
- 【LeetCode-SQL每日一练】——2. 第二高的薪水
猜你喜欢
随机推荐
Simple read operation of EasyExcel
[Cloud Native] DevOps (5): Integrating Harbor
datagrip带参sql查询
Distributed transaction processing solution big PK!
DVWA靶场环境搭建
STM32 - DMA
MySQL transaction (transaction) (this is enough..)
DVWA installation tutorial (understand what you don't understand · in detail)
MySQL transaction isolation level, rounding
Three oj questions on leetcode
关于LocalDateTime的全局返回时间带“T“的时间格式处理
DVWA之SQL注入
Temporal介绍
2022-07-30:以下go语言代码输出什么?A:[]byte{} []byte;B:[]byte{} []uint8;C:[]uint8{} []byte;D:[]uin8{} []uint8。
With MVC, why DDD?
数据集划分以及交叉验证法
[Introduction to MySQL 8 to Mastery] Basics - silent installation of MySQL on Linux system, cross-version upgrade
面试官问我TCP三次握手和四次挥手,我真的是
Flink sink redis 写入Redis
mysql stored procedure