道阻且长,行则将至,走慢一点没关系,不停下就好了.|

Ac_c0mpany丶

园龄:3年7个月粉丝:6关注:3

2023-12-04 14:48阅读: 2评论: 0推荐: 0

LeetCode567. 字符串的排列

题目描述

image.png

思路:滑动窗口模板

  • 定义需要维护的变量
Map<Character, Integer> map = new HashMap<>();
Map<Character, Integer> map_s1 = new HashMap<>();
for (char c : s1.toCharArray()) {
	map_s1.put(c, map_s1.getOrDefault(c, 0) + 1);
}
  • 根据题意可知:窗口为固定大小所以用if
 if (end - start + 1 == s1.length()) {
	char startChar = s2.charAt(start);
	map.put(startChar, map.get(startChar) - 1);
	if (map.get(startChar) == 0) {
		map.remove(startChar);
	}
	start ++;
}

方法一:

class Solution {
    public boolean checkInclusion(String s1, String s2) {
        // 1. 定义需要维护的变量
        Map<Character, Integer> map = new HashMap<>();
        Map<Character, Integer> map_s1 = new HashMap<>();
        for (char c : s1.toCharArray()) {
            map_s1.put(c, map_s1.getOrDefault(c, 0) + 1);
        }

        // 2. 定义窗口边界
        int start = 0;
        for (int end = 0; end < s2.length(); end ++) {
            // 3. 更新需要维护的变量
            char currentChar = s2.charAt(end);
            map.put(currentChar, map.getOrDefault(currentChar, 0) + 1);
            if (map.equals(map_s1)) {
                return true;
            }
            if (end - start + 1 == s1.length()) {
                char startChar = s2.charAt(start);
                map.put(startChar, map.get(startChar) - 1);
                if (map.get(startChar) == 0) {
                    map.remove(startChar);
                }
                start ++;
            }
        }
        return false;
    }
}

本文作者:Ac_c0mpany丶

本文链接:https://www.cnblogs.com/keyongkang/p/17874907.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Ac_c0mpany丶  阅读(2)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 You Are My Sunshine REOL
You Are My Sunshine - REOL
00:00 / 00:00
An audio error has occurred.

作曲 : Traditional

You are my sunshine

My only sunshine.

You make me happy

When skies are gray.

You'll never know, dear,

How much I love you.

Please don't take my sunshine away

The other night, dear,

When I lay sleeping

I dreamed I held you in my arms.

When I awoke, dear,

I was mistaken

So I hung my head and cried.

You are my sunshine,

My only sunshine.

You make me happy

When skies are gray.

You'll never know, dear,

How much I love you.

Please don't take my sunshine away.

You are my sunshine,

My only sunshine

You make me happy

When skies are gray.

You'll never know, dear

How much I love you

Please don't take my sunshine away

Please don't take my sunshine away.

Please don't take my sunshine away.