当前位置:网站首页>leetcode-08
leetcode-08
2022-07-19 10:53:00 【Fairy wants carry】
The sliding window

class Solution {
public static List<Integer> findAnagrams(String s,String p){
int slength=s.length();
int plength=p.length();
ArrayList<Integer> result = new ArrayList<>();
if(slength<plength) return result;
int[] sCnt = new int[26];
int[] pCnt = new int[26];
//1. initialization p Word frequency array and s Word frequency array of , Next two positions
for (int i = 0; i < plength; i++) {
pCnt[p.charAt(i)-'a']++;
sCnt[s.charAt(i)-'a']++;
}
//2. Make an initial judgment
if (Arrays.equals(sCnt,pCnt)){
result.add(0);
}
//3. Traverse p Judge , Because I judged once before , Clear the left side every time you judge , Then move to the right once , Make a second judgment
for (int i =plength; i < slength; i++) {
sCnt[s.charAt(i-plength)-'a']--;
sCnt[s.charAt(i)-'a']++;
if (Arrays.equals(sCnt, pCnt)) {
result.add(i-plength+1);
}
}
return result;
}
}
class Solution {
public int characterReplacement(String s, int k) {
int[] num = new int[26];
int n = s.length();
int maxn = 0;
//left: Left boundary , Used to subtract the head or calculate the length when sliding
//right: Right border , Used to add a window tail or calculate the length
int left = 0, right = 0;
while (right < n) {
int indexR = s.charAt(right) - 'A';
num[indexR]++;
// Find the maximum number of times a letter has appeared in the window
// Calculate the maximum number of times a letter appears in a window , The window length can only be increased or unchanged ( Pay attention to the back left The pointer just moved 0-1 Time )
// The significance of doing so : We want the longest , If you can't find a longer hold length, the return result will not be affected
maxn = Math.max(maxn, num[indexR]);
// length len=right-left+1, hereinafter referred to as len
//len- Maximum number of letter occurrences > Number of replacements => len> Maximum number of letter occurrences + Number of replacements
// Look at the , The number of substitutions is constant =k, The maximum number of letters is possible to change , therefore , Only the maximum number of letters increases ,len To get the maximum
// Without meeting the conditions ,left and right Move together ,len constant
if (right - left + 1 - maxn > k) {
// Here we need to reduce , because left Cross this point , Will affect the maximum value
num[s.charAt(left) - 'A']--;
left++;
}
// After walking here , Actually right Will take one more step
right++;
}
// because right Take one more step , The result is (right-1)-left+1==right-left
return right - left;
}
}
边栏推荐
- Integrated network architecture and network slicing technology of air, earth and sea
- 从预测到决策,九章云极DataCanvas推出YLearn因果学习开源项目
- 线程池原理
- LeetCode 2325. Decrypt message (map)
- [Acwing]第 60 场周赛 B- 4495. 数组操作
- 【在vivado中调ila IP核】
- [leetcode weekly replay] 302 weekly 20220717
- 追根问底:Objective-C关联属性原理分析
- String类型函数传递问题
- High number_ Chapter 1 space analytic geometry and vector algebra__ Distance from point to plane
猜你喜欢
随机推荐
LeetCode 2325. 解密消息(map)
ue4对动画蓝图的理解
Satellite network capacity improvement method based on network coding
unity3d中的旋转
Pytorch学习记录2 线性回归(Tensor,Variable)
Brush questions with binary tree (2)
人大、微软等提出InclusiveFL:异构设备上的包容性联邦学习
LeetCode 2325. Decrypt message (map)
antd表单设置数组字段
过拟合与欠拟合
Win10的环境变量配置
多元线性回归详解
英伟达用AI设计GPU:最新H100已经用上,比传统EDA减少25%芯片面积
【手写数字识别】基于Lenet网络实现手写数字识别附matlab代码
高数__方程与函数的关系
Common collection properties
[acwing] 60th weekly match b- 4495 Array operation
使用tesseract.js-offline识别图片文字记录
Leetcode ugly number problem solution
博弈论(depu)与投资(40/100)




![[leetcode weekly replay] 302 weekly 20220717](/img/38/446db9b4755f8b30f9887faede7e95.png)




