Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 角点情况下的滑动窗口算法_Algorithm_Data Structures_Sliding Window_Array Algorithms - Fatal编程技术网

Algorithm 角点情况下的滑动窗口算法

Algorithm 角点情况下的滑动窗口算法,algorithm,data-structures,sliding-window,array-algorithms,Algorithm,Data Structures,Sliding Window,Array Algorithms,在LeetCode 438上工作 我的示例代码和解释粘贴在下面。最优化的解决方案使用更少的空间,而我的解决方案只使用很少的额外空间,但仍然是线性的。然而,我无法用一些小数据找出它可能失败的地方。我试过的大多数样本数据都通过了。提交时失败 // dict map denotes the characters of 'p' with their actual frequency // found map denotes the characters that found tracks with th

在LeetCode 438上工作

我的示例代码和解释粘贴在下面。最优化的解决方案使用更少的空间,而我的解决方案只使用很少的额外空间,但仍然是线性的。然而,我无法用一些小数据找出它可能失败的地方。我试过的大多数样本数据都通过了。提交时失败

// dict map denotes the characters of 'p' with their actual frequency
// found map denotes the characters that found tracks with their frequency
// as the window of size 'p' moves from start of 's' forward
// As long as the character frequency seen in 'found' ie in the sliding window is
// lesser than in dict, we keep increasing 'count' variable. 'Count' variable
// must never be greater than length of 'p'. Similarly when a character
// falls off the window as window moves to right, we decrease count
// only if tht character is not present in higher frequency.
public List<Integer> findAnagrams(String s, String p) {
    List<Integer> lst = new ArrayList<>();

    Map<Character, Integer> dict = new HashMap<>();
    Map<Character, Integer> found = new HashMap<>();

    for (char ch : p.toCharArray()) {
        if (dict.containsKey(ch)) {
            dict.put(ch, dict.get(ch) + 1);
        } else {
            dict.put(ch, 1);
        }
    }

    int count = 0;
    int i = 0;
    for (i = 0; i < s.length(); i++) {
        if (count == p.length()) {
            lst.add(i - p.length());
        }
        if (i - p.length() >= 0) {
            char sc = s.charAt(i - p.length());
            if (dict.containsKey(sc)) {
                if (found.get(sc) == dict.get(sc)) {
                    count--;
                }
                found.put(sc, found.get(sc) - 1);
            }
        }
        char ch = s.charAt(i);
        if (dict.containsKey(ch)) {
            if (found.containsKey(ch)) {
                if (found.get(ch) < dict.get(ch)) {
                    count++;
                }
                found.put(ch, found.get(ch) + 1);
            } else {
                found.put(ch, 1);
                count++;
            }
        }
    }
    if (count == p.length()) {
        lst.add(i - p.length());
    }
    return lst;
}
//dict map表示“p”的字符及其实际频率
//找到的地图表示找到轨迹的字符及其频率
//随着大小为“p”的窗口从“s”的开头向前移动
//只要在滑动窗口中的“找到”ie中看到的字符频率为
//小于dict,我们不断增加“count”变量计数变量
//不得大于“p”的长度。同样,当一个角色
//当窗口向右移动时从窗口上掉下来,我们减少计数
//仅当tht字符在较高频率中不存在时。
公共列表FindAgrams(字符串s、字符串p){
List lst=new ArrayList();
Map dict=newhashmap();
找到的映射=新的HashMap();
for(char ch:p.toCharArray()){
if(主语(ch)){
dict.put(ch,dict.get(ch)+1);
}否则{
dict.put(第1章);
}
}
整数计数=0;
int i=0;
对于(i=0;i=0){
char sc=s.charAt(i-p.length());
if(主旨(sc)){
if(find.get(sc)=dict.get(sc)){
计数--;
}
发现。放置(sc,发现。获取(sc)-1);
}
}
char ch=s.charAt(i);
if(主语(ch)){
如果(发现。容器(ch)){
if(find.get(ch)
第一个for循环还可以,但第二个很难理解。同样,这个问题也可以不用Map来解决。因为字符串是由小写字母组成的,所以只能使用26个字符。因此,使用Map是不必要的。我将创建两个数组
subar
target
,每个数组的大小为26<代码>目标将包含
p
中字符的频率。因此,
目标
将是一个
常数
。现在使用滑动窗口,计算
子字符串
字符的频率,并将其存储在
子字符串
中。最后,如果数组
target
subrr
相等,则将
substring
的起始索引添加到应答列表中。对数组
s
的其余子字符串重复此操作。使用哈希创建字符频率数组“subar”。示例:
用于(int i=0;问题不在于映射的使用,尽管用数组作为计数器替换会略微提高效率是正确的。算法描述看起来也很正确,但代码并不完全匹配。简化第二个循环中的逻辑,并在简单输入上手动跟踪算法(可以使用调试器,最重要的是跟踪您的状态)应该可以帮助您找出错误。