自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(58)
  • 收藏
  • 关注

原创 【leetcode】Validate Binary Search Tree 题解三种思路

https://leetcode.com/submissions/detail/39976412///2015-10-22 20:58:42 //1.中序 保存在一个vector中,检查是否有序//2.传递前驱指针,看是否有序//3.从底向上 ,开始只传递回最大值,不对,比如右侧应该传回最小值,因此改为两个返回值//2015-10-22 21:24:21/** *

2015-10-22 21:35:32 423

原创 树的非递归遍历

树的非递归遍历中根遍历 思路: 一直遍历左子树 p = p->left; 直到p为空,此时访问栈顶元素,栈顶元素出栈,开始遍历右子树p = p->right; 遍历右子树的左子树出栈时访问/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;*

2015-08-17 17:08:04 864

原创 【leetcode】gray-code

//2014年8月26日19:16:49//2014年8月26日19:27:08//归纳//倒叙加上一个基数即可#include #include using namespace std;class Solution {public: vector grayCode(int n) { vector ret; ret.push_back(0)

2014-08-26 19:26:21 510

原创 【leetcode】remove-duplicates-from-sorted-array

//2014年8月26日18:51:17//2014年8月26日18:57:24//遍历一次即可,关键是用两个指示位置的变量#include using namespace std;class Solution {public: int removeDuplicates(int A[], int n) { if(n == 0){ ret

2014-08-26 18:57:05 430

原创 【leetcode】best-time-to-buy-and-sell-stock

//2014年8月26日14:04:48//2014年8月26日14:12:22//从前向后遍历一次,记录截止到目前的最低价,计算可能受益,更新最大受益,更新最低价//出错点是:受益为负值时返回0#include #include using namespace std;class Solution {public: int maxProfit(vector &pric

2014-08-26 14:16:56 602

原创 【leetcode】swap-nodes-in-pairs

//2014年8月25日22:57:33//2014年8月25日23:11:30//水题 链表操作#include using namespace std;struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {}};class Solution {

2014-08-25 23:12:55 469

原创 【leetcode】merge-sorted-array

水题,关键是从后qian//2014年8月25日21:35:09//从后往前遍历//2014年8月25日21:45:04#include using namespace std;class Solution {public: void merge(int A[], int m, int B[], int n) { int p = m-1,q = n

2014-08-25 21:48:54 370

原创 【leetcode】merge-two-sorted-lists

链表操作//水题 练手 链表//2014年8月25日19:17:20//#include using namespace std;struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {}};class Solution {public

2014-08-25 21:27:55 361

原创 【leetcode】unique-binary-search-trees

//2014年8月24日10:09:19//2014年8月24日10:23:27//公式,迭代,效率不高//f(n) = 2f(n-1)+2f(1)f(n-2)+2f(2)f(n-3)+...+2f((n-1)/2-1)f((n-1)/2+1)+f((n-1)/2)f((n-1)/2) n为奇数//f(n) = 2f(n-1)+2f(1)f(n-2)+2f(2)f(n-3)+...+2f(

2014-08-24 10:24:34 421

原创 【leetcode】maximum-depth-of-binary-tree

求树的深度,比较简单;可以shenduiyouxina

2014-08-23 13:18:53 443

原创 【leetcode】path-sum-ii

huale//2014年8月23日11:16:49//2014年8月23日11:45:25//修改上个代码即可,在处理返回值时浪费很长时间#include #include using namespace std; struct TreeNode { int val; TreeNode *left; TreeNode *right;

2014-08-23 11:54:47 382

原创 【leetcode】path-sum

//先根遍历,每次修改sum值//2014年8月23日10:52:51//2014年8月23日11:07:50#include using namespace std; struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), le

2014-08-23 11:11:11 446

原创 【leetcode】sqrtx

暴力破解//暴力破解,i从0开始取值,直到i*i>x,输出i-1#include using namespace std;class Solution {public: int sqrt(int x) { if(x==0 || x==1){ return x; } for(int i=0 ; i<

2014-08-23 09:12:15 379

原创 【leetcode】powx-n

//二分//注意特殊的情况,x=0,1,-1;n=0//!!特殊的n=-2147483648,-n越界#include using namespace std;class Solution {public: double pow(double x, int n) { if(x==1 || x==0){ return x;

2014-08-22 21:51:40 409

原创 【leetcode】two-sum

该题目在编程之美上看过该题目,解题思路很清晰,先排序再从头和尾遍历一次,但写起来还是有很多问题:1.      输出的下标为未排序的vector内元素的下标,因此排序过程中需要记录下标位置,我为了省事直接使用c++中sort函数,然后通过查找元素在原vector中位置得到下标;因此也引出了另一个问题,如果两个元素相同,得到相同的位置;2.      要求两个下标位置从小到大;

2014-08-22 20:07:07 402

原创 【leetcode】binary-tree-maximum-path-sum

后序遍历;注意两个值,以gaijiedi

2014-08-21 19:09:46 500

原创 【leetcode】Evaluate Reverse Polish Notation

用栈实现比较简单,遇到数字压栈,遇到

2014-08-20 21:08:04 340

原创 【leetcode】 Reverse Words in a String

用vector存储每个单词#include #include #include using namespace std;class Solution {public: void reverseWords(string &s) { string retStr=""; string nullstr = " "; vector s

2014-08-20 20:12:23 359

原创 算法竞赛入门 7.1.4 双基回文数

暴力破解,没有特别要注意的#include #include using namespace std;int Test(int n);int main(){ int n; cin >> n; while(1){ n++;// cout << n << endl; if(Test(n) == 1){

2014-07-11 22:34:37 502

原创 算法竞赛入门 7.1.3 分数拆分

求解的关键是对等式变形,

2014-07-11 21:59:15 594 2

原创 算法竞赛入门 暴力求解法 7.1.2 最大乘积

暴力破解:第一次循环1,2,3,4,5..n;第二次xu

2014-07-11 20:40:24 664

原创 遇到的问题

1.scanf函数在读入数据时,优先与

2014-06-28 19:38:08 378

原创 算法入门 6.1.2 铁轨

重点:stack和queue1.先检查两个队列头是否相等,若xiangd

2014-06-28 16:48:56 715

原创 算法入门竞赛 5.4.2 因子和阶乘

重点:1.数组P[101]:

2014-06-27 17:12:26 566

原创 算法竞赛入门 5.4.1 Cantor的数表

重点:1.按对角线第i行有i个元素,且当i为奇数时,

2014-06-27 16:00:22 521

原创 算法竞赛入门 5.3.2 字母重排

重点:1.用vector保存字典单词;2.

2014-06-27 15:44:26 570

原创 算法竞赛入门 5.3.1 6174问题

知识点:1.排序(冒泡法);2.字符串逆转(每次首尾交换,或者)

2014-06-27 14:46:11 430

原创 算法竞赛入门 第三章习题

习题3-1 分数统计#include #include #include using namespace std;int main(){ ifstream fin("stat.in"); ofstream fout("stat.out"); map scoreMap; double score; while(fin >> score){

2014-06-25 15:32:17 487

原创 算法入门 3-4回文

代码如下://2014年6月24日23:16:08#include #include #include using namespace std;int p[5000+10];int main(){ freopen("huiwen.in","r",stdin);// freopen("huiwen.out","w",stdout); strin

2014-06-25 12:19:25 422

原创 算法竞赛入门 3-3 竖式问题

#include #include #include #include using namespace std;bool Solute(int a,int b,int c1,int c2,int c,string s);int GetLength(int n);int main(){ freopen("out.txt","w",stdout); int c1,c

2014-06-24 22:22:44 478

原创 算法竞赛入门 例题3-2 蛇形填数P35

代码如下:每次循环打印一个外圈正方形,主要是用length控制#include #include #define N 10using namespace std;int num[N][N];int main(){ int n;// scanf("%d",&n); n = 3; int counter = 1; int length

2014-06-24 19:19:20 503

原创 2013编程之美全国挑战赛 长方形

原题连接:http://programming2013.cstnet.cn/qualification/problem/2题目描述:时间限制: 1000ms 内存限制: 256MB描述在 N 条水平线与 M 条竖直线构成的网格中,放 K 枚石子,每个石子都只能放在网格的交叉点上。问在最优的摆放方式下,最多能找到多少四边平行于坐标轴的长方形,它的四个角上都恰好

2014-03-27 20:19:17 535

原创 编程之美读书笔记之2.16求数组中最长递增子序列

1.自己的解题思路是用动态规划的思想,从后往前遍历定义f(x)表示以a[x]为最小元素的最长递增子序列的长度;则f(n-1)=1;f(i) = 1,如果对于任意的j>i有 a[i]>=a[j];f(i)=1+f(j),其中j满足a[i]i所有j中最小的一个。实现代码如下:int Solute1(vector &Vec){ int maxLength = 1;

2014-03-26 20:00:31 469

原创 关于vector.size()返回值

vector.size()返回值类型为size_type,Member type size_type is an unsigned integral type,即无符号整数;这也是以下语句总是出错的原因 for(vector::size_type i=Vec.size()-1 ; i>=0 ; i--){ cout << Vec[i] << " " << endl

2014-03-26 17:33:00 15362

原创 编程之美读书笔记之1.11~1.13 一排石头的游戏

1.11 要求是N块石头排成一排,位置固定,A和B每次取任意一块或者相邻两块,最后取光者获胜;使用对称策略,先取者B先取中间的一个(奇数个)或者两个(偶数个),然后取跟A对称的位置相同的个数的石头即可。先取者获胜;扩展问题1:条件不变,最后取光者输。根据石头的个数分析:1输,2赢,3赢,4输,5赢,6赢,接下来分析就比较麻烦了,未解决。。。扩展问题2:一堆石头,每人每次最多

2014-03-26 09:43:51 716

原创 算法导论+编程之美,查找数组中最大值和最小值

查找数组中最大值和最小值,方法一:分治法,一分为二,分别查找子数组中最大值和最小值,合并时取两个子数组的较大者和较小者。f(n) = 2f(n-1)+2;f(n)=1.5n-2;方法二:将数组中元素每两个一组,先比较每组中两个元素,让较大者与最大值比较,较小者与最小值比较,每组比较3次,共需比较次数为1+1.5(n-2)=1.5n-2;从空间角度看,非递归的方法二更简单;

2014-03-21 10:11:17 619

原创 编程之美2.10寻找数组中的最大值和最小值代码

P161 分治法求解,复杂度为f(N) = 1.5N-1;代码如下:比较简单#include using namespace std;void FindMaxAndMin(int num[],int l,int r,int &minNum,int &maxNum);int main(){ int n = 6; int num[] = {6,5,8,3,

2014-03-19 18:40:05 592

原创 算法导论_基数排序学习笔记

算法导论之基数排序 例子的简单实现:对7个三位数的排序,中间用到计数排序,每次排序获得位置的index,然后再重排数组a。其中数组copya用于获得数组a中的每一位。#include using namespace std;void CountSort(int a[],int index[],int n,int k);void RadixSort(int a[],int n,int

2014-03-19 17:55:04 486

原创 算法导论_计数排序学习笔记

计数排序学习笔记//注意下标,数组C[]的下标范围为0~k,包括k,因为要排序的数字的范围是0~k//数组b的下标范围为1~n,包括n//b的下标不能为0,如下标为0,则表示c[i]=0,而实际上c[i]=0时,对应的a中所有小于等于i的元素已经访问完//c[i]最后表示a[n]中小于等于i的元素的总个数,b[c[a[i]]]=a[i]表示a[i]在数组b中的位置//例如,a[5]=

2014-03-18 14:32:42 560

转载 win7中VC6.0 visual C++6.0无法打开文件和向工程中添加文件的解决办法【已添加部分注释】

具体做法如下:1. 从本文下方的下载地址下载FileTool.dll,并将FileTool.dll解压至VC安装目录中。2. 在VC6.0中点击Tools(工具)——Customize(定制) 3. 在出现的“Customize”对话框中,点击Add-I and Macro Files(附加项和宏文件)标签 4. 点击Browse(浏览),定位刚才解压出来的FileTool.dl

2014-03-01 20:44:13 1363 1

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除