当前位置:网站首页>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;
}
}
边栏推荐
- mysql5.7.35安装配置教程【超级详细安装教程】
- 面试官,不要再问我三次握手和四次挥手
- On-line monitoring system for urban waterlogging and water accumulation in bridges and tunnels
- C语言教程(三)-if和循环
- 快速掌握并发编程 --- 基础篇
- MySQL optimization slow log query
- 信息系统项目管理师核心考点(五十五)配置管理员(CMO)的工作
- Temporal介绍
- CentOS7 - yum install mysql
- Summary of MySQL common interview questions (recommended collection!!!)
猜你喜欢

STM32——DMA

The interviewer asked me how to divide the database and the table?Fortunately, I summed up a set of eight-part essays

限流的原理

剑指offer基础版 ---- 第29天

MySQL transaction isolation level, rounding

110 MySQL interview questions and answers (continuously updated)

MYSQL一站式学习,看完即学完

Goodbye to the cumbersome Excel, mastering data analysis and processing technology depends on it

ES source code API call link source code analysis

Refinement of the four major collection frameworks: Summary of List core knowledge
随机推荐
精解四大集合框架:List 核心知识总结
运用flask框架发送短信验证码的流程及具体代码
剑指offer专项突击版 --- 第 3 天
Redis进阶 - 缓存问题:一致性、穿击、穿透、雪崩、污染等.
C语言实验一 熟悉C程序的环境
C语言教程(三)-if和循环
Centos7 install mysql5.7 steps (graphical version)
Minio upload file ssl certificate is not trusted
mysql stored procedure
C语言实验五 循环结构程序设计(二)
MySQL window function
剑指offer专项突击版 --- 第 4 天
剑指offer专项突击版 ---- 第2天
可点击也可直接复制指定内容js
Redis的初识
C语言教程(一)-准备
Temporal线上部署
C语言实验四 循环结构程序设计(一)
tf.keras.utils.pad_sequences()
有了MVC,为什么还要DDD?