当前位置:网站首页>Swordsman Offer Special Assault Edition ---- Day 6
Swordsman Offer Special Assault Edition ---- Day 6
2022-07-31 05:32:00 【Milan's little red and black】

class Solution {
public String minWindow(String s, String t) {
int n = s.length(),m = t.length();
if(n<m) return "";
int[] cnt = new int[150];
int k = 0;
for(char c:t.toCharArray()){
if(++cnt[c]==1){
k++;
}
}
int l = 0,left = 0,right = 0;
for(int i = 0;i<s.length();i++){
char c = s.charAt(i);
if(--cnt[c]==0) k--;
if(k==0){
while(cnt[s.charAt(l)]<0){
++cnt[s.charAt(l++)];
}
if(left==right||right-left>i-l+1){
right = i+1;
left = l;
}
}
}
return s.substring(left,right);
}
}

class Solution {
public boolean isPalindrome(String s) {
StringBuilder str = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (Character.isLetterOrDigit(c)) {
str.append(Character.toLowerCase(c));
}
}
return str.toString().equals(new StringBuffer(str).reverse().toString());
}
}

class Solution {
public boolean validPalindrome(String s) {
for(int left = 0, right = s.length() - 1; left < right; left++, right--){
// 如果不相等,则分两种情况:删除左边的元素,或者右边的元素,再判断各自是否回文,满足一种即可.
if(s.charAt(left) != s.charAt(right))
return isPalindrome(s, left+1, right) || isPalindrome(s, left, right - 1);
}
return true;
}
// 判断字符串 s 的 [left, right] 是否回文
private boolean isPalindrome(String s, int left , int right){
while (left < right){
if (s.charAt(left++) != s.charAt(right--))
return false;
}
return true;
}
}

class Solution {
public int countSubstrings(String s) {
int n = s.length(), ans = 0;
for (int i = 0; i < 2 * n - 1; ++i) {
int l = i / 2, r = i / 2 + i % 2;
while (l >= 0 && r < n && s.charAt(l) == s.charAt(r)) {
--l;
++r;
++ans;
}
}
return ans;
}
}
边栏推荐
- 为什么要用Flink,怎么入门使用Flink?
- 剑指offer基础版 --- 第24天
- 限流的原理
- 剑指offer基础版 ----- 第28天
- Paginate the list collection and display the data on the page
- 剑指offer基础版 ----- 第25天
- Redis Advanced - Cache Issues: Consistency, Penetration, Penetration, Avalanche, Pollution, etc.
- 快速掌握并发编程 --- 基础篇
- 剑指offer专项突击版 ---- 第2天
- 剑指offer基础版 ---- 第26天
猜你喜欢
随机推荐
ABC D - Distinct Trio (Number of k-tuples
一文了解大厂的DDD领域驱动设计
精解四大集合框架:List 核心知识总结
Goodbye to the cumbersome Excel, mastering data analysis and processing technology depends on it
C语言指针详解
Flink sink redis 写入Redis
tf.keras.utils.pad_sequences()
【mysql 提高查询效率】Mysql 数据库查询好慢问题解决
Flink sink ES 写入 ES(带密码)
MYSQL下载及安装完整教程
C语言实验一 熟悉C程序的环境
剑指offer基础版 --- 第21天
面试官竟然问我怎么分库分表?幸亏我总结了一套八股文
Three handshakes and four waves
Apache DButils使用注意事项--with modifiers “public“
CentOS7 —— yum安装mysql
再见了繁琐的Excel,掌握数据分析处理技术就靠它了
C语言如何分辨大小端
Refinement of the four major collection frameworks: Summary of List core knowledge
面试Redis 高可靠性|主从模式、哨兵模式、Cluster集群模式









