another good solution
redace85
@redace85
Posts made by redace85

RE: Accepted C Solution with explanation
u're welcome. I think what u have learned by solving these problems matters most.

Accepted C Solution with explanation
bool canWinNim(int n) { return n%4; }
well,the code is short and the important part is the idea behind the scene.
according the example,if there are 4 stones,no matter what u do,u will lose. so as a smart guy u should leave 4 stone to yr friend. cos u can remove 3 stones at max, u'd win if there are 5~7 stones in the heap.how about 8 stones in the heap? after removed,5~7 stones will be left.and yr smart friend will win by drive u into the 4 stones situation.
so u can win the game by counting the number of the stones, and make sure yr friend face the 4 stones situation.for example when n equal 15, u remove 3 stones,and 12 is 4 multiple 3.no matter how many stones yr friend removed, u can make it 8, and so on~~
BTW this is a boring game,u can win when the stones can not be divide by 4.

Accepted C Solution nearly one pass with some explanation
basic idea is scanning from both side of the array,and multiply the factors. no tricky at all.
int* productExceptSelf(int* nums, int numsSize, int* returnSize) { if(0==numsSize) return 0; int *ret=malloc(numsSize*sizeof(int)); *returnSize=numsSize; int i=0; while(i<numsSize){ ret[i]=1; ++i; } i=1; int right=1; int left=1; while(i<numsSize){ left*=nums[i1]; ret[i]*=left; right*=nums[numsSizei]; ret[numsSizei1]*=right; ++i; } return ret; }

Accepted C recursive solution
well, it's too simple to explain,just make use of BST
struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) { if (p>val<root>val && q>val<root>val) return lowestCommonAncestor(root>left,p,q); if (p>val>root>val && q>val>root>val) return lowestCommonAncestor(root>right,p,q); return root; }

Accepted C Solution Rt(N) Sp(1) and restored the list
when come to single linked list,there are few primary operations you should always keep in mind,like reverse,merge,find certain node etc.then u can solve all the problems with a combination of these operations.
bool isPalindrome(struct ListNode* head) { if(!head  !head>next) return true; struct ListNode *pslow=head; struct ListNode *pfast=head>next; struct ListNode *pp=NULL; struct ListNode *pt=NULL; struct ListNode *ph=NULL; //reverse the first half while(pfast && pfast>next){ pt=pslow>next; pslow>next=pp; pp=pslow; pslow=pt; pfast=pfast>next>next; } //odd or even pt=pslow>next; if(pfast){ pslow>next=pp; head=pslow; ph=pt; }else{ head=pp; ph=pslow; } //restore and judge bool res=true; while(head){ if(res && head>val!=pt>val){ res=false; } pp=head>next; head>next=ph; ph=head; head=pp; pt=pt>next; } return res; }