摘要: 编程之美上也有这道题,但是个人觉得讲的并不是易懂,之后我在剑指offer上看到这道题看一遍就懂了;知识提取:一个整数n和n-1的与运算,获得整数n的二进制数最右边的1变0;int NumberOf1( int n ){ int num=0; while( n ) { n&=n-1; num++; } return num;} 详细内容参考剑指offer 阅读全文
posted @ 2013-07-19 14:42 NinaGood 阅读(72) 评论(0) 推荐(0) 编辑
摘要: 时间复杂度O(n) 1 long Fibonacci( long n ) 2 { 3 if( n <= 1 ) 4 return n; 5 6 long one=0; 7 long two=1; 8 9 for( long i = 2; i <= n; ++i )10 {11 int tmp = two;12 two += one;13 one = tmp;14 }15 return two;16 } 阅读全文
posted @ 2013-07-19 09:52 NinaGood 阅读(144) 评论(0) 推荐(0) 编辑
摘要: // Type your C++ code and click the "Run Code" button!// Your code output will be shown on the left.// Click on the "Show input" button to enter input data to be read (from stdin).#include #include#includeusing namespace std;void swap( int &a, int &b){ int tmp; tmp=a; a=b 阅读全文
posted @ 2013-07-18 17:19 NinaGood 阅读(172) 评论(0) 推荐(0) 编辑
摘要: /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: TreeNode *build( vector&inorder, vector &postorder,int in_s,int in_e, int pos_s, int pos_e) ... 阅读全文
posted @ 2013-07-01 08:39 NinaGood 阅读(113) 评论(0) 推荐(0) 编辑
摘要: 递归实现/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: TreeNode *build( vector&preorder,vector &inorder,int start,int end,int start1,int end1) ... 阅读全文
posted @ 2013-06-30 22:04 NinaGood 阅读(173) 评论(0) 推荐(0) 编辑
摘要: /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ #define neg -10000 struct Node { int val1; int val2; Node():val1(neg),val2(neg){}; }; class Solution {public... 阅读全文
posted @ 2013-06-28 20:45 NinaGood 阅读(162) 评论(0) 推荐(0) 编辑
摘要: http://www.cnblogs.com/miloyip/archive/2010/02/25/1673114.html 阅读全文
posted @ 2013-06-28 14:14 NinaGood 阅读(115) 评论(0) 推荐(0) 编辑
摘要: 回文数:"回文数"是一种数字。如:98789, 这个数字正读是98789,倒读也是98789,正读倒读一样,所以这个数字就是回文数。第一种方法:把数的每位上的数存储到数组中,接着对数组进行判断,用两个指针分别指向数组的头和尾,并判断这两个位置上的数字是否相等,知道头指针不再等于尾指针;#define MAX 50bool isHuiwen( long n ){ int s[MAX]; int tmp = n; int i = 0; while( tmp ) {s[i] = tmp % 10;tmp = tmp/10;i++; } int start=0; int end = 阅读全文
posted @ 2013-06-27 09:48 NinaGood 阅读(353) 评论(0) 推荐(0) 编辑
摘要: 折半查找:class Solution {public: int searchInsert(int A[], int n, int target) { // Start typing your C/C++ solution below // DO NOT write int main() function if( n <= 0 ) { return 0; } int low = 0; int high = n-1; while( low <... 阅读全文
posted @ 2013-06-20 10:05 NinaGood 阅读(138) 评论(0) 推荐(0) 编辑
摘要: 1.只需要遍历数组一遍;使用start记录存储0的位置;使用end记录存储2的位置;A[cur]等于1时往后移动cur;A[cur]等于其他则进行交换;class Solution {public: inline void swap( int &a, int &b) { int tmp; tmp=a; a=b; b=tmp; } void sortColors(int A[], int n) { // Start typing your C/C++ solution below ... 阅读全文
posted @ 2013-06-18 21:04 NinaGood 阅读(201) 评论(0) 推荐(0) 编辑