
每日一题
BEconfidence
坚持才是胜利之道
展开
-
枚举poj2965poj1753
刷了这两道枚举的题。用的都是bfs。1753这道题用到二进制的压缩方法。不管我感觉不压缩也不会超时什么的。其次的方法就是中规中矩的bfs。2965中。除了bfs,换要把路径记录下来。我开了两个数组,一个数组用来记录当前状态的前序状态。另一个数组记录到达这个状态的需要改变的change[i]。详细看代码。可能有更简答的做法,我换没搜一搜。1753:#inclu原创 2017-12-07 09:25:05 · 325 阅读 · 0 评论 -
leetcode448. Find All Numbers Disappeared in an Array
vector新建容量为n的数组。先循环一遍记录出现过的,然后在看哪个没出现输出就好。网上很多其他方法。class Solution {public: vector findDisappearedNumbers(vector& nums) { int n=nums.size(); vector used(n+1,0); fo原创 2017-08-28 23:36:15 · 395 阅读 · 0 评论 -
leetcode349. Intersection of Two Arrays
vector的一系列操作。1 -- set_intersection(交集)2 -- set_union(并集)3 -- set_difference(差集)4 -- set_symeetric_difference(对称差集)去重的unique。class Solution {public: vector unique_element原创 2017-09-06 13:50:46 · 227 阅读 · 0 评论 -
leetcode598. Range Addition II降维
终于有点有意思的题目了。题意是给一个都是0的mn数组。给一组操作。m[i][j]就死把数组的前i行前j列全部加1.求最后最大的数的个数。开始想用简单的模拟去做,数组明显超内存。所以要用降维的思路。 开一个行的数组,一个列的数组。最后最大值肯定是m[0][0]。最后乘积代表个数。class Solution {public: int maxCount(int m原创 2017-09-06 13:39:16 · 261 阅读 · 0 评论 -
leetcode Add to List 637. Average of Levels in Binary Tree
就是对二叉树每一层求平均值。开始想法用层次遍历,然而又不知道该点是在哪一层。所以要求没一点的深度,而通过先序遍历递归求没一点的深度的过程中。好像并不需要再去层次遍历,结果就已经很明显了。要注意leetcode的树要用->。两个数组用来存结果。没有数据量范围我就写的100000.还要注意有一个奇葩数据2147483647 两个节点也是2147483647 ,所以要改原创 2017-08-24 13:20:49 · 353 阅读 · 0 评论 -
Leetcode496. Next Greater Element I
这个题,开始我是想用find和upper_bound两个函数解决,但是upper_bound并不对。upper_bound返回的是插入的数字的上界。如一个数组number序列1,2,2,4.upper_bound(2)后,返回的位置是3(下标)也就是4所在的位置。并且用的是二分查找,所以序列要求有序。那就自己写了个循环。还有find的返回值,找不到返回end。最原创 2017-08-24 09:51:21 · 247 阅读 · 0 评论 -
POJ-2559利用栈往前迭代
Largest Rectangle in a Histogram POJ - 2559 #include#include#include #include #include #include using namespace std;const int N=int(1e5)+9;int h[N];int n;int main(){ while(cin原创 2017-09-05 20:04:59 · 291 阅读 · 0 评论 -
leetcode171. Excel Sheet Column Number
26进制的问题class Solution {public: int sum=0; void f(string s ,int i,int t){ if(i<0) return ; int x=s[i]-'A'+1; for(int j=1;j<t;j++){ x*=26; }原创 2017-09-13 18:40:04 · 385 阅读 · 0 评论 -
leetcode538. Convert BST to Greater Tree
考察树的中序遍历。BST是左子树小于右子树。中序便利会输出他的从达到小的顺序。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(原创 2017-09-05 15:44:08 · 221 阅读 · 0 评论 -
leetcode492. Construct the Rectangle
求给定一个面积的长和宽。要求长和宽的差最小。开根号面积,得到的是最大的长,然后--找到满足条件的就好。class Solution {public: vector constructRectangle(int area) { int x=sqrt(area); vector ans; for(int i=x;i>=1;i-原创 2017-09-05 15:29:14 · 217 阅读 · 0 评论 -
leetcode463. Island Perimeter
计算周长,遇到一个1加4.在去判断这个1的周围,如果有1,结果减一。class Solution {public: int islandPerimeter(vector>& grid) { int r=grid.size(); int c=grid[0].size(); int ans=0; for(int i=0;原创 2017-08-23 16:09:15 · 212 阅读 · 0 评论 -
leetcode620. Not Boring Movies
# Write your MySQL query statement belowselect * from cinemawhere mod(id,2)=1and description!='boring'order by rating DESC或者SELECT * FROM cinema WHERE (id % 2 = 1) AND (description原创 2017-08-23 16:01:36 · 263 阅读 · 0 评论 -
leetcode412. Fizz Buzz
字符串的简单处理,学到一个转换字符串的函数。std::to_string C++ Strings librarystd::basic_stringDefined in header string>(1)std::string to_string( int value );(2)std::string to_string( long value原创 2017-08-23 15:56:29 · 192 阅读 · 0 评论 -
leetcode344. Reverse String
处理好边界。class Solution {public: string reverseString(string s) { for(int i=0;i<s.size()/2;i++){ char x=s[i]; s[i]=s[s.size()-i-1]; s[s.size()-i-1]=x;原创 2017-08-23 15:41:59 · 186 阅读 · 0 评论 -
leetcode371. Sum of Two Integers
这题挺有意思不用+-算+法先&:求出需要进位的位后^:求出除了进位的结果然后:把进位的那位左移,用上面的结果再去+进位后的结果。class Solution { public: int getSum(int a, int b) { while(b) { int carry = a & b;原创 2017-08-28 23:48:03 · 394 阅读 · 0 评论 -
258. Add Digits
一看不让用循环就有点蒙。只能找规律。和9相关。class Solution {public: int addDigits(int num) { if(num==0) return 0; if(num%9==0) return 9; return num%9; } }};原创 2017-08-29 10:49:26 · 261 阅读 · 0 评论 -
贪心poj2586
主要题意很难懂。转一篇博客,讲的很好。题意比较难懂,其实只要读懂题意,就很简单了。大意是一个公司在12个月中,或固定盈余s,或固定亏损d.但记不得哪些月盈余,哪些月亏损,只能记得连续5个月的代数和总是亏损(问全年是否可能盈利?若可能,输出可能最大盈利金额,否则输出“Deficit". 根据经验,贪心选择往往都在极端处(临界点)选择。(其实这题转载 2017-12-12 08:36:46 · 308 阅读 · 0 评论 -
贪心poj2109
这个题可以用高精度算法写,套模板吧。为什么把它归类到贪心。下面的解释来自知乎:这题其实最大漏洞就在于k的范围被限制在了1double的有效数字是15~16位,也就是说将p塞进double误差率在10^-15数量级左右。而开n次根号误差率是只会减小不会增大的,塞回double误差率仍然是10^-15左右的数量级。而答案只可能有9位有效数字,显然这个误差率只会影响到小数部分原创 2017-12-11 19:56:56 · 253 阅读 · 0 评论 -
贪心poj1328
把每个岛转换成区间。然后对区间排序。选择区间最右点为站点。最初一直wrong,是因为我排序的方式不对。应该按最后点排序。而不是区间的第一个点。#include#include#include#include#include#includeusing namespace std;pair p[10000+10];bool cmp(const pair &p1,const原创 2017-12-10 10:23:15 · 226 阅读 · 0 评论 -
leetcode645. Set Mismatch
类似于哈希 查找就好了。class Solution {public: vector findErrorNums(vector& nums) { int n=nums.size(); vector ans; vector s(n+2,0); for(int i=0;i<n;i++){原创 2017-09-01 20:30:36 · 282 阅读 · 0 评论 -
leetcode287. Find the Duplicate Number
找出这些串中一个重复。遍历一遍,然后用vector的find函数去查找就好了。查找之前要把当前值记录一下,并删去,找到不久是这个重复了吗。class Solution {public: int findDuplicate(vector& nums) { for(int i=0;i<nums.size();i++){ int原创 2017-09-01 20:05:13 · 199 阅读 · 0 评论 -
leetcode268. Missing Number
找出传中从0到n缺少的那个数。最近论文看的真的很烦,什么都不会,希望赶紧上课把。是从0到n,所以排个序,找出值与下标不对应的就好,也可能是最后一个数缺少。循环到n+1.class Solution {public: int missingNumber(vector& nums) { sort(nums.begin(),nums.end());原创 2017-09-01 19:38:19 · 196 阅读 · 0 评论 -
leetcode389. Find the Difference
求出这两个串中一个不同的值。开始想用multiset,后来想到string里也有find。返回的索引值。所以用stringfind很好解决。class Solution {public: char findTheDifference(string s, string t) { int n=s.size(); int n2=n+1;原创 2017-09-01 19:29:21 · 233 阅读 · 0 评论 -
leetcode653. Two Sum IV - Input is a BST
题意是求二叉树中的任意两个数的和能不能是k。把二叉树便利一遍,求出所有元素,然后在循环便利每两个的和就好。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(i原创 2017-08-30 23:42:16 · 229 阅读 · 0 评论 -
leetcode58. Length of Last Word
不知为什么我的博客也会多了些关注,非常感谢能给你们提供到帮助。让我坚持做的一点事情也有了点意义。就是判断最后一个字符串的长度。有一个坑就是最后一个可能为空格,所以要去掉最后的空格。class Solution {public: int lengthOfLastWord(string s) { int n=s.size();原创 2017-08-30 23:30:36 · 229 阅读 · 0 评论 -
leetcode530. Minimum Absolute Difference in BST
平衡二叉树的性质。中序遍历二叉树为其拍好序列。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), ri原创 2017-09-08 10:14:21 · 248 阅读 · 0 评论 -
leetcode462. Minimum Moves to Equal Array Elements II
好题啊,开始想直接暴力,超时。怎么也想不出最优的解到底是哪个数字。应该去想最优解一定是nums数组里面的。我们首先给数组排序,那么我们最终需要变成的相等的数字就是中间的数,如果数组有奇数个,那么就是最中间的那个数字;如果是偶数个,那么就是中间两个数的区间中的任意一个数字。而两端的数字变成中间的一个数字需要的步数实际上就是两端数字的距离class Solu原创 2017-09-07 11:27:09 · 332 阅读 · 0 评论 -
leetcode453. Minimum Moves to Equal Array Elements
题意是把vector的n-1个数加1,直到所有数相等。其实相等于把大的数减一,直到都等于最小数。相等于逆否命题。class Solution {public: int minMoves(vector& nums) { int n=nums.size(); int minx=nums[0]; for(int i=0;i<n;i++原创 2017-09-07 10:47:08 · 217 阅读 · 0 评论 -
leetcode202. Happy Number
以为这题是找规律,试着写了个循环,也过了。用集合去判断出现过的数字,判断循环。然后就根据题目中的算法模拟就好。class Solution {public: bool isHappy(int n) { set s; s.insert(n); int k=0; while(1){ int n2=0; while(n){原创 2017-08-29 11:17:52 · 342 阅读 · 0 评论 -
Leetcode566. Reshape the Matrix
这里学习一下vector >的输入方法:需要在新建一个变量vector v然后v.push_back();之后把ans.push_back(v);不要忘记每次清空v:class Solution {public: vector> matrixReshape(vector>& nums, int r, int c) { int r1=nums.size(原创 2017-08-23 15:26:01 · 232 阅读 · 0 评论 -
Leetcode657. Judge Route Circle
一直刷水题,从未进步过。class Solution {public: bool judgeCircle(string moves) { int x=0,y=0; for(int i=0;i<moves.size();i++) { if(moves[i]=='L'){ x-=1;原创 2017-08-23 14:59:56 · 461 阅读 · 0 评论 -
cfCodeforces Round #432 (Div. 2,
第一题:找规律就好了,前k个和后n到n+k是不一样的,中间都是k。#include #include #include #include using namespace std;int main(){ int n,k,t; scanf("%d%d%d",&n,&k,&t); if(t<=k) cout<<t; else if(t>k&&t<n) co原创 2017-09-05 09:23:14 · 274 阅读 · 0 评论 -
Leetcode500. Keyboard Row
class Solution {public: vector findWords(vector& words) { vector ret; int sz=words.size(); for(int i=0;i<sz;i++){ if(isOnlyOne(words[i]))原创 2017-08-11 16:15:08 · 196 阅读 · 0 评论 -
Leetcode476. Number Complement
class Solution {public: int findComplement(int num) { int ans=0; for(int i=0;num!=0;i++){ int x=num%2; if(x==0){ ans+=(int)(pow(2.0,i)+0.5)原创 2017-08-11 10:08:40 · 205 阅读 · 0 评论 -
leetcode627. Swap Salary
# Write your MySQL query statement belowUpdate salarySet sex=Case sex WHEN 'f' THEN 'm' ELSE 'f' END;原创 2017-08-11 09:52:34 · 231 阅读 · 0 评论 -
Leetcode561. Array Partition I
#include class Solution {public: int arrayPairSum(vector& nums) { sort(nums.begin(),nums.end()); int ans=0; for(int i=0;i<nums.size();i+=2){ ans+=nums[i];原创 2017-08-11 09:36:17 · 194 阅读 · 0 评论 -
hdu6012Lotus and Horticulture离散化线性取最大值
题意简单的说,每个植物的在不同时间段有三个价值,分为a,b,c。找到最佳时间的最大价值,只需要求出价值的值。可以把这三个值,类似于哈希用map把对应的时间边界和价值数关联。那么循环便利一遍,map是有自动排序的,从小的时间点循环到最大的时间点。其中得到的最值便为最大价值。离散化,因为l到r的时间段内,时间点可取任意实数,也就是可以取0.5,那么必须要把r+1和下次会原创 2017-08-10 13:23:51 · 339 阅读 · 0 评论 -
hdu6011增量法
这个题问题在于怎么解决价值*i,这个i是不确定的。如果从前往后,只能遍历来解决,还不知道行不行。从后往前,每次记录要增加的增量,然后对每个数的循环后,增加增量的值,并且结果增加增量,这样最终的结果和从第1个数*i的结果是一样的。当增量小于0,即在加上增量会使最大值减小的时候,跳出即可。#include using namespace std;int main(){原创 2017-08-10 09:46:58 · 393 阅读 · 0 评论 -
hdu6016二分图思路
男的一列,女的一列。因为题意是找连续的四个人,从题目给出的每一对关系去延展。男的在找一个女的,女的再找一个男的,正好四个人。男的关系去掉这一对中的这个女的,女的关系去掉这一对中的这个男的,乘积遍为个数。因为可以两头开始数,结果乘2.#include using namespace std;pair A[100009];int main(){ int T;原创 2017-08-09 16:40:05 · 240 阅读 · 0 评论 -
判断回文数
这么经典而且简单的算法。class Solution {public: bool isPalindrome(int x) { if(x<0) return 0; int y=x; int sum = 0; while(x){ sum = sum*10 + x%10; x/=原创 2017-07-19 23:48:12 · 283 阅读 · 0 评论