摘要: 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NU 阅读全文
posted @ 2013-07-27 17:31 NinaGood 阅读(111) 评论(0) 推荐(0) 编辑
摘要: 题目:统计一个数字在排序数组中出现的次数。例如输入排序{1,2,3,3,3,3,4,5}和数字3,由于3在这个数组中出现了4次,因此输出4. 时间复杂度要求O(logn) 1 #include<iostream> 2 #include<assert.h> 3 using namespace std; 阅读全文
posted @ 2013-07-27 16:17 NinaGood 阅读(126) 评论(0) 推荐(0) 编辑
摘要: 题目:输入两个链表,找出它们的第一个公共结点。 O(m+n) 思路:首先遍历两个链表得到它们的长度,就能知道哪个链表比较长,以及长的链表比短的链表多几个结点。在第二次遍历的时候,在较长的链表上先走若干步,接着再同时在两个链表上遍历,找到的第一个相同的结点就是它们的第一个公共结点。 1 struct 阅读全文
posted @ 2013-07-26 17:47 NinaGood 阅读(156) 评论(0) 推荐(0) 编辑
摘要: 题目:输入一个整型数组,数组里有正数也有负数。数组中一个或连续的多个整数组成一个子数组。求所有子数组的和的最大值。要求时间复杂度O(n) 1 #include 2 #include 3 using namespace std; 4 5 int MaxAdd( int *data,int len) 6 { 7 assert( data != NULL && len > 0 ); 8 int max,current; 9 max=data[0];10 current=0;11 for( int i = 0; i max )23 m... 阅读全文
posted @ 2013-07-25 16:26 NinaGood 阅读(122) 评论(0) 推荐(0) 编辑
摘要: 题目:输入一个字符串,输出该字符串中字符的所有组合。举个例子,如果输入abc,它的组合有a、b、c、ab、ac、bc、abc。非递归实现: 1 #include 2 using namespace std; 3 4 void Print_str(char *str,int length , int s) 5 { 6 7 for(int i = 0 ; i 2 #include 3 #include 4 #include 5 using namespace std; 6 7 8 void Combination(char *string ,int number,vect... 阅读全文
posted @ 2013-07-24 18:58 NinaGood 阅读(206) 评论(0) 推荐(0) 编辑
摘要: 题目:输入一个字符串,打印出该字符串中字符的所有排列。(有重复字符和没有重复字符)1.递归实现:(1)递归算法设计的基本思想是将一个难以解决的大问题,分解为一些规模较小的相同问题,以便各个击破,分而治之。其实,上述算法按照以下方式理解更为合适:我们有一字符串为abc,对应的全排列有6种:abc、acb、bca、bac、cab和cba。它可以由下述规则得到:Abc的全排列可以由下述规律得到:a + 全排列(bc)b + 全排列(ac)c + 全排列(ab)递归实现有重复字符的字符串全排列:由于全排列就是从第一个数字起每个数分别与它后面的数字交换。我们先尝试加个这样的判断——如果一个数与后面的数字 阅读全文
posted @ 2013-07-23 20:05 NinaGood 阅读(171) 评论(0) 推荐(0) 编辑
摘要: 题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则返回true,否则返回false。假设输入的数组的任意两个数字都互不相同。 1 // Type your C++ code and click the "Run Code" button! 2 // Your code output will be shown on the left. 3 // Click on the "Show input" button to enter input data to be read (from stdin). 4 5 #include 6 阅读全文
posted @ 2013-07-23 09:49 NinaGood 阅读(121) 评论(0) 推荐(0) 编辑
摘要: 题目:输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。需要辅助栈,规律:如果下一个弹出的数字刚好是栈顶数字,那么直接弹出。如果下一个弹出的数字不在栈顶,我们把压栈序列中还没有入栈数字压入辅助栈,直到把下一个需要弹出的数字压入栈顶为止。如果所有的数字都压入栈仍然没有找到下一个弹出的数字,那么该序列不可能是一个弹出序列。// Type your C++ code and click the "Run Code" button!// Your code output will be shown on the left.// Click on t 阅读全文
posted @ 2013-07-22 21:25 NinaGood 阅读(234) 评论(0) 推荐(1) 编辑
摘要: 1 // Type your C++ code and click the "Run Code" button! 2 // Your code output will be shown on the left. 3 // Click on the "Show input" button to enter input data to be read (from stdin). 4 5 #include 6 using namespace std; 7 8 /*字符串里的数字增1*/ 9 bool Increment( char * number )10 { 阅读全文
posted @ 2013-07-20 16:03 NinaGood 阅读(205) 评论(0) 推荐(0) 编辑
摘要: 1 class Solution { 2 public: 3 bool equal( double a,double b ) 4 { 5 if( a-b -0.0000001 ) 6 { 7 return true; 8 } 9 else10 {11 return false;12 }13 }14 15 double pow_unsigned(double x,unsigned int n )16 {... 阅读全文
posted @ 2013-07-19 21:38 NinaGood 阅读(116) 评论(0) 推荐(0) 编辑