03 2014 档案

Java Enum
摘要:JDK APIEnumprotected Enum(String name, int ordinal)单独的构造方法。程序员无法调用此构造方法。该构造方法用于由响应枚举类型声明的编译器发出的代码。 参数:name - - 此枚举常量的名称,它是用来声明该常量的标识符。ordinal - - 枚举常量的序数(它在枚举声明中的位置,其中初始常量序数为零)。转自:http://www.cnblogs.com/frankliiu-java/archive/2010/12/07/1898721.htmlJava中枚举实现的分析:示例:publicenumColor{RED,B... 阅读全文

posted @ 2014-03-31 22:25 crane_practice 阅读(316) 评论(0) 推荐(0)

Java 定义常量
摘要:转自:http://www.softservice.org.cn/html/zjbk/2012-8/7685.html方法一采用接口(Interface)的中变量默认为static final的特性。方法二采用了Java 5.0中引入的Enum类型。方法三采用了在普通类中使用static final修饰变量的方法。方法四类似方法三,但是通过函数来获取常量。首先定义全局变量似乎有违Java的面向对象的封装特性,增加的耦合。所以最佳的方法是避免定义全局变量。如果是参数等,可以写入配置文件。如果实在是必须的,方法二是最为推荐的。方法三是大家都能想到的,非常的直观。方法一和方法三本质上一样。方法四提供 阅读全文

posted @ 2014-03-31 22:22 crane_practice 阅读(327) 评论(0) 推荐(0)

交换战报???
摘要:战报交流:战场上不同的位置有N个战士(n>4),每个战士知道当前的一些战况,现在需要这n个战士通过通话交流,互相传达自己知道的战况信息,每次通话,可以让通话的双方知道对方的所有情报,设计算法,使用最少的通话次数,是的战场上的n个士兵知道所有的战况信息,不需要写程序代码,得出最少的通话次数。这个用分治f(n)=2f(n/2)+n/2注意这个+n/2 没错,就是+n/2不要用master定理去求,就展开求但是求的时候终止条件是啥,f(1)?f(2)?···结果是2n-3 阅读全文

posted @ 2014-03-28 21:41 crane_practice 阅读(164) 评论(0) 推荐(0)

数星星
摘要:A和B晚上无聊就开始数星星。每次只能数K个(20<=k<=30)A和B轮流数。最后谁把星星数完谁就获胜,那么当星星数量为多少时候A必胜?n=r*(m+n)+s只要满足这个就可以了:其中m、n是取的范围,r为任意自然数,m<=s<=n巴什博奕 阅读全文

posted @ 2014-03-28 19:56 crane_practice 阅读(142) 评论(0) 推荐(0)

堆、栈、free
摘要:转自:http://codeup.org/archives/212http://bbs.bccn.net/thread-82212-1-1.htmlhttp://www.cppblog.com/oosky/archive/2006/01/21/2958.html如果你在函数上面定义了一个指针变量,然后在这个函数里申请了一块内存让指针指向它。实际上,这个指针的地址是在栈上,但是它所指向的内容却是在堆上面的!千万不要认为函数返回,函数所在的栈被销毁指针也跟着销毁,申请的内存也就一样跟着销毁了!这绝对是错误的!因为申请的内存在堆上,而函数所在的栈被销毁跟堆完全没有啥关系。所以,还是那句话:记得释放! 阅读全文

posted @ 2014-03-28 14:46 crane_practice 阅读(323) 评论(0) 推荐(1)

进程的状态转换
摘要:转自:http://www.tantengvip.com/2011/10/state/四种进程间的状态转换:进程的状态转换图1.就绪–>执行 2.执行–>就绪 3.执行–>阻塞 4.阻塞–>就绪一、进程的三种基本状态进程在运行中不断地改变其运行状态。通常,一个运行进程必须具有以下三种基本状态。就绪(Ready)状态当进程已分配到除CPU以外的所有必要的资源,只要获得处理机便可立即执行,这时的进程状态称为就绪状态。执行(Running)状态当进程已获得处理机,其程序正在处理机上执行,此时的进程状态称为执行状态。阻塞(Blocked)状态正在执行的进程,由于等待某个事件发生 阅读全文

posted @ 2014-03-28 11:09 crane_practice 阅读(1006) 评论(0) 推荐(0)

