随笔分类 - LeetCode
摘要:思路 方法一:暴力枚举 最容易想到的方法是枚举数组中的每一个数 x,寻找数组中是否存在 target - x。 当我们使用遍历整个数组的方式寻找 target - x 时,需要注意到每一个位于 x 之前的元素都已经和 x 匹配过,因此不需要再进行匹配。而每一个元素不能被使用两次,所以我们只需要在 x
阅读全文
摘要:思路 方法:哈希表 1 class Solution { 2 public: 3 int leastBricks(vector<vector<int>>& wall) { 4 unordered_map<int, int> mp; 5 for(int i = 0; i < wall.size();
阅读全文
摘要:思路 方法:前缀和 记数组的全部元素之和为sum,当遍历到第 i 个元素时,设其左侧元素之和为 tmpSum,则其右侧元素之和为 sum-nums[i]-tmpSum。左右侧元素相等即为 tmpSum = sum-nums[i]-tmpSum。 1 class Solution { 2 public
阅读全文
摘要:思路 1 class Solution { 2 public: 3 bool judgePoint24(vector<int>& nums) { 4 vector<double> digits; 5 for (int num : nums) { 6 digits.push_back((double)
阅读全文
摘要:思路 方法一:暴力法 对每一个数,都向两边一一扩散,寻找山脉。 1 class Solution { 2 public: 3 int longestMountain(vector<int>& arr) { 4 int n = arr.size(); 5 6 int maxMountainLen =
阅读全文
摘要:思路 方法:并查集 并查集模板题。注意:并查集路径压缩后的查找根节点函数的最坏复杂度为O(logn),平均时间复杂度为 O(α(n)),这里α 表示阿克曼函数的反函数,在宇宙可观测的 n 内(例如宇宙中包含的粒子总数),α(n)不会超过 5。 具体解释见这里:并查集各种情况下的时间复杂度 1 cla
阅读全文
摘要:思路 方法一:暴力法,双重循环判断 1 class Solution { 2 public: 3 //暴力法,时间复杂度O(n^2),提交之后会超时,不能accept 4 string longestPalindrome(string s) { 5 int slen = s.length(); 6
阅读全文
摘要:思路 方法:层序遍历 1 class Solution { 2 public: 3 vector<vector<int>> zigzagLevelOrder(TreeNode* root) { 4 vector<vector<int>> ans; 5 if (!root) { 6 return an
阅读全文
摘要:思路 哈希表 + 双向链表。在面试中,面试官一般会期望读者能够自己实现一个简单的双向链表,而不是使用语言自带的、封装好的数据结构。 方法一:哈希表 + C++自带的双向链表 list 1 struct Node { 2 int key; 3 int value; 4 Node(){} 5 Node(
阅读全文
摘要:思路 贪心思想,先对小孩的胃口和饼干的尺寸进行从小到大排序,每次选出能满足该小孩的最小饼干。 1 class Solution { 2 public: 3 int findContentChildren(vector<int>& g, vector<int>& s) { 4 // 先对小孩和饼干从小
阅读全文
摘要:思路 贪心算法 因为区间的右边界end越小,可以给后面留到的空间就越大。所以按照区间的右边界从小到大进行排序,每次都选右边界最小并且左边界大于前一个区间右边界的区间,最后可以累加得到最大不重叠区间的个数m,最后用总区间个数-m即为要删去的最小区间数。 1 class Solution { 2 pri
阅读全文
摘要:思路 方法一:回溯 + 剪枝 生成所有2n个‘(’ 和“)”构成的序列,逐一判断生成的序列是否是有效的括号组合。 这里可以剪枝:在左括号/右括号的数量到达n的时候,将剩余的(2n - 左括号数-右括号数)数量 的右括号/左括号补到字符串末尾,然后对此字符串进行判断。 判断方法有两种: (1)使用栈进
阅读全文
摘要:思路 首先创建字典树,之后对字典树进行dfs搜索。 代码实现 1 class Trie { 2 public: 3 bool isWord; 4 Trie* next[26]; 5 6 void insert(const string word) { 7 Trie* t = this; 8 for(
阅读全文
摘要:思路 方法:字典树 + 贪心 1. 将数组中的数全部存入字典树中2. 遍历树中的每一个数在字典树中异或的最大结果,最后再求最大结果里面的最大值返回 代码实现 1 class Solution { 2 class Trie { 3 public: 4 Trie* next[2] = {NULL}; 5
阅读全文
摘要:思路 字典树,边插入边更新sum 代码实现 1 class MapSum { 2 class Trie { 3 public: 4 bool isWord = false; 5 int value = 0; 6 int sum = 0; 7 Trie* next[26] = {NULL}; 8 9
阅读全文
摘要:思路 先将所有单词存入字典树。对于每个单词,在字典树中检查它的全部前缀是否存在。 代码实现 1 class Solution { 2 3 class Trie { 4 public: 5 bool isWord = false; 6 Trie* next[26] = {NULL}; 7 8 Trie
阅读全文
摘要:1 class Solution { 2 public: 3 bool isPalindrome(ListNode* head) { 4 deque<int> d1, d2; 5 ListNode* p = head; 6 while (p != NULL) 7 { 8 d1.push_back(p
阅读全文
摘要:1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * };
阅读全文
摘要:给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。 有效字符串需满足: 左括号必须用相同类型的右括号闭合。 左括号必须以正确的顺序闭合。 注意空字符串可被认为是有效字符串。 示例 1: 输入: "()" 输出: true 示例 2: 输入: "()[]{}
阅读全文
摘要:1 class Solution { 2 public: 3 vector<int> twoSum(vector<int>& nums, int target) { 4 vector<int> v; 5 for(int i = 0; i < nums.size(); ++i) 6 for(int j
阅读全文