当前位置:网站首页>剑指offer专项突击版 ---- 第 6 天
剑指offer专项突击版 ---- 第 6 天
2022-07-31 05:09:00 【米兰的小红黑】

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;
}
}
边栏推荐
- torch.normal函数用法
- Temporal对比Cadence
- 目标检测学习笔记
- Interviewer, don't ask me to shake hands three times and wave four times again
- 关于superset集成到自己的项目中
- MySQL开窗函数
- 【一起学Rust】Rust学习前准备——注释和格式化输出
- 数据集划分以及交叉验证法
- MySQL优化:从十几秒优化到三百毫秒
- Goodbye to the cumbersome Excel, mastering data analysis and processing technology depends on it
猜你喜欢

Blockbuster | foundation for platinum, gold, silver gave nameboards donors

面试官竟然问我怎么分库分表?幸亏我总结了一套八股文

基于web3.0使用钱包Metamask的三方登陆

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

账号或密码多次输入错误,进行账号封禁

Distributed transaction processing solution big PK!

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

ES 源码 API调用链路源码分析

MySQL-Explain详解

With MVC, why DDD?
随机推荐
Mysql应用安装后找不到my.ini文件
Why use Flink and how to get started with Flink?
Tapdata 与 Apache Doris 完成兼容性互认证,共建新一代数据架构
Flink sink redis 写入Redis
SQL行列转换
Distributed Transactions - Introduction to Distributed Transactions, Distributed Transaction Framework Seata (AT Mode, Tcc Mode, Tcc Vs AT), Distributed Transactions - MQ
CentOS7 安装MySQL 图文详细教程
torch.normal函数用法
DVWA之SQL注入
MySQL-Explain详解
Numpy中np.meshgrid的简单用法示例
Mysql application cannot find my.ini file after installation
mysql uses on duplicate key update to update data in batches
【一起学Rust】Rust的Hello Rust详细解析
MySQL_关于JSON数据的查询
matlab simulink欠驱动水面船舶航迹自抗扰控制研究
MySQL事务隔离级别详解
ABC D - Distinct Trio (Number of k-tuples
110 MySQL interview questions and answers (continuously updated)
STM32 - DMA