当前位置:网站首页>剑指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;
}
}
边栏推荐
- Minio上传文件ssl证书不受信任
- 快速掌握并发编程 --- 基础篇
- Interview Redis High Reliability | Master-Slave Mode, Sentinel Mode, Cluster Cluster Mode
- sql statement - how to query data in another table based on the data in one table
- ES 源码 API调用链路源码分析
- 面试官:生成订单30分钟未支付,则自动取消,该怎么实现?
- torch.normal函数用法
- Paginate the list collection and display the data on the page
- 对list集合进行分页,并将数据显示在页面中
- 【MySQL8入门到精通】基础篇- Linux系统静默安装MySQL,跨版本升级
猜你喜欢
About the problems encountered by Xiaobai installing nodejs (npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)
Unity mobile game performance optimization series: performance tuning for the CPU side
分布式事务处理方案大 PK!
账号或密码多次输入错误,进行账号封禁
DVWA安装教程(懂你的不懂·详细)
面试Redis 高可靠性|主从模式、哨兵模式、Cluster集群模式
[Cloud Native] DevOps (5): Integrating Harbor
Go language study notes - dealing with timeout problems - Context usage | Go language from scratch
A complete introduction to JSqlParse of Sql parsing and conversion
ERP Production Operation Control Kingdee
随机推荐
CentOS7 安装MySQL 图文详细教程
MySQL transaction (transaction) (this is enough..)
Mysql——字符串函数
.NET-6.WinForm2.NanUI learning and summary
Simple read operation of EasyExcel
Linux的mysql报ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘ (using password NOYSE)
Linux系统安装mysql(rpm方式安装)
<urlopen error [Errno 11001] getaddrinfo failed>的解决、isinstance()函数初略介绍
tf.keras.utils.pad_sequences()
Interviewer: If the order is not paid within 30 minutes, it will be automatically canceled. How to do this?
Moment Pool Cloud quickly installs packages such as torch-sparse and torch-geometric
【ORACLE Explain 详解】
Interviewer, don't ask me to shake hands three times and wave four times again
可点击也可直接复制指定内容js
【一起学Rust】Rust的Hello Rust详细解析
精解四大集合框架:List 核心知识总结
Flink sink redis 写入Redis
SQL row-column conversion
Unity resources management series: Unity framework how to resource management
docker安装postgresSQL和设置自定义数据目录