Swap Nodes in Pairs
摘要:1 ListNode *swapPairs(ListNode *head) { 2 if(head==NULL||head->next==NULL)//链表为空或者只有一个节点 3 return head; 4 ListNode *p,*q,*h; 5 p=head; 6 q=head->next; 7 p->next=q->next; 8 q->next=p; 9 head=q;//头处理完了,其实这个时候就和一般情况下交换过之后是一样的了,就可以... 阅读全文

posted @ 2014-03-26 22:27 crane_practice 阅读(111) 评论(0) 推荐(0)

Maximum Subarray
摘要:1 /* 2 从头开始,每次加过之后和之前的最大值比较,若比最大值大,则更新最大值 3 如果加到某个数时,值为负,那么下次累加的起点就要改变 4 */ 5 int maxSubArray(int A[], int n) { 6 if(A==NULL||nmax?sum:max;12 if(sum<0)13 sum=0;//如果加到某个数时,值为负,那么下次累加的起点就要改变14 }15 return max;16 }AC 阅读全文

posted @ 2014-03-25 20:22 crane_practice 阅读(99) 评论(0) 推荐(0)

Unique Paths
摘要:1 int uniquePaths(int m, int n) { 2 if(m==0||n==0) 3 return 0; 4 int A[m][n]; 5 int i,j; 6 for(i=0;i<m;++i) 7 A[i][0]=1; 8 for(i=1;i<n;++i) 9 A[0][i]=1;10 for(i=1;i<m;++i){11 for(j=1;j<n;++j)12 ... 阅读全文

posted @ 2014-03-25 16:48 crane_practice 阅读(149) 评论(0) 推荐(0)

Merge Two Sorted Lists
摘要:1 ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { 2 if(l1==NULL) 3 return l2; 4 if(l2==NULL) 5 return l1; 6 ListNode *head,*p; 7 if(l1->valval){ 8 head=l1; 9 l1=l1->next;10 }11 else{12 ... 阅读全文

posted @ 2014-03-25 11:45 crane_practice 阅读(99) 评论(0) 推荐(0)

Climbing Stairs
摘要:1 int climbStairs(int n) 2 { 3 if(n<=0) 4 return 0; 5 if(n==1) 6 return 1; 7 if(n==2) 8 return 2; 9 return climbStairs(n-1)+climbStairs(n-2);10 }青蛙跳台阶问题,第一反应就是上面的代码了,结果是 超时 1 int climbStairs(int n) { 2 if(n<=0) 3 return 0; 4 in... 阅读全文

posted @ 2014-03-25 11:06 crane_practice 阅读(124) 评论(0) 推荐(0)

Sort Colors
摘要:其实这个Follow up说的也很好哇,计数排序A rather straight forward solution is a two-pass algorithm using counting sort.First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's. 1 void sortColors(int A[], int n) 阅读全文

posted @ 2014-03-24 19:49 crane_practice 阅读(184) 评论(0) 推荐(0)

Merge Sorted Array
摘要:如果另外开辟一个数组C,A,B从头开始比较,谁小,谁最先放到中,题上说最后都放到A中,那么就扫描C,复制数据就行了,时间复杂度O(m+n),空间复杂度O(m+n)当然还有比这个好的办法,空间复杂度O(1). 1 void merge(int A[], int m, int B[], int n) { 2 int C[m+n]; 3 int i=0,j=0,k=0; 4 while(i=0&&j>=0){ 7 if(A[i]=0){18 A[k]=A[i];19 k--... 阅读全文

posted @ 2014-03-23 19:17 crane_practice 阅读(135) 评论(0) 推荐(0)

Gray Code
摘要:1 //reference http://blog.csdn.net/fightforyourdream/article/details/14517973 2 vector grayCode(int n) { 3 vector codes; 4 if(n>1;11 codes.push_back(tmp);12 }13 return codes;14 } 阅读全文

posted @ 2014-03-22 17:35 crane_practice 阅读(138) 评论(0) 推荐(0)

Populating Next Right Pointers in Each Node II ?
摘要:1 void connect(TreeLinkNode *root) { 2 if(root==NULL) 3 return; 4 if(root->left&&root->right) 5 root->left->next=root->right; 6 if(root->next){ 7 if(root->left!=NULL&&root->right==NULL){ 8 if(root->next->left) 9 ... 阅读全文

posted @ 2014-03-22 11:59 crane_practice 阅读(165) 评论(0) 推荐(0)

Populating Next Right Pointers in Each Node
摘要:1 //按说层次遍历最方便了,但是空间复杂度不满足题意 2 //遍历吧,可是像例子中的,5—>6怎么实现呢 3 //You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children). 4 //这个提示是什么意思? 5 void connect(TreeLinkNode *root) { 6 if(root==NULL) 7 return;... 阅读全文

posted @ 2014-03-22 10:51 crane_practice 阅读(206) 评论(0) 推荐(0)

Pascal's Triangle
摘要:1 vector > generate(int numRows) { 2 int i,j; 3 vector > intVV; 4 vector intV; 5 if(numRows >' 7 return intVV; 8 intV[0].push_back(1); 9 intVV[0].push_back(intV[0]);10 for(i=1;i > generate(int numRows) { 2 int i,j; 3 ... 阅读全文

posted @ 2014-03-21 23:01 crane_practice 阅读(208) 评论(0) 推荐(0)

Best Time to Buy and Sell Stock
摘要:先说点股票的事,对股票一无所知,我问明哥,比如说我今天10元买了个股票,明天11元,后天12元,那我后天是不是赚了3元?他说不是,股票只有卖了才有钱,也就是说后天卖了的话,赚2元。一般都会提到的算法是分治和动态规划(《剑指offer》中的第一种方法也很好,容易理解),动态规划后面再去实现,看到http://blog.csdn.net/iammiaoyan/article/details/11730059中说的另外一种感觉容易被忽视的方法(虽然很常见):要求最大的收益,自然是用最高的股票价格减去最低的价格即可,考虑到时间的因素,当前的收益一定是当天价格减去前面几天里的最低价格即可。 1 ... 阅读全文

posted @ 2014-03-21 22:02 crane_practice 阅读(200) 评论(0) 推荐(0)

Java 对象和实例的区别
摘要:本来我以为是一样的,其实是不一样的参看:http://www.blogjava.net/dreamstone/archive/2011/06/03/101733.html 阅读全文

posted @ 2014-03-21 13:28 crane_practice 阅读(292) 评论(0) 推荐(0)

java中抽象类跟接口的区别
摘要:额,好吧,本来是打算转载些神马的,但是搜资料的过程中发现了一个专注与java的人,那就关注啦,以后多进他blog学习学习:http://www.cnblogs.com/chenssy/p/3376708.html就这个问题来说,这个写的也很不错:http://www.xl7788.com/zt/computerprogram/JavaInterface.htmlJava抽象类与接口一个软件设计的好坏,我想很大程度上取决于它的整体架构,而这个整体架构其实就是你对整个宏观商业业务的抽象框架,当代表业务逻辑的高层抽象层结构 合理时,你底层的具体实现需要考虑的就仅仅是一些算法和一些具体的业务实现了。当 阅读全文

posted @ 2014-03-20 22:25 crane_practice 阅读(226) 评论(0) 推荐(0)

Single Number II
摘要:转自:http://blog.csdn.net/kenden23/article/details/136252971 int singleNumber(int A[], int n) { 2 for(int i = 0; i 3<2不成立,然后呢,就木有然后了 1 int singleNumber(int A[], int n) { 2 if(A==NULL||n<=0) 3 return 0; 4 //if(n<=2) 5 //return A[0]; 6 sort... 阅读全文

posted @ 2014-03-19 17:30 crane_practice 阅读(149) 评论(0) 推荐(0)

Single Number
摘要:1 int singleNumber(int A[], int n) {2 if(A==NULL||n<=0)//这是个好习惯,防止程序崩溃3 return 0;4 int i;5 int result=A[0];6 for(i=1;i<n;++i)7 result ^= A[i];8 return result;9 } 阅读全文

posted @ 2014-03-19 15:23 crane_practice 阅读(97) 评论(0) 推荐(0)

Linked List Cycle II
摘要:1 ListNode *detectCycle(ListNode *head) { 2 if(!head||!head->next) 3 return NULL; 4 ListNode *fast,*slow; 5 fast=head; 6 slow=head; 7 while(fast&&fast->next) 8 { 9 slow=slow->next;10 fast=fast->next->next;11 ... 阅读全文

posted @ 2014-03-18 21:20 crane_practice 阅读(118) 评论(0) 推荐(0)

Linked List Cycle
摘要:1 bool hasCycle(ListNode *head) { 2 if(head==NULL) 3 return false; 4 ListNode *pBefore,*pLater; 5 if(head->next) 6 pBefore=head->next; 7 else 8 return false; 9 pLater=head;10 while(pBefore)11 {12 ... 阅读全文

posted @ 2014-03-18 17:29 crane_practice 阅读(541) 评论(0) 推荐(0)

字符串,字符数组(C/C++)
摘要:这个地方困惑我好久了,废话不多说 char c1[]="12345"; char *c2="12345"; string c3="12345"; int a1=strlen(c1); int a2=strlen(c2); int a3=strlen(c3);//error: cannot convert 'std::string {aka std::basic_string}' to 'const char*' for argument '1' to 'size_t strlen 阅读全文

posted @ 2014-03-14 10:21 crane_practice 阅读(903) 评论(0) 推荐(0)

空格替换
摘要:1 #include 2 #include 3 using namespace std; 4 5 void f(char *s) 6 { 7 char *p=s; 8 while(*s!='\0') 9 {10 if(*s==' ')11 {12 while(*p!='\0')13 p++;14 while(p>s)15 {16 *(p+2)=*p;17 p--;1... 阅读全文

posted @ 2014-03-13 10:21 crane_practice 阅读(178) 评论(0) 推荐(0)

Palindrome Number
摘要:1 bool isPalindrome(int x) { 2 if(x=10) 9 div*=10;10 while(x)11 {12 i=x/div;13 j=x%10;14 if(i!=j)15 return false;16 x=(x%div)/10;17 div /= 100;18 }19 return true;20 ... 阅读全文

posted @ 2014-03-12 21:09 crane_practice 阅读(181) 评论(0) 推荐(0)

String to Integer (atoi) ???
摘要:1 #define INT_MAX 2147483647 2 #define INT_MIN -2147483648 3 class Solution { 4 public: 5 int atoi(const char *str) { 6 //string如果能转换成int的话,各位都要是数字,一个例外是第一个字符可以是负号 7 int n=strlen(str); 8 int i=0; 9 int flag=0;//0表示正值,1表示负值10 int val=0;11 /*12 ... 阅读全文

posted @ 2014-03-12 09:27 crane_practice 阅读(420) 评论(0) 推荐(0)

Reverse Integer
摘要:1 int reverse(int x) {2 int y=0;3 while(x)4 {5 y=y*10+x%10;6 x=x/10;7 }8 return y;9 }我试过了,不用区分是正数还是负数,他们的计算方法是一样的上面的代码虽然AC了,不过题上spoilers说的好,有可能会溢出,这个上述代码并没有考虑32位,1位符号位,剩下31位,表示范围 From−2,147,483,648 to 2,147,483,647, from −(231) ... 阅读全文

posted @ 2014-03-11 15:44 crane_practice 阅读(184) 评论(0) 推荐(0)

Two Sum
摘要:之前遇到一道题和这个基本一样,数组中元素各不相同,另外创建一个数组,里面的元素为target依次减去原数组中所有元素,然后两个数组合并,排序,扫描,如果有相邻元素相等并且不等于target的一半,则说明原数组中有两个不同的元素和为target。不过这个题目的返回值是两个,这个怎么做到?而且函数的返回值类型为vector······这个我现在真不会,参看了别人的写法才知道是这么写的:vectorret(2,0)Submission Result:Runtime Error,不要怕错,要从错误中吸取经验和教训 1 class Soluti 阅读全文

posted @ 2014-03-11 14:42 crane_practice 阅读(234) 评论(0) 推荐(0)

Path Sum
摘要:常规做法是算出每一条路径的和然后和sum比较。类似的题目是打印出从根节点到每个叶子节点的路径。之前学过分支界限法,刚开始觉得可以用来解这道题,不过再想想的话觉得应该不行,分支界限法是找最优化的。常规的做法中加上一些小的判别,会付出一些代价,不过也许会比盲目相加好一些吧。比如说每到一个结点sum就减去那个结点的值,如果结果小于0就不走这条路了。不过问题是题目上并没有说结点的值都是正值呀,所以这样不行。二叉树的非递归遍历参看:http://blog.csdn.net/kofsky/article/details/2886453/*http://www.cppblog.com/CodeStream/ 阅读全文

posted @ 2014-03-09 21:01 crane_practice 阅读(198) 评论(0) 推荐(0)

Minimum Depth of Binary Tree
摘要:Maximum Depth of Binary Tree可以这样写1 int maxDepth(TreeNode *root) {2 if(!root)3 return 0;4 else5 return 1+max(maxDepth(root->left),maxDepth(root->right));6 }Minimum Depth of Binary Tree如果仿照上面1 int minDepth(TreeNode *root) {2 if(!root)3 re... 阅读全文

posted @ 2014-03-09 19:25 crane_practice 阅读(215) 评论(0) 推荐(0)

Plus One
摘要:我觉得逻辑上是清晰的,但是写起来很别扭,感觉不好写,写出来,结果是错的······而且vector,哪里是头哪里是尾?12345我的想法是1是尾,5是头,每次添加都是从尾添加的。而且这个表示的数应该是12345而不是54321,不知道这么理解对不? 1 class Solution { 2 public: 3 vector plusOne(vector &digits) { 4 vector::size_type i,n; 5 n=digits.size(); 6 for(i=0;i' [-fpermissive]13 阅读全文

posted @ 2014-03-09 19:01 crane_practice 阅读(172) 评论(0) 推荐(0)

Maximum Depth of Binary Tree
摘要:第一反应是用按层次遍历,用队列做。看了别人的代码才想起来也可以用递归去做。就是比较根的左右子树的深度,深度大的那个再加一就是根的深度了。递归: 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(NULL), right(NULL) {} 8 * }; 9 */10 class Solution {11 p... 阅读全文

posted @ 2014-03-08 10:39 crane_practice 阅读(297) 评论(0) 推荐(0)

Symmetric Tree
摘要: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(NULL), right(NULL) {} 8 * }; 9 */10 class Solution {11 public:12 bool isSymmetricSame(TreeNode *p,TreeNode *q)13 {14 ... 阅读全文

posted @ 2014-03-07 11:04 crane_practice 阅读(111) 评论(0) 推荐(0)

Same Tree
摘要:我觉得和树的遍历很像,想用递归,不过返回值那块我很迷茫,递归使用栈,返回值是返回给上一层的,我怎么才能让递归只返回一个bool值呢,阶乘可以用递归来写,返回值也只有一个,如果我一定这样写的话,我就让返回值做“与”操作,感觉程序写出来很ugly,不管了,小菜鸟先这么写着,之后再参看网上大牛的大作,嘿嘿呃,也许不是吧,递归中return到底是什么样子的呀,写个小程序看看吧: 1 #include 2 3 using namespace std; 4 5 //判断2个数组中元素是否都相等 6 //程序本身没有什么意思,一些条件也没有判断,比如说为空什么的, 7 //我只是想看看递归中retur... 阅读全文

posted @ 2014-03-07 10:33 crane_practice 阅读(195) 评论(0) 推荐(0)

Remove Duplicates from Sorted List
摘要:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *deleteDuplicates(ListNode *head) { if(head) { ListNode *p,*q; p=head; ... 阅读全文

posted @ 2014-03-06 21:21 crane_practice 阅读(120) 评论(0) 推荐(0)

Length of Last Word
摘要:1 class Solution { 2 public: 3 int lengthOfLastWord(const char *s) { 4 int length=0; 5 int i=0; 6 int n=0;//存储遇到空格之前的word的长度 7 if(s!=NULL) 8 { 9 10 while(s[i]!='\0')11 {12 while(s[i]!=' ')13 ... 阅读全文

posted @ 2014-03-06 19:31 crane_practice 阅读(227) 评论(0) 推荐(0)

Remove Element
摘要:源码: 1 class Solution { 2 public: 3 int removeElement(int A[], int n, int elem) { 4 sort(A,A+n); 5 int i,j; 6 i=0; 7 j=n-1; 8 while(i<=j) 9 {10 if(A[i]!=elem)11 {12 i++;13 }14 else15 ... 阅读全文

posted @ 2014-03-05 18:49 crane_practice 阅读(161) 评论(0) 推荐(0)

Remove Duplicates from Sorted Array
摘要:我的思路:如果一个一个比较的话就是太野蛮的写法了,可以先排序在处理关于排序函数参看:http://www.cnblogs.com/ForeverJoker/archive/2013/05/25/qsort-sort.html(在本题中题目上说了Given a sorted array,人家已经排好序啦``````)刚开始写的,结果:Time Limit Exceeded 1 class Solution { 2 public: 3 int removeDuplicates(int A[], int n) { 4 if(n1,2,2,3——>1,2,3,3但是,如果是1,... 阅读全文

posted @ 2014-03-05 18:38 crane_practice 阅读(215) 评论(0) 推荐(0)

导航