随笔分类 -  哈希表

摘要:题目链接:https://leetcode-cn.com/problems/permutation-in-string 题目描述: 给你两个字符串 s1 和 s2 ,写一个函数来判断 s2 是否包含 s1 的排列。如果是,返回 true ;否则,返回 false 。 换句话说,s1 的排列之一是 s 阅读全文
posted @ 2021-10-25 11:24 张宵 阅读(52) 评论(0) 推荐(0)
摘要:题目链接:https://leetcode-cn.com/problems/roman-to-integer/ 题目描述: 题解: class Solution { public: int romanToInt(string s) { unordered_map<char, int> umap; u 阅读全文
posted @ 2021-09-18 11:04 张宵 阅读(20) 评论(0) 推荐(0)
摘要:题目链接:https://leetcode-cn.com/problems/number-of-boomerangs 题目描述: 给定平面上 n 对 互不相同 的点 points ,其中 points[i] = [xi, yi] 。回旋镖 是由点 (i, j, k) 表示的元组 ,其中 i 和 j  阅读全文
posted @ 2021-09-13 16:51 张宵 阅读(44) 评论(0) 推荐(0)
摘要:题目链接:https://leetcode-cn.com/problems/longest-palindrome 题目描述: 给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串。 在构造过程中,请注意区分大小写。比如 "Aa" 不能当做一个回文字符串。 注意: 假设字符串的 阅读全文
posted @ 2021-08-11 10:44 张宵 阅读(29) 评论(0) 推荐(0)
摘要:题目链接:https://leetcode-cn.com/problems/remove-duplicate-letters/ 题目描述: 题解: 1.遍历字符串,记录字符出现次数。 2.定义标记数组,记录字符是否重复出现过。 3.为了确保返回结果的字典序最小,使用单调栈。 class Soluti 阅读全文
posted @ 2021-07-17 21:30 张宵 阅读(84) 评论(0) 推荐(0)
摘要:题目链接:https://leetcode-cn.com/problems/partition-labels/ 题目描述: 题解: 解题思路:划分字母区间 1.统计每一个字符最后出现的位置 2.从头遍历字符,并更新字符的最远出现下标,如果找到字符最远出现位置下标和当前下标相等了,则找到了分割点 cl 阅读全文
posted @ 2021-06-30 11:02 张宵 阅读(60) 评论(0) 推荐(0)
摘要:题目链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/ 题目描述: 题解: class Solution { public: int lengthOfLongestSubstring( 阅读全文
posted @ 2021-06-20 17:51 张宵 阅读(26) 评论(0) 推荐(0)
摘要:题目链接: https://leetcode-cn.com/problems/lemonade-change/ 题目描述: 题解: class Solution { public: bool lemonadeChange(vector<int>& bills) { unordered_map<int 阅读全文
posted @ 2021-06-17 11:26 张宵 阅读(30) 评论(0) 推荐(0)
摘要:题解: 方法1: 哈希表:遍历所有节点,每次遍历到一个节点时,判断该节点是否被访问过。 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x 阅读全文
posted @ 2021-06-07 19:34 张宵 阅读(68) 评论(0) 推荐(0)
摘要:题目链接:https://leetcode-cn.com/problems/valid-anagram/ 题目描述: 题解: class Solution { public: bool isAnagram(string s, string t) { unordered_map<char, int> 阅读全文
posted @ 2021-05-29 22:28 张宵 阅读(45) 评论(0) 推荐(0)
摘要:题目链接:https://leetcode-cn.com/problems/top-k-frequent-words 题目描述: 给一非空的单词列表,返回前 k 个出现次数最多的单词。 返回的答案应该按单词出现频率由高到低排序。如果不同的单词有相同出现频率,按字母顺序排序。 示例 1: 输入: [" 阅读全文
posted @ 2021-05-20 19:40 张宵 阅读(76) 评论(0) 推荐(0)
摘要:题目链接:https://leetcode-cn.com/problems/reconstruct-itinerary 题目描述: 给定一个机票的字符串二维数组 [from, to],子数组中的两个成员分别表示飞机出发和降落的机场地点,对该行程进行重新规划排序。所有这些机票都属于一个从 JFK(肯尼 阅读全文
posted @ 2021-04-01 10:08 张宵 阅读(92) 评论(0) 推荐(0)
摘要:题目链接:https://leetcode-cn.com/problems/design-hashmap 题目描述: 不使用任何内建的哈希表库设计一个哈希映射(HashMap)。 实现 MyHashMap 类: MyHashMap() 用空映射初始化对象 void put(int key, int 阅读全文
posted @ 2021-03-14 18:46 张宵 阅读(74) 评论(0) 推荐(0)
摘要:题目链接:https://leetcode-cn.com/problems/design-hashset 题目描述: 不使用任何内建的哈希表库设计一个哈希集合(HashSet)。 实现 MyHashSet 类: void add(key) 向哈希集合中插入值 key 。 bool contains( 阅读全文
posted @ 2021-03-13 16:20 张宵 阅读(140) 评论(0) 推荐(0)
摘要:3.四数之和 题目链接:https://leetcode-cn.com/problems/4sum 题目描述: 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等 阅读全文
posted @ 2021-01-25 15:53 张宵 阅读(71) 评论(0) 推荐(0)
摘要:1.两数之和 题目链接:https://leetcode-cn.com/problems/two-sum 题目描述: 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 的那 两个 整数,并返回它们的数组下标。 你可以假设每种输入只会对应一个答案。但是,数组中 阅读全文
posted @ 2021-01-24 19:06 张宵 阅读(366) 评论(0) 推荐(0)
摘要:题目链接: 题目描述:https://leetcode-cn.com/problems/happy-number 编写一个算法来判断一个数 n 是不是快乐数。 「快乐数」定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是 无限循环 但始 阅读全文
posted @ 2021-01-23 11:26 张宵 阅读(72) 评论(0) 推荐(0)
摘要:题目链接:https://leetcode-cn.com/problems/valid-anagram 题目描述: 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。 示例 1: 输入: s = "anagram", t = "nagaram" 输出: true 示例 阅读全文
posted @ 2021-01-22 19:07 张宵 阅读(104) 评论(0) 推荐(0)