当前位置:网站首页>剑指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;
}
}
边栏推荐
- MySQL (updating)
- MySQL优化:从十几秒优化到三百毫秒
- MySQL transaction (transaction) (this is enough..)
- Paginate the list collection and display the data on the page
- Numpy中np.meshgrid的简单用法示例
- Centos7 install mysql5.7 steps (graphical version)
- Sql解析转换之JSqlParse完整介绍
- 账号或密码多次输入错误,进行账号封禁
- MySQL-如何分库分表?一看就懂
- Multiple table query of sql statement
猜你喜欢
随机推荐
mysql存储过程
PCL calculates the point cloud coordinate maximum and its index
Numpy中np.meshgrid的简单用法示例
面试官:生成订单30分钟未支付,则自动取消,该怎么实现?
The MySQL database installed configuration nanny level tutorial for 8.0.29 (for example) have hands
MySQL事务隔离级别详解
With MVC, why DDD?
CentOS7 - yum install mysql
MySQL_关于JSON数据的查询
为什么要用Flink,怎么入门使用Flink?
numpy和pytorch中的元素拼接操作:stack,concatenat,cat
【mysql 提高查询效率】Mysql 数据库查询好慢问题解决
110 MySQL interview questions and answers (continuously updated)
mysql5.7.35安装配置教程【超级详细安装教程】
Shell重油常压塔模拟仿真与控制
【JS面试题】面试官:“[1,2,3].map(parseInt)“ 输出结果是什么?答上来就算你通过面试
CentOS7 —— yum安装mysql
基于web3.0使用钱包Metamask的三方登陆
面试官竟然问我怎么分库分表?幸亏我总结了一套八股文
【ORACLE Explain 详解】