字符串、线性表、队列、栈、哈希表、dfs、bfs

发布时间 2023-10-29 22:09:49作者: 爱情丶眨眼而去

题目列表:

1. 字符串

  1. 无重复字符的最长子串 (中等难度)
    给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
AC代码,展开查看
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int res = 0;
        unordered_map<char, int> heap;
        for(int i = 0, j = 0; i < s.size(); i ++ ){
            heap[s[i]] ++ ;
            while(heap[s[i]] > 1) heap[s[j ++ ]] -- ;
            res = max(res, i - j + 1); 
        }
        return res;
    }
};
  1. 字母异位词分组
    给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。
    字母异位词 是由重新排列源单词的所有字母得到的一个新单词。
AC代码,展开查看
class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        unordered_map<string, vector<string>> hash;
        for(const auto &str : strs){
            string tmp = str;
            sort(tmp.begin(), tmp.end());
            hash[tmp].push_back(str);
        }
        vector<vector<string>> res;
        for(const auto &[k, v] : hash){
            res.push_back(v);
        }
        return res;
    }
};

2. 线性表

3. 队列

4. 栈

5. 哈希表

6. dfs

7. bfs