- 博客(35)
- 资源 (3)
- 收藏
- 关注
原创 剑指Offer(24)______二叉树中和为某一值的路径
输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。class Solution {public: vector > ans; int arr[10000]; void solve(TreeNode* root,int index,int sum,int expectNum
2017-03-24 08:44:48 730
原创 剑指Offer(23)______二叉搜索树的后序遍历序列
输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。class Solution {public: bool check(vector& s,int l,int r){ if(r - l <= 2) return true; int index =
2017-03-24 08:42:55 382
原创 剑指Offer(22)______从上往下打印二叉树
从上往下打印出二叉树的每个节点,同层节点从左至右打印。class Solution {public: vector PrintFromTopToBottom(TreeNode* root) { vector ans; vector tree; if(root == NULL) return ans; //
2017-03-24 08:40:07 443
原创 剑指Offer(21)______栈的压入、弹出序列
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)class Solution {public: bool IsPopOrder(
2017-03-24 08:37:38 425
原创 剑指Offer(20)______包含min的栈
定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。class Solution {public: stack stk; stack m; void push(int value) { if(m.size()==0){ m.push(value); }else{
2017-03-24 08:36:01 538
原创 剑指Offer(19)______顺时针打印矩阵
输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.class Solution {public: vector printMatrix(vector >
2017-03-24 08:34:48 370
原创 剑指Offer(18)______二叉树的镜像
操作给定的二叉树,将其变换为源二叉树的镜像。输入描述:二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ 11 9 7 5
2017-03-24 08:33:12 394
原创 剑指Offer(17)______树的子结构
输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)class Solution {public: bool isSubtree(TreeNode* pRoot1, TreeNode* pRoot2){ if(pRoot2 == NULL) return true; if(p
2017-03-24 08:31:13 375
原创 剑指Offer(16)______合并两个排序的链表
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。class Solution {public: ListNode* Merge(ListNode* l1, ListNode* l2) { if(l1 == NULL)return l2; //鲁棒性:链表不能为空 if(l2 == N
2017-03-24 08:29:50 335
原创 剑指Offer(15)______反转链表
输入一个链表,反转链表后,输出链表的所有元素。class Solution {public: pair reverseList(ListNode* pHead){ if(pHead->next == NULL)return {pHead,pHead}; auto tp = reverseList(pHead->next); tp
2017-03-24 08:26:57 348
原创 剑指Offer(14)______链表中倒数第k个结点
输入一个链表,输出该链表中倒数第k个结点。class Solution {public: ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) { if(pListHead == NULL) return NULL; //鲁棒性:判链表不为空 if(
2017-03-24 08:22:39 409
原创 剑指Offer(13)______调整数组顺序使奇数位于偶数前面
输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。class Solution {public: void reOrderArray(vector &array) { if(array.size() == 0) return;
2017-03-24 08:21:30 327
原创 剑指Offer(12)______数值的整数次方
给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。class Solution {public: bool equal(double a,double b){ if(fabs(a-b) < 0.00001) return true; return false; }
2017-03-24 08:10:23 346
原创 剑指Offer(11)______二进制中1的个数
输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。class Solution {public: int NumberOf1(int n) { int cnt = 0; while(n){ cnt ++; n = n&(n-1); }
2017-03-24 08:09:37 335
原创 剑指Offer(10)______矩形覆盖
我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?class Solution {public: int rectCover(int number) { if(number <= 0) return 0; //鲁棒性:判断number是否合法
2017-03-24 08:08:42 351
原创 剑指Offer(8)______跳台阶
一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法。class Solution {public: int jumpFloor(int number) { if(number <= 0) return 0; //鲁棒性:判断number是否合法 int dp[4
2017-03-24 08:06:29 301
原创 剑指Offer(9)______变态跳台阶
一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。class Solution {public: int jumpFloorII(int number) { int dp[40] = {1,1,2}; int sum[40] = {1,1,4};
2017-03-24 08:06:26 319
原创 剑指Offer(7)______斐波那契数列
大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项。n代码:class Solution {public: int Fibonacci(int n) { if(n <= 0) return 0; //鲁棒性: 判断n是否合法 int dp[40]
2017-03-24 08:04:02 431
原创 剑指Offer(6)______旋转数组的最小数字
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。class Solution {public: int minNumberInRotateArra
2017-03-23 21:29:20 250
原创 剑指Offer(5)______用两个栈实现队列
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。class Solution{public: void push(int node) { stack1.push(node); } int pop() { if(stack2.empty()){ while(stac
2017-03-23 21:27:32 333
原创 剑指Offer(4)______重建二叉树
输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。class Solution {public: bool errflag; void clearNode(TreeNode
2017-03-23 21:23:03 301
原创 剑指Offer(3)______从尾到头打印链表
输入一个链表,从尾到头打印链表每个节点的值。栈方式:class Solution {public: vector printListFromTailToHead(ListNode* head) { vector ans; if(head == NULL) return ans;//鲁棒性 : 判断链表是否为空
2017-03-23 21:19:56 227
原创 剑指Offer(2)______替换空格
请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。class Solution {public: void replaceSpace(char *str,int length) { if(str == NULL)return; //鲁棒性:
2017-03-23 21:17:28 267
原创 剑指Offer(1)______二维数组中的查找
在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。代码:class Solution {public: bool Find(int target, vector > array) { int n,m; n = array.si
2017-03-23 21:11:40 365
原创 LeetCoder_____Generate Parentheses(22)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:[ "((()))", "(()())", "(())()", "()(())
2017-03-22 21:53:02 251
原创 LeetCoder_____Merge Two Sorted Lists(21)
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.题意:合并两个有序链表,返回新链表的头指针。注意:新链表应该由原两链表分割组合而成,不能新开辟
2017-03-22 21:14:38 306
原创 LeetCoder_____Valid Parentheses(20)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are all va
2017-03-22 20:36:39 270
原创 LeetCoder_____Remove Nth Node From End of List(11)
Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the
2017-03-22 19:52:45 260
原创 LeetCoder_____2Sum,3Sum,4Sum
LeetCoder____Two Sum(1)Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solu
2017-03-22 19:02:41 310
原创 LeetCoder_____Container With Most Water(11)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Fin
2017-03-19 09:56:26 340
原创 LeetCoder_____Regular Expression Matching(10)
Implement regular expression matching with support for '.' and '*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input st
2017-03-16 10:48:10 284
原创 LeetCoder_____ZigZag Conversion(6)
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA P L S I
2017-03-11 11:26:29 277
原创 如何辨认一个复杂声明表达式子中变量的类型?
何谓复杂声明表达式子中变量的类型?先来看看这段代码:typedef double* (*a)[10];typedef int* (*b[10])[10];typedef int(*(*c)(int,int))(int);typedef int(*(**d[5])(int))(void(*)(void));以上a,b,c,d 的类型是什么呢?当然,实际工程中不太会出现这么复杂的类型,所以这个知识
2017-03-03 21:08:28 577
原创 类型转换下编译器偷偷做的事————整形提升(Inter Promotion)
一.前言 在讲述这个话题之前,还是先举几个代码例子:long long ll = 1;long l = 2;double db = 3;float f = 4;unsigned int uint = 5;decltype(db + ll) // ->类型?decltype(uint
2017-03-03 12:55:57 905 1
原创 #define 和 const 定义常量比较。
在C++中可以使用#define或者const来定义常量,但是使用const相比起#define有更多的优点。——–<Effective C++>以下部分内容选自<程序猿面试宝典>const 常量有数据类型,而宏常量是没有数据类型。编译器可以对前者进行类型安全检查,而后者仅仅进行字符替换,没有类型安全检查,并且在字符替换过程中会产生一些意料不到的错误。(边际效应)#define X 10#de
2017-03-02 20:06:39 3020
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人