2020年3月13日

计算一个整数二进制表示中1的个数(剑指)

摘要: 题意大概是就是标题所说,不过特别需要注意这里有一个坑,就是输入的整数也可能是负数,而C++负数表示为高位全部为1,如-2表示为111...10(32位),所以如果输入-2,输出1的个数为31。 因此我们肯定不能简单地右移n,这会导致左边高位不断补1,最后陷入死循环(负数不断右移不会为0) 第一种办法 阅读全文

posted @ 2020-03-13 17:43 QingFengDaHui 阅读(242) 评论(0) 推荐(0) 编辑

2020年3月11日

leetcode 87

摘要: 题意: 题解: 这道题可以通过dp求解,我们定义dp[i][j][k]为s1中下标i开始,s2中下标j开始,且长度为k的两个子字符串是否为扰乱字符串 然后我们可以发现dp[i][j][k]的求解可以分为两种情况,一种是这两个子字符串分别被分割为两个字符串,且被分割的两个子字符串不进行替换就和另外一个 阅读全文

posted @ 2020-03-11 21:17 QingFengDaHui 阅读(101) 评论(0) 推荐(0) 编辑

leetcode 85 最大矩形

摘要: 题意: 题解: 我们知道最大矩形一定是由矩形中某个点向上延伸直到为‘0’,然后往左右延伸(在高度得到保证的情况下左右伸展)的矩形 因此我们通过遍历每一行来求取对于以每个点来说的最大矩形并获得最大值 这里最关键的一点就是当遍历到某一个点的上面的那个点为‘0’,此时该点的左右延伸长度不在与上一行进行比较 阅读全文

posted @ 2020-03-11 17:59 QingFengDaHui 阅读(202) 评论(0) 推荐(0) 编辑

2020年3月10日

leetcode 72 编辑距离

摘要: 题目: 对于这道题目,我们可以使用dp来解决,我们定义dp[i][j]表示将word1中的前 i 个字符到word2的前 j 个字符的最短距离, 然后我们可以得到dp的方程式,当word1的第i个字符等于word2的第 j 个字符时,对于word1来说存在两种操作,即插入和删除, 此时的方程式如下: 阅读全文

posted @ 2020-03-10 22:05 QingFengDaHui 阅读(231) 评论(0) 推荐(0) 编辑

2020年3月9日

最长上升子序列 leetcode 300

摘要: dp解法 int lengthOfLIS(vector<int>& nums) { //dp解法O(n^2) int len=nums.size(); if(len==0) return 0; vector<int> dp(len,1); int maxlen=1; for(int i=1;i<le 阅读全文

posted @ 2020-03-09 18:37 QingFengDaHui 阅读(210) 评论(0) 推荐(0) 编辑

最长回文子序列 leetcode 516

摘要: class Solution { public: int longestPalindromeSubseq(string s) { int len=s.size(); if(len==0) return 0; vector<vector<int>> dp(len,vector<int>(len,1)) 阅读全文

posted @ 2020-03-09 18:33 QingFengDaHui 阅读(125) 评论(0) 推荐(0) 编辑

leetcode 5 最长回文子串(马拉车算法)

摘要: 添加‘#’的原因是让偶数回文串也可以有中心点,添加‘!’和’?’的原因是防止len数组越界 其中len[i]表示以i为中心时,从i到以i为中心的回文串的最右边的长度 mx表示循环到此时之前所有的回文串的右边界的最大值,p表示该回文串的中心 class Solution { public: strin 阅读全文

posted @ 2020-03-09 18:30 QingFengDaHui 阅读(184) 评论(0) 推荐(0) 编辑

leetcode 28(简单kmp)

摘要: void GetNext(string &needle,int next[]) { next[0]=-1; int k=-1; int len=needle.size(); for(int i=1;i<len;i++) { while(k!=-1&&needle[k+1]!=needle[i]) k 阅读全文

posted @ 2020-03-09 18:24 QingFengDaHui 阅读(217) 评论(0) 推荐(0) 编辑

区间动态规划 scu2117 加分二叉树

摘要: 题目链接:https://vjudge.net/problem/SCU-2117 #include <iostream> #include<vector> #include<algorithm> #include<string> #include<map> using namespace std; 阅读全文

posted @ 2020-03-09 16:15 QingFengDaHui 阅读(152) 评论(0) 推荐(0) 编辑

2020年3月1日

leetcode 347 (堆排序)

摘要: void topdown(map<int,int> &m,vector<int> &heap,int size,int r) { int tmp=heap[r]; int i; for(i=r;i*2+1<size;) { int j=i*2+1; if(j<size-1&&m[heap[j]]<m 阅读全文

posted @ 2020-03-01 02:53 QingFengDaHui 阅读(355) 评论(0) 推荐(0) 编辑

导航