滑动窗口的方法解决一些String的查找问题
来源:互联网 发布:怎么写好网络小说知乎 编辑:程序博客网 时间:2024/06/06 01:06
今天来介绍一种“滑动窗口”算法,可以用来解决一些子串查找问题
1.查找同字母异序词
题目描述:提供一个字符串s和一个非空字符串t,求出字符串t在字符串s中的所有同字母异序词的起始位置
示例代码:
public class Solution { public List<Integer> findAnagrams(String s, String t) { List<Integer> result = new LinkedList<>(); if(t.length()> s.length()) return result; Map<Character, Integer> map = new HashMap<>(); for(char c : t.toCharArray()){ map.put(c, map.getOrDefault(c, 0) + 1); } int counter = map.size(); int begin = 0, end = 0; int head = 0; int len = Integer.MAX_VALUE; while(end < s.length()){ char c = s.charAt(end); if( map.containsKey(c) ){ map.put(c, map.get(c)-1); if(map.get(c) == 0) counter--; } end++; while(counter == 0){ char tempc = s.charAt(begin); if(map.containsKey(tempc)){ map.put(tempc, map.get(tempc) + 1); if(map.get(tempc) > 0){ counter++; } } if(end-begin == t.length()){ result.add(begin); } begin++; } } return result; }}
2.查找最长不重复子串
题目描述:给出一个字符串,求出这个字符串中最长不重复子串的长度
Given "abcabcbb", the answer is "abc", which the length is 3.Given "bbbbb", the answer is "b", with the length of 1.Given "pwwkew", the answer is "wke", with the length of 3.
示例代码:
public class Solution { public int lengthOfLongestSubstring(String s) { Map<Character, Integer> map = new HashMap<>(); int begin = 0, end = 0, counter = 0, d = 0; while (end < s.length()) { char c = s.charAt(end); map.put(c, map.getOrDefault(c, 0) + 1); if(map.get(c) > 1) counter++; end++; while (counter > 0) { char charTemp = s.charAt(begin); if (map.get(charTemp) > 1) counter--; map.put(charTemp, map.get(charTemp)-1); begin++; } d = Math.max(d, end - begin); } return d; }}
3.同类问题的解决思路:
public class Solution { public List<Integer> slidingWindowTemplateByHarryChaoyangHe(String s, String t) { //定义一个list来存储结果 List<Integer> result = new LinkedList<>(); if(t.length()> s.length()) return result; //用到的数据结构主要是hashMap,用来存放目标子串中的字符 //(K, V) = (Character, Frequence of the Characters) Map<Character, Integer> map = new HashMap<>(); for(char c : t.toCharArray()){ map.put(c, map.getOrDefault(c, 0) + 1); } int counter = map.size();//值必须是map的size,这样可以保证重复的字符不会重复记录 //begin和end指针,共同确定滑动窗口的位置 int begin = 0, end = 0; //来记录结果字符串的长度,先定义为最大整数 int len = Integer.MAX_VALUE; //遍历源字符串s,循环条件:end < s.length(); while(end < s.length()){ char c = s.charAt(end); if( map.containsKey(c) ){ map.put(c, map.get(c)-1); if(map.get(c) == 0) counter--;//通过计算counter的值来记录状态 } end++; while(counter == 0 /*这里的判断条件要根据题目需求来定*/){ char tempc = s.charAt(begin); if(map.containsKey(tempc)){//恢复map中的值和counter的值,以便进行下一个窗口的判断 map.put(tempc, map.get(tempc) + 1); if(map.get(tempc) > 0) counter++; } /*存储,更新结果,这里具体操作根据题目而定*/ begin++; } } return result; } }
阅读全文
0 0
- 滑动窗口的方法解决一些String的查找问题
- FragmentTabHost的应用,完美解决滑动的时候,一些问题
- 字符串的窗口滑动问题
- 滑动窗口的最小值问题
- 一些String的方法
- String 的一些问题
- 解决ScrollView滑动过程中ScrollTo()方法不起作用的问题
- 解决ScrollView滑动过程中ScrollTo()方法不起作用的问题
- 解决 ScrollView 滑动过程中 ScrollTo() 方法不起作用的问题
- 解决ScrollView滑动过程中ScrollTo()方法不起作用的问题
- 解决ScrollView滑动过程中ScrollTo()方法不起作用的问题
- IE6中“窗口正忙,关闭该窗口将导致一些问题,是否关闭该窗口”问题的解决
- 一些问题的解决
- 一些问题的解决
- String.CopyTo方法和java多线程的一些问题整理
- String方法的一些介绍
- String字符串的一些方法
- String的一些常用方法
- 转移 MySql 的数据目录
- [leetcode: Python]500.Keyboard Row
- Android N 添加系统服务Freg
- spark 2.1 the runJob process
- Android上的RN不能展示尺寸大的图,怎么办?
- 滑动窗口的方法解决一些String的查找问题
- Android 禁止状态栏下拉
- lintcode 在二叉查找树中插入节点
- 制作mac系统正式版U盘USB启动安装盘方法教程 (全新安装 Mac 系统)
- CentOS6.7 上面安装Docker 1.7.1
- zeppelin spark SparkContext问题 Cannot call methods on a stopped SparkContext
- 静态编译Qt
- AIDL基本使用4—- 4、linkToDeath和unlinkToDeath
- the object has no symbol