当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢
目标检测学习笔记
Interviewer, don't ask me to shake hands three times and wave four times again
【LeetCode-SQL每日一练】——2. 第二高的薪水
Typec手机有线网卡网线转网口转接口快充方案
为什么要用Flink,怎么入门使用Flink?
剑指offer基础版 ---- 第26天
【mysql 提高查询效率】Mysql 数据库查询好慢问题解决
Summary of MySQL common interview questions (recommended collection!!!)
剑指offer基础版 ----第31天
mysql uses on duplicate key update to update data in batches
随机推荐
剑指offer基础版 --- 第21天
MySQL transaction isolation level, rounding
Temporal客户端模型
SQL statement to range query time field
What are the advantages and disadvantages of Unity shader forge and the built-in shader graph?
Flask 的初识
[Introduction to MySQL 8 to Mastery] Basics - silent installation of MySQL on Linux system, cross-version upgrade
Input length must be multiple of 8 when decrypting with padded cipher
CentOS7 —— yum安装mysql
Data set partitioning and cross-validation
限流的原理
Flink sink ES 写入 ES(带密码)
Reference code series_1. Hello World in various languages
About the problems encountered by Xiaobai installing nodejs (npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)
剑指offer专项突击版 ---- 第2天
关于小白安装nodejs遇到的问题(npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)
The interviewer asked me TCP three handshake and four wave, I really
Summary of MySQL common interview questions (recommended collection!!!)
TOGAF之架构标准规范(一)
Information System Project Manager Core Test Site (55) Configuration Manager (CMO) Work