随笔分类 -  07_力扣

摘要:有两个经典问题:一个是最长公共子序列(不连续)、最长公共子串(连续); 思路都是动态规划,直接上代码: 1 class Solution { 2 public: 3 4 int longestCommonSubsequence(string text1, string text2) { 5 int 阅读全文
posted @ 2020-02-27 11:43 Grooovvve 阅读(733) 评论(0) 推荐(0)
摘要:使用动态规划的方法。 确定动态方程; 1 class Solution { 2 public: 3 string longestPalindrome(string s) { 4 int length = s.length(); 5 if(length <2){ 6 return s; 7 } 8 9 阅读全文
posted @ 2020-02-26 23:05 Grooovvve 阅读(128) 评论(0) 推荐(0)
摘要:题目:如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值。如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值。我们使用Insert()方法读取数据流,使用GetMedian()方法获取当前读取数据的中位数。 思路: 可 阅读全文
posted @ 2020-02-26 16:44 Grooovvve 阅读(206) 评论(0) 推荐(0)
摘要:利用map先统计一下元素的频率; 利用优先队列,求前K大频率,注意使用最小堆(维护一个元素个数k个的最小堆); 重新设置比较器为greater,即最小堆。因为优先队列默认是最大堆less; 另外对于队列元素是pair,需要了解比较规则为先比较first再比较second; 1 // 347. Top 阅读全文
posted @ 2020-02-26 15:32 Grooovvve 阅读(104) 评论(0) 推荐(0)
摘要:题目:将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数。 数值为0或者字符串不是一个合法的数值则返回0 输入一个字符串,包括数字字母符号,可以为空 如果是合法的数值表达则返回该数字,否则返回0 溢出判断是难点; 符号的判断,非数字符号的判断; 空字符串的处理; //+-只能出现在开头, 阅读全文
posted @ 2020-02-25 23:50 Grooovvve 阅读(320) 评论(0) 推荐(0)
摘要:这个题解超时了,mark一下后面再改 1 #include <iostream> 2 #include <algorithm> 3 4 5 using namespace std; 6 7 /*** 8 * 9 * 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b 阅读全文
posted @ 2020-02-25 11:29 Grooovvve 阅读(142) 评论(0) 推荐(0)
摘要:思路: 1、遍历数组-构建map:用一个map存储遍历数组获取<数值,索引>这样的键值对。方便查找想要的某个数值的位置; 2、遍历数组-查询map:再遍历一遍数组,通过查询map确定是否存在两个数之和为目标值;同时注意不能重复利用这个数组中同样的元素; 1 class Solution { 2 pu 阅读全文
posted @ 2020-02-25 10:23 Grooovvve 阅读(145) 评论(0) 推荐(0)
摘要:1 #include <iostream> 2 #include <stack> 3 #include <queue> 4 5 using namespace std; 6 7 //用两个栈实现一个队列; 8 /*** 9 * 思路:队列是先进先出; 10 * push操作:对stack1压栈 11 阅读全文
posted @ 2020-02-25 09:42 Grooovvve 阅读(193) 评论(0) 推荐(0)
摘要:1 #include <iostream> 2 #include <string> 3 #include <cmath> 4 #include <vector> 5 using namespace std; 6 7 8 9 struct ListNode{ 10 int val; 11 ListNo 阅读全文
posted @ 2020-02-25 08:53 Grooovvve 阅读(335) 评论(0) 推荐(0)
摘要:题目: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。 思路: 滑动窗口方法实现;用数组代替集合来记录子串是否存在不重复字符; 代码实现C++: 1 class 阅读全文
posted @ 2019-11-08 09:27 Grooovvve 阅读(236) 评论(0) 推荐(0)
摘要:题目: 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。 有效字符串需满足: 左括号必须用相同类型的右括号闭合。左括号必须以正确的顺序闭合。注意空字符串可被认为是有效字符串。 来源:力扣(LeetCode)链接:https://leetcode-cn.c 阅读全文
posted @ 2019-07-25 23:20 Grooovvve 阅读(177) 评论(0) 推荐(0)
摘要:题目: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are+,-,*,/. Each operand may be an integer or another e 阅读全文
posted @ 2019-05-07 15:43 Grooovvve 阅读(195) 评论(0) 推荐(0)
摘要:题目: Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the neares 阅读全文
posted @ 2019-05-07 15:30 Grooovvve 阅读(257) 评论(0) 推荐(0)