├── .gitignore ├── 10-numberof1.c ├── 11-power.c ├── 11-power_rec.c ├── 12-print1ToNDigits.c ├── 13-deleteNode.c ├── 14-reorderArray.c ├── 15-last_Kth_node.c ├── 16-reverse_list.c ├── 17-merge_sortedList.c ├── 18-subTree.c ├── 19-treeMirror.c ├── 20-printMatrixClockwisely.c ├── 21-minStack.cpp ├── 22-stackPopOrder.c ├── 23-levelOrder.cpp ├── 24-isPostOrderSearchTree.c ├── 25-pathInTree.cpp ├── 26-copyComlexlist.c ├── 27-convertBST.c ├── 28-strPermutation.c ├── 29-more_than_half.c ├── 3-find.c ├── 30-k_least_number.c ├── 31-greatest_sum_of_subarrays.c ├── 32-numof1_recursive.c ├── 32-numof1_simple.c ├── 33-sort_as_min_number.cpp ├── 34-ugly_number.c ├── 35-first_no_repeat.c ├── 36-inverse_pairs.cpp ├── 37-first_common_node.c ├── 38-number_of_k.c ├── 39-is_balanced_tree.c ├── 39-tree_depth.c ├── 4-replaceblank.c ├── 40-appear_once_num.c ├── 41-sequence_withsum.c ├── 41-two_num_withsum.c ├── 42-reverse_words.c ├── 42-rotate_left.c ├── 43-dices_probability.c ├── 44-cards_continue.c ├── 45-last_num_in_circle.cpp ├── 46-accumulate.c ├── 47-add_two_num.c ├── 49-string_to_int.c ├── 5-printListReversingly.c ├── 50-lowest_common_parent.c ├── 6-buildBinarytree.c ├── 7-stackQueue.c ├── 8-minInRotate.c ├── 9-fibonacci.c ├── 9-frogjump2.c ├── 9-frogjumpN.c ├── PermutationSequence.cpp ├── README.md ├── atoi.cpp ├── count_and_say.cpp ├── decodeWays.cpp ├── editDistance.cpp ├── generate_parenthesis.cpp ├── jobdu1530-longest_substr.c ├── jobdu1531-cash_value.c ├── linkedListCycle.cpp ├── longest_common_substr.c ├── minStack.cpp ├── nextPermutation.cpp ├── validPalindrome.cpp └── validSudoku.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | # Object files 2 | *.o 3 | *.ko 4 | 5 | # Libraries 6 | *.lib 7 | *.a 8 | 9 | # Shared objects (inc. Windows DLLs) 10 | *.dll 11 | *.so 12 | *.so.* 13 | *.dylib 14 | 15 | # Executables 16 | *.exe 17 | *.out 18 | *.app 19 | -------------------------------------------------------------------------------- /10-numberof1.c: -------------------------------------------------------------------------------- 1 | #include 2 | int numberof1(int n) 3 | { 4 | int count = 0; 5 | while(n) 6 | { 7 | ++count; 8 | n=(n-1) & n; 9 | } 10 | return count; 11 | } 12 | int main() 13 | { 14 | int n; 15 | scanf("%d",&n); 16 | printf("%d\n",numberof1(n)); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /11-power.c: -------------------------------------------------------------------------------- 1 | #include 2 | int flag_invalid_input = 0; 3 | 4 | int equal(double num1,double num2) 5 | { 6 | if((num1 - num2 < 0.0000001) && (num1 - num2 > -0.0000001)) 7 | return 1; 8 | else 9 | return 0; 10 | } 11 | 12 | double power(double base,int exponent) 13 | { 14 | double result = 1.0; 15 | unsigned int exponent_value ; 16 | int i; 17 | if(equal(base,0.0) && exponent<0) 18 | { 19 | flag_invalid_input = 1; 20 | return 0.0; 21 | } 22 | if(exponent<0) 23 | exponent_value = (unsigned int)(-exponent); 24 | else 25 | exponent_value = exponent; 26 | for(i=1;i<=exponent_value;i++) 27 | result *=base; 28 | if(exponent <0) 29 | result = 1.0/result; 30 | return result; 31 | } 32 | 33 | int main() 34 | { 35 | double base; 36 | int exponent; 37 | scanf("%lf%d",&base,&exponent); 38 | printf("%lf\n",power(base,exponent)); 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /11-power_rec.c: -------------------------------------------------------------------------------- 1 | #include 2 | int flag_invalid_input = 0; 3 | 4 | int equal(double num1,double num2) 5 | { 6 | if((num1 - num2 < 0.0000001) && (num1 - num2 > -0.0000001)) 7 | return 1; 8 | else 9 | return 0; 10 | } 11 | 12 | double power_rec(double base,unsigned int exponent_value) 13 | { 14 | double result; 15 | if(exponent_value == 0) 16 | return 1; 17 | if(exponent_value == 1) 18 | return base; 19 | result = power_rec(base,exponent_value>>1); 20 | result *=result; 21 | if(exponent_value &0x1 ==1) 22 | result *=base; 23 | return result; 24 | } 25 | 26 | double power(double base,int exponent) 27 | { 28 | double result = 1.0; 29 | unsigned int exponent_value ; 30 | int i; 31 | if(equal(base,0.0) && exponent<0) 32 | { 33 | flag_invalid_input = 1; 34 | return 0.0; 35 | } 36 | if(exponent<0) 37 | exponent_value = (unsigned int)(-exponent); 38 | else 39 | exponent_value = exponent; 40 | result =power_rec(base,exponent_value); 41 | if(exponent <0) 42 | result = 1.0/result; 43 | return result; 44 | } 45 | 46 | int main() 47 | { 48 | double base; 49 | int exponent; 50 | scanf("%lf%d",&base,&exponent); 51 | printf("%lf\n",power(base,exponent)); 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /12-print1ToNDigits.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int increase(char *number) 5 | { 6 | int overflow = 0; 7 | int takeOver = 0; 8 | int length = strlen(number); 9 | int i=0 ,sum; 10 | for(i=length-1;i>=0;i--) 11 | { 12 | sum = number[i]-'0'+takeOver; 13 | if(i==length-1) 14 | sum++; 15 | if(sum>=10) 16 | { 17 | if(i==0) 18 | overflow = 1; 19 | else 20 | { 21 | sum-=10; 22 | takeOver=1; 23 | number[i]='0'+sum; 24 | } 25 | } 26 | else 27 | { 28 | number[i]='0'+sum; 29 | break; 30 | } 31 | } 32 | return overflow; 33 | } 34 | void printNumber(char *number) 35 | { 36 | int isBegin0 = 1; 37 | int length = strlen(number); 38 | int i=0; 39 | for(i=0;i 2 | #include 3 | typedef struct node{ 4 | int data; 5 | struct node *next; 6 | }list_node; 7 | 8 | list_node *pToDelete=NULL; 9 | //创建链表。 10 | list_node *createList() 11 | { 12 | int n; 13 | list_node *pHead=NULL,*p; 14 | list_node *tail= pHead; 15 | int node_data[]={0,1,2,3,4,5,6,7,8}; 16 | int i=0; 17 | while(i<9) 18 | { 19 | p=(list_node *)malloc(sizeof(list_node)); 20 | p->data=node_data[i]; 21 | p->next=NULL; 22 | if(pHead==NULL) 23 | pHead=p; 24 | else 25 | tail->next=p; 26 | tail=p; 27 | if(i==4) 28 | pToDelete=tail; 29 | i++; 30 | //假设要删除的节点就是4 31 | } 32 | return pHead; 33 | } 34 | 35 | //因为可能要改变头节点的值,所以要传二维指针 36 | void delete_node(list_node **ppHead,list_node *pToDelete) 37 | { 38 | if(!ppHead||!pToDelete) 39 | return; 40 | //普通情况,不是删尾节点 41 | if(pToDelete->next!=NULL) 42 | { 43 | list_node *pnext=pToDelete->next; 44 | pToDelete->data=pnext->data; 45 | pToDelete->next=pnext->next; 46 | free(pnext); 47 | pnext=NULL; 48 | } 49 | //链表只有一个节点,就是删除这个节点 50 | else if(*ppHead==pToDelete) 51 | { 52 | free(pToDelete); 53 | pToDelete=NULL; 54 | *ppHead=NULL; 55 | } 56 | else //链表有多个节点,删除尾节点,这只有遍历了。 57 | { 58 | list_node *pnode=*ppHead; 59 | while(pnode->next!=pToDelete) 60 | pnode=pnode->next; 61 | pnode->next=NULL; 62 | free(pToDelete); 63 | pToDelete=NULL; 64 | } 65 | } 66 | 67 | //打印链表 68 | void print_list(list_node *pHead) 69 | { 70 | while(pHead) 71 | { 72 | printf("%d\t",pHead->data); 73 | pHead=pHead->next; 74 | } 75 | printf("\n"); 76 | } 77 | 78 | int main() 79 | { 80 | list_node *pHead=createList(); 81 | print_list(pHead); 82 | delete_node(&pHead,pToDelete); 83 | print_list(pHead); 84 | return 0; 85 | } 86 | -------------------------------------------------------------------------------- /14-reorderArray.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void reorder(int *pdata,int length) 4 | { 5 | int temp; 6 | if(!pdata||length==0) 7 | return; 8 | int *pfront=pdata; 9 | int *pend=pdata+length-1; 10 | while(pfront 2 | #include 3 | typedef struct node{ 4 | int key; 5 | struct node *next; 6 | }ListNode; 7 | 8 | ListNode *createList() 9 | { 10 | int key; 11 | ListNode *p=NULL; 12 | ListNode *pHead=NULL, *tail=NULL; 13 | scanf("%d",&key); 14 | while(key!=-1) 15 | { 16 | p=(ListNode *)malloc(sizeof(ListNode)); 17 | p->key=key; 18 | p->next=NULL; 19 | if(pHead == NULL) 20 | { 21 | pHead=p; 22 | tail=p; 23 | } 24 | else 25 | { 26 | tail->next=p; 27 | tail=tail->next; 28 | } 29 | scanf("%d",&key); 30 | } 31 | return pHead; 32 | } 33 | 34 | ListNode *last_KthNode(ListNode *phead,int k) 35 | { 36 | int i=0; 37 | ListNode *pAhead=phead; 38 | ListNode *pBehind=NULL; 39 | if(phead==NULL||k==0) 40 | return NULL; 41 | for(;inext) 44 | pAhead=pAhead->next; 45 | else //链表不足K长度 46 | return NULL; 47 | } 48 | pBehind = phead; 49 | while(pAhead->next) 50 | { 51 | pAhead=pAhead->next; 52 | pBehind=pBehind->next; 53 | } 54 | return pBehind; 55 | } 56 | 57 | int main() 58 | { 59 | ListNode *pHead=NULL; 60 | ListNode *last_K=NULL; 61 | int k; 62 | pHead=createList(); 63 | printf("input K:"); 64 | scanf("%d",&k); 65 | last_K=last_KthNode(pHead,k); 66 | if(last_K) 67 | printf("last K:%d\n",last_K->key); 68 | else 69 | printf("No last K node.\n"); 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /16-reverse_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef struct node{ 4 | int key; 5 | struct node *next; 6 | }ListNode; 7 | 8 | ListNode *createList() 9 | { 10 | int key; 11 | ListNode *p=NULL; 12 | ListNode *pHead=NULL, *tail=NULL; 13 | scanf("%d",&key); 14 | while(key!=-1) 15 | { 16 | p=(ListNode *)malloc(sizeof(ListNode)); 17 | p->key=key; 18 | p->next=NULL; 19 | if(pHead == NULL) 20 | { 21 | pHead=p; 22 | tail=p; 23 | } 24 | else 25 | { 26 | tail->next=p; 27 | tail=tail->next; 28 | } 29 | scanf("%d",&key); 30 | } 31 | return pHead; 32 | } 33 | 34 | void printList(ListNode *phead) 35 | { 36 | while(phead) 37 | { 38 | printf("%d\t",phead->key); 39 | phead=phead->next; 40 | } 41 | printf("\n"); 42 | } 43 | 44 | ListNode *reverseList(ListNode *phead) 45 | { 46 | ListNode *p1,*p2,*p3; 47 | if(!phead||!phead->next) 48 | return phead; 49 | p1=phead; 50 | p2=p1->next; 51 | while(p2->next) 52 | { 53 | p3=p2->next; 54 | p2->next=p1; 55 | p1=p2; 56 | p2=p3; 57 | p3=p3->next; 58 | } 59 | phead->next=NULL; 60 | p2->next=p1; 61 | return p2; 62 | } 63 | 64 | void freeList(ListNode *phead) 65 | { 66 | ListNode *p=NULL; 67 | while(phead) 68 | { 69 | p=phead; 70 | phead=phead->next; 71 | free(p); 72 | } 73 | p=NULL; 74 | } 75 | 76 | int main() 77 | { 78 | ListNode *pHead=NULL; 79 | ListNode *last_K=NULL; 80 | pHead=createList(); 81 | printf("Origin List:"); 82 | printList(pHead); 83 | printf("After Reverse:"); 84 | pHead=reverseList(pHead); 85 | printList(pHead); 86 | freeList(pHead); 87 | return 0; 88 | } 89 | -------------------------------------------------------------------------------- /17-merge_sortedList.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef struct node{ 4 | int key; 5 | struct node *next; 6 | }ListNode; 7 | 8 | ListNode *createList() 9 | { 10 | int key; 11 | ListNode *p=NULL; 12 | ListNode *pHead=NULL, *tail=NULL; 13 | printf("Input sorted list node:"); 14 | scanf("%d",&key); 15 | while(key!=-1) 16 | { 17 | p=(ListNode *)malloc(sizeof(ListNode)); 18 | p->key=key; 19 | p->next=NULL; 20 | if(pHead == NULL) 21 | { 22 | pHead=p; 23 | tail=p; 24 | } 25 | else 26 | { 27 | tail->next=p; 28 | tail=tail->next; 29 | } 30 | scanf("%d",&key); 31 | } 32 | return pHead; 33 | } 34 | 35 | void freeList(ListNode *phead) 36 | { 37 | ListNode *p=NULL; 38 | while(phead) 39 | { 40 | p=phead; 41 | phead=phead->next; 42 | free(p); 43 | } 44 | p=NULL; 45 | } 46 | 47 | void printList(ListNode *phead) 48 | { 49 | while(phead) 50 | { 51 | printf("%d\t",phead->key); 52 | phead=phead->next; 53 | } 54 | printf("\n"); 55 | } 56 | 57 | ListNode *merge_sortedList(ListNode *phead1,ListNode *phead2) 58 | { 59 | ListNode *mergedHead=NULL; 60 | if(phead1==NULL) 61 | return phead2; 62 | if(phead2==NULL) 63 | return phead1; 64 | if(phead1->key < phead2->key) 65 | { 66 | mergedHead = phead1; 67 | mergedHead->next = merge_sortedList(phead2,phead1->next); 68 | } 69 | else 70 | { 71 | mergedHead = phead2; 72 | mergedHead->next = merge_sortedList(phead2->next,phead1); 73 | } 74 | return mergedHead; 75 | } 76 | 77 | int main() 78 | { 79 | ListNode *pHead1=createList(); 80 | ListNode *pHead2=createList(); 81 | ListNode *mergedHead=merge_sortedList(pHead1,pHead2); 82 | printf("After Merge:\n"); 83 | printList(mergedHead); 84 | freeList(mergedHead); 85 | return 0; 86 | } 87 | -------------------------------------------------------------------------------- /18-subTree.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct tNode{ 5 | int data; 6 | struct tNode *lchild; 7 | struct tNode *rchild; 8 | }treeNode; 9 | 10 | //按照先序遍历的方式创建二叉树 11 | treeNode *createTree() 12 | { 13 | int key; 14 | treeNode *p=NULL; 15 | scanf("%d",&key); 16 | if(key!=-1) 17 | { 18 | p=(treeNode *)malloc(sizeof(treeNode)); 19 | p->data=key; 20 | p->lchild=createTree(); 21 | p->rchild=createTree(); 22 | } 23 | else 24 | return NULL; 25 | return p; 26 | } 27 | int DoesTree1HaveTree2(treeNode *pRoot1,treeNode *pRoot2) 28 | { 29 | if(pRoot2==NULL) 30 | return 1; 31 | if(pRoot1==NULL) 32 | return 0; 33 | if(pRoot1->data!=pRoot2->data) 34 | return 0; 35 | return DoesTree1HaveTree2(pRoot1->lchild,pRoot2->lchild) && DoesTree1HaveTree2(pRoot1->rchild,pRoot2->rchild); 36 | } 37 | 38 | int hasSubTree(treeNode *pRoot1,treeNode *pRoot2) 39 | { 40 | int result=0; 41 | if(pRoot1!=NULL &&pRoot2!=NULL) 42 | { 43 | result = DoesTree1HaveTree2(pRoot1,pRoot2); 44 | if(!result) 45 | result = hasSubTree(pRoot1->lchild,pRoot2); 46 | if(!result) 47 | result = hasSubTree(pRoot1->rchild,pRoot2); 48 | } 49 | return result; 50 | } 51 | 52 | //二叉树的先序遍历递归方法 53 | void PreOrderTraverse(treeNode *T) 54 | { 55 | if(!T) 56 | return; 57 | printf("%d\t",T->data); 58 | PreOrderTraverse(T->lchild); 59 | PreOrderTraverse(T->rchild); 60 | } 61 | 62 | int main () 63 | { 64 | int result = 0; 65 | treeNode *pRoot1=createTree(); 66 | // PreOrderTraverse(pRoot1); 67 | treeNode *pRoot2=createTree(); 68 | result = hasSubTree(pRoot1,pRoot2); 69 | if(result) 70 | printf("yes\n"); 71 | else 72 | printf("no\n"); 73 | return 0; 74 | } 75 | 76 | -------------------------------------------------------------------------------- /19-treeMirror.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct tNode{ 5 | int data; 6 | struct tNode *lchild; 7 | struct tNode *rchild; 8 | }treeNode; 9 | 10 | //按照先序遍历的方式创建二叉树 11 | treeNode *createTree() 12 | { 13 | int key; 14 | treeNode *p=NULL; 15 | scanf("%d",&key); 16 | if(key!=-1) 17 | { 18 | p=(treeNode *)malloc(sizeof(treeNode)); 19 | p->data=key; 20 | p->lchild=createTree(); 21 | p->rchild=createTree(); 22 | } 23 | else 24 | return NULL; 25 | return p; 26 | } 27 | 28 | //二叉树的先序遍历递归方法 29 | void PreOrderTraverse(treeNode *T) 30 | { 31 | if(!T) 32 | return; 33 | printf("%d\t",T->data); 34 | PreOrderTraverse(T->lchild); 35 | PreOrderTraverse(T->rchild); 36 | } 37 | 38 | void mirrorTree(treeNode *pRoot) 39 | { 40 | if(pRoot == NULL) 41 | return ; 42 | if(pRoot->lchild ==NULL && pRoot->rchild == NULL) 43 | return ; 44 | treeNode *tempNode; 45 | tempNode=pRoot->lchild; 46 | pRoot->lchild = pRoot->rchild; 47 | pRoot->rchild = tempNode; 48 | if(pRoot->lchild) 49 | mirrorTree(pRoot->lchild); 50 | if(pRoot->rchild) 51 | mirrorTree(pRoot->rchild); 52 | } 53 | 54 | int main () 55 | { 56 | int result = 0; 57 | treeNode *pRoot=createTree(); 58 | PreOrderTraverse(pRoot); 59 | printf("\n"); 60 | mirrorTree(pRoot); 61 | PreOrderTraverse(pRoot); 62 | printf("\n"); 63 | return 0; 64 | } 65 | 66 | -------------------------------------------------------------------------------- /20-printMatrixClockwisely.c: -------------------------------------------------------------------------------- 1 | #include 2 | void printCircle(int num[][4],int col,int row,int start) 3 | { 4 | int i=0; 5 | int endx=row-start-1; 6 | int endy=col-start-1; 7 | //从左往右打印一行 8 | for(i=start;i<=endx;i++) 9 | printf("%d\t",num[start][i]); 10 | //从上往下打印一列 11 | if(start=start;i--) 17 | printf("%d\t",num[endy][i]); 18 | //从下往上打印一列 19 | if(start=start+1;i--) 21 | printf("%d\t",num[i][start]); 22 | } 23 | 24 | void printClockwisely(int num[][4],int col,int row) 25 | { 26 | int start=0; 27 | if(num==NULL || col<=0 || row<=0) 28 | return; 29 | while(col >start*2 && row >start*2) 30 | { 31 | printCircle(num,col,row,start); 32 | start ++; 33 | } 34 | printf("\n"); 35 | } 36 | 37 | int main() 38 | { 39 | int num[][4]={ 40 | {1,2,3,4}, 41 | {5,6,7,8}, 42 | {9,10,11,12}, 43 | {13,14,15,16} 44 | }; 45 | printClockwisely(num,4,4); 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /21-minStack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace::std; 4 | 5 | class minStack 6 | { 7 | private: 8 | stack datastack; 9 | stack minstack; 10 | public: 11 | int top(); 12 | void pop(); 13 | void push(int); 14 | void getMin(); 15 | }; 16 | 17 | int minStack::top() 18 | { 19 | return datastack.top(); 20 | } 21 | 22 | void minStack::pop() 23 | { 24 | cout<<"pop():\t\t"; 25 | if(datastack.empty()||minstack.empty()) 26 | cout << "stack empty."<=n) 43 | minstack.push(n); 44 | minStack::getMin(); 45 | } 46 | 47 | void minStack::getMin() 48 | { 49 | if (minstack.empty()) 50 | cout<<"stack empty."< 2 | #include 3 | using namespace::std; 4 | 5 | bool isPopOrder(int *pPush ,int *pPop,int length) 6 | { 7 | bool result = false; 8 | int *nextPush=pPush; 9 | int *nextPop=pPop; 10 | if(pPush!=NULL ||pPop!=NULL||length>0) 11 | { 12 | stack stackData; 13 | while(nextPop-pPop 2 | #include 3 | #include 4 | #include 5 | using namespace::std; 6 | typedef struct tNode{ 7 | int data; 8 | struct tNode *lchild; 9 | struct tNode *rchild; 10 | }treeNode; 11 | 12 | //按照先序遍历的方式创建二叉树 13 | treeNode *createTree() 14 | { 15 | int key; 16 | treeNode *p=NULL; 17 | scanf("%d",&key); 18 | if(key!=-1) 19 | { 20 | p=(treeNode *)malloc(sizeof(treeNode)); 21 | p->data=key; 22 | p->lchild=createTree(); 23 | p->rchild=createTree(); 24 | } 25 | else 26 | return NULL; 27 | return p; 28 | } 29 | 30 | void levelOrder(treeNode *T) 31 | { 32 | if(!T) 33 | return ; 34 | dequenodeQueue; 35 | nodeQueue.push_back(T); 36 | while(!nodeQueue.empty()) 37 | { 38 | treeNode *p=nodeQueue.front(); 39 | printf("%d\t",p->data); 40 | nodeQueue.pop_front(); 41 | if(p->lchild) 42 | nodeQueue.push_back(p->lchild); 43 | if(p->rchild) 44 | nodeQueue.push_back(p->rchild); 45 | } 46 | printf("\n"); 47 | return; 48 | } 49 | 50 | int main() 51 | { 52 | treeNode *T=createTree(); 53 | levelOrder(T); 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /24-isPostOrderSearchTree.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define true 1 3 | #define false 0 4 | typedef int bool; 5 | 6 | bool isPostOrderSearchTree(int n[],int length) 7 | { 8 | if(n==NULL||length<=0) 9 | return false; 10 | int root=n[length-1]; 11 | int i=0; 12 | for(;iroot) 14 | break; 15 | int j=i; 16 | for(;j<=length-1;j++) 17 | if(n[j]0) 21 | left=isPostOrderSearchTree(n,i); 22 | bool right=true; 23 | if(i 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | vector path; 7 | 8 | typedef struct tNode{ 9 | int data; 10 | struct tNode *lchild; 11 | struct tNode *rchild; 12 | }treeNode; 13 | 14 | //按照先序遍历的方式创建二叉树 15 | treeNode *createTree() 16 | { 17 | int key; 18 | treeNode *p=NULL; 19 | scanf("%d",&key); 20 | if(key!=-1) 21 | { 22 | p=(treeNode *)malloc(sizeof(treeNode)); 23 | p->data=key; 24 | p->lchild=createTree(); 25 | p->rchild=createTree(); 26 | } 27 | else 28 | return NULL; 29 | return p; 30 | } 31 | 32 | void findPath_detail(treeNode *root,int expectedSum,vector& path,int currentSum) 33 | { 34 | currentSum+=root->data; 35 | path.push_back(root->data); 36 | bool isLeaf=root->lchild==NULL &&root->rchild==NULL; 37 | if(currentSum==expectedSum &&isLeaf) 38 | { 39 | printf("A path is found: "); 40 | vector::iterator iter=path.begin(); 41 | for(;iter!=path.end();++iter) 42 | printf("%d\t",*iter); 43 | printf("\n"); 44 | } 45 | if(root->lchild) 46 | findPath_detail(root->lchild,expectedSum,path,currentSum); 47 | if(root->rchild) 48 | findPath_detail(root->rchild,expectedSum,path,currentSum); 49 | path.pop_back(); 50 | } 51 | 52 | void findPath(treeNode *root,int expectedSum) 53 | { 54 | if(!root) 55 | return; 56 | std::vector path; 57 | int currentSum=0; 58 | findPath_detail(root,expectedSum,path,currentSum); 59 | } 60 | 61 | int main() 62 | { 63 | int expectedSum; 64 | treeNode *T=createTree(); 65 | printf("input expectedSum:\n"); 66 | scanf("%d",&expectedSum); 67 | findPath(T,expectedSum); 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /26-copyComlexlist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct node{ 5 | char ch; 6 | struct node *next; 7 | struct node *sibling; 8 | }ListNode; 9 | 10 | ListNode *createList(char value[],int sibling[],int length) 11 | { 12 | ListNode *p,*listHead=NULL,*tail=NULL; 13 | int i=0; 14 | //根据value数组创建链表 15 | while(ich=value[i]; 19 | p->sibling=NULL; 20 | if(listHead==NULL) 21 | { 22 | listHead=p; 23 | tail=p; 24 | } 25 | else 26 | { 27 | tail->next=p; 28 | tail=tail->next; 29 | } 30 | i++; 31 | } 32 | //从头节点开始,设置每个节点的sibling域 33 | p=listHead; 34 | i=0; 35 | while(p!=NULL) 36 | { 37 | ListNode *p2=listHead; 38 | int cnt=0; 39 | while(sibling[i]!=-1 && p2!=NULL) 40 | { 41 | if(cnt==sibling[i]) 42 | { 43 | p->sibling=p2; 44 | break; 45 | } 46 | else 47 | { 48 | cnt++; 49 | p2=p2->next; 50 | } 51 | } 52 | p=p->next; 53 | i++; 54 | } 55 | return listHead; 56 | } 57 | 58 | void printList(ListNode *phead) 59 | { 60 | while(phead) 61 | { 62 | printf("%c",phead->ch); 63 | if(phead->sibling) 64 | printf(":%c",phead->sibling->ch); 65 | putchar('\t'); 66 | phead=phead->next; 67 | } 68 | printf("\n"); 69 | } 70 | 71 | //第一步:在原始节点后面复制出新节点 72 | void cloneNodes(ListNode *phead) 73 | { 74 | ListNode *p=phead; 75 | ListNode *pclone=NULL; 76 | while(p!=NULL) 77 | { 78 | pclone=(ListNode *)malloc(sizeof(ListNode)); 79 | pclone->ch=p->ch; 80 | pclone->next=p->next; 81 | pclone->sibling=NULL; 82 | p->next=pclone; 83 | p=pclone->next; 84 | } 85 | } 86 | 87 | //第二步:给新节点设置sibling域 88 | void setSibling(ListNode *phead) 89 | { 90 | ListNode *p=phead; 91 | ListNode *pclone=NULL; 92 | while(p!=NULL) 93 | { 94 | pclone=p->next; 95 | if(p->sibling!=NULL) 96 | pclone->sibling=p->sibling->next; 97 | p=pclone->next; 98 | } 99 | } 100 | 101 | //第三步:让新节点脱离原始节点形成新链表 102 | ListNode *reconnectNodes(ListNode *phead) 103 | { 104 | ListNode *p=phead; 105 | ListNode *clonehead=NULL; 106 | ListNode *pclone=NULL; 107 | if(p!=NULL) 108 | { 109 | clonehead=pclone=p->next; 110 | p->next=pclone->next; 111 | p=p->next; 112 | } 113 | while(p!=NULL) 114 | { 115 | pclone->next=p->next; 116 | pclone=pclone->next; 117 | p->next=pclone->next; 118 | p=p->next; 119 | } 120 | return clonehead; 121 | } 122 | 123 | ListNode *cloneComplexNodes(ListNode *phead) 124 | { 125 | cloneNodes(phead); 126 | setSibling(phead); 127 | return reconnectNodes(phead); 128 | } 129 | 130 | int main() 131 | { 132 | //vaule依次存储链表节点的值 133 | char value[]={'a','b','c','d','e'}; 134 | //sibling存储每个节点的sibling节点位置坐标 135 | //例如,a的next是b,a的sibling指向位置2的节点c,sibling为NULL用-1表示 136 | int sibling[]= {2,4,-1,1,-1}; 137 | ListNode *phead=createList(value,sibling,5); 138 | ListNode *clonehead=NULL; 139 | printList(phead); 140 | clonehead=cloneComplexNodes(phead); 141 | printList(clonehead); 142 | return 0; 143 | } 144 | -------------------------------------------------------------------------------- /27-convertBST.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct tNode{ 5 | int data; 6 | struct tNode *lchild; 7 | struct tNode *rchild; 8 | }treeNode; 9 | 10 | //按照先序遍历的方式创建二叉树 11 | treeNode *createTree() 12 | { 13 | int key; 14 | treeNode *p=NULL; 15 | scanf("%d",&key); 16 | if(key!=-1) 17 | { 18 | p=(treeNode *)malloc(sizeof(treeNode)); 19 | p->data=key; 20 | p->lchild=createTree(); 21 | p->rchild=createTree(); 22 | } 23 | else 24 | return NULL; 25 | return p; 26 | } 27 | 28 | void convertNode(treeNode *pNode,treeNode **plastNodeInList) 29 | { 30 | if(pNode == NULL) 31 | return ; 32 | treeNode *pcurrent=pNode; 33 | if(pcurrent->lchild!=NULL) 34 | convertNode(pcurrent->lchild,plastNodeInList); 35 | pcurrent->lchild = *plastNodeInList; 36 | if(*plastNodeInList!=NULL) 37 | (*plastNodeInList)->rchild=pcurrent; 38 | *plastNodeInList=pcurrent; 39 | if(pcurrent->rchild!=NULL) 40 | convertNode(pcurrent->rchild,plastNodeInList); 41 | } 42 | 43 | treeNode *convert(treeNode *pRoot) 44 | { 45 | treeNode *plastNodeInList=NULL; 46 | convertNode(pRoot,&plastNodeInList); 47 | treeNode *listHead = plastNodeInList; 48 | while(listHead!=NULL && listHead->lchild!=NULL) 49 | listHead=listHead->lchild; 50 | return listHead; 51 | } 52 | 53 | void printList(treeNode *phead) 54 | { 55 | while(phead) 56 | { 57 | printf("%d\t",phead->data); 58 | phead=phead->rchild; 59 | } 60 | printf("\n"); 61 | } 62 | 63 | int main() 64 | { 65 | treeNode *pRoot=createTree(); 66 | treeNode *listHead=convert(pRoot); 67 | printList(listHead); 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /28-strPermutation.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define LENGTH 50 3 | 4 | void permutation(char *pstr,char *pbegin) 5 | { 6 | char *pch=NULL; 7 | char temp; 8 | if(pstr==NULL) 9 | return; 10 | if(*pbegin=='\0') 11 | printf("%s\n",pstr); 12 | else 13 | { 14 | for(pch=pbegin;*pch!='\0';++pch) 15 | { 16 | temp=*pch; 17 | *pch=*pbegin; 18 | *pbegin=temp; 19 | permutation(pstr,pbegin+1); 20 | temp=*pch; 21 | *pch=*pbegin; 22 | *pbegin=temp; 23 | } 24 | } 25 | } 26 | 27 | int main() 28 | { 29 | char str[LENGTH]; 30 | scanf("%s",str); 31 | permutation(str,str); 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /29-more_than_half.c: -------------------------------------------------------------------------------- 1 | #include 2 | //输入是否有效的全局标志 3 | int input_valid = 1; 4 | 5 | //检查某整数number是否出现次数超过一半 6 | int is_than_half(int *num,int length,int number) 7 | { 8 | int times=0,i=0; 9 | int than_half=0; 10 | for(i=0;ilength) 16 | than_half=1; 17 | else 18 | input_valid=0; 19 | } 20 | 21 | //找出可能超过一半的那个数,然后调用is_than_half 22 | //对该数确认 23 | int more_than_half(int *num,int length) 24 | { 25 | int result = num[0]; 26 | int i,times=1; 27 | if(num==NULL||length<=0) 28 | { 29 | input_valid=0; 30 | return 0; 31 | } 32 | 33 | for(i=0;i 2 | using namespace std; 3 | 4 | bool find(int *a,int rows,int columns,int number) 5 | { 6 | bool found = false; 7 | int row = 0,col = columns -1; 8 | while(row =0) 9 | { 10 | if(a[row*columns+col]>number) 11 | col--; 12 | else if(a[row*columns+col]== number) 13 | { 14 | found = true; 15 | break; 16 | } 17 | else 18 | row++; 19 | } 20 | return found; 21 | } 22 | 23 | int main() 24 | { 25 | int a[4][4] = { {1,2,8,9}, 26 | {2,4,9,12}, 27 | {4,7,10,13}, 28 | {6,8,11,15}, 29 | }; 30 | int number=9; 31 | bool found = find(&a[0][0],4,4,number); 32 | if(found) 33 | cout << number << " is in the matrix" << endl; 34 | else 35 | cout << number << " isn't in the matrix" << endl; 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /30-k_least_number.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void heap_adjust(int *a,int i,int n) 4 | { 5 | int child,temp; 6 | for(temp=a[i];2*i+1 < n;i=child) 7 | { 8 | child = 2*i +1; 9 | if(child!=n-1 && a[child+1]=0;i--) 31 | heap_adjust(a,i,n); 32 | 33 | //取走目前最小的a[0],继续把剩下数组调整成小顶堆 34 | //重复K次,则取走了最小的K个数 35 | for(i=n-1;i>=n-k;i--) 36 | { 37 | swap(a,0,i); 38 | printf("%d\t",a[i]); 39 | heap_adjust(a,0,i); 40 | } 41 | printf("\n"); 42 | } 43 | 44 | int main() 45 | { 46 | int a[]={1,2,39,77,88,50,100,200,712,21,9,10,20,30,44,33,22,17,18,39}; 47 | get_least_k(a,20,5); 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /31-greatest_sum_of_subarrays.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int invalid_input=0; 4 | int greatest_sub_array(int *a,int n) 5 | { 6 | if(a==NULL||n<=0) 7 | { 8 | invalid_input=1; 9 | return 0; 10 | } 11 | int current_sum=0; 12 | int greatest_sum=0x80000000; 13 | int i=0; 14 | for(i=0;i greatest_sum) 21 | greatest_sum = current_sum; 22 | } 23 | return greatest_sum; 24 | } 25 | 26 | int main() 27 | { 28 | int a[]={1,-2,3,10,-4,7,2,-5}; 29 | int greatest = greatest_sub_array(a,8); 30 | if(invalid_input==0) 31 | printf("greatest sum:%d\n",greatest); 32 | else 33 | printf("invalid input\n"); 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /32-numof1_recursive.c: -------------------------------------------------------------------------------- 1 | //时间复杂度O(logn) 2 | #include 3 | #include 4 | 5 | int power_of_10(unsigned int n) 6 | { 7 | int i=0; 8 | int result=1; 9 | while(i1) 30 | return 1; 31 | //number_of_1表示n的最高位1出现的次数 32 | int num_first_digits=0; 33 | if(first>1) 34 | num_first_digits = power_of_10(length-1); 35 | else if(first==1) 36 | num_first_digits = atoi(strN+1)+1; 37 | int num_other_digits=first * (length-1)*power_of_10(length-2); 38 | int num_recursive=number_of_1(atoi(strN+1)); 39 | return num_first_digits+num_other_digits+num_recursive; 40 | } 41 | 42 | int main() 43 | { 44 | unsigned int n; 45 | scanf("%d",&n); 46 | printf("%d\n",number_of_1(n)); 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /32-numof1_simple.c: -------------------------------------------------------------------------------- 1 | #include 2 | int numberof1(unsigned int n) 3 | { 4 | int number = 0; 5 | while(n) 6 | { 7 | if(n%10==1) 8 | number++; 9 | n=n/10; 10 | } 11 | return number; 12 | } 13 | 14 | int numberof1_from1toN(unsigned int n) 15 | { 16 | int number=0; 17 | int i; 18 | for(i=1;i<=n;i++) 19 | number+=numberof1(i); 20 | return number; 21 | } 22 | 23 | int main() 24 | { 25 | unsigned int n; 26 | scanf("%d",&n); 27 | printf("%d\n",numberof1_from1toN(n)); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /33-sort_as_min_number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | const int g_MaxNumberlength = 10; 6 | char *g_StrCombine1 = new char[g_MaxNumberlength*2 + 1]; 7 | char *g_StrCombine2 = new char[g_MaxNumberlength*2 + 1]; 8 | 9 | int compare(const void *strNumber1,const void *strNumber2) 10 | { 11 | strcpy(g_StrCombine1,*(const char **)strNumber1); 12 | strcat(g_StrCombine1,*(const char **)strNumber2); 13 | strcpy(g_StrCombine2,*(const char **)strNumber2); 14 | strcat(g_StrCombine2,*(const char **)strNumber1); 15 | return strcmp(g_StrCombine1,g_StrCombine2); 16 | } 17 | 18 | void PrintMinNumber(int *numbers,int length) 19 | { 20 | if(numbers==NULL||length<=0) 21 | return; 22 | char **strNumbers = (char **)(new int[length]); 23 | for(int i=0;i 2 | #include 3 | #include 4 | 5 | int min(int a,int b,int c) 6 | { 7 | int min=(a 2 | 3 | char first_no_repeat(char *pstr) 4 | { 5 | if(pstr==NULL) 6 | return '\0'; 7 | const int table_size=256; 8 | unsigned int hash_table[table_size]; 9 | unsigned int i; 10 | for(i=0;i 2 | 3 | int inverse_pair_core(int *data,int *copy,int start,int end) 4 | { 5 | if(start == end) 6 | { 7 | copy[start]=data[start]; 8 | return 0; 9 | } 10 | int length = (end-start)/2; 11 | int left = inverse_pair_core(copy,data,start,start+length); 12 | int right = inverse_pair_core(copy,data,start+length+1,end); 13 | int i=start+length; 14 | int j=end; 15 | int index_copy = end; 16 | int count=0; 17 | while(i>=start &&j>=start+length+1) 18 | { 19 | if(data[i]>data[j]) 20 | { 21 | copy[index_copy--]=data[i--]; 22 | count+=j-start-length; 23 | } 24 | else 25 | copy[index_copy--]=data[j--]; 26 | } 27 | for(;i>=start;--i) 28 | copy[index_copy--]=data[i]; 29 | for(;j>=start+length+1;--j) 30 | copy[index_copy--]=data[j]; 31 | return left+right+count; 32 | } 33 | 34 | 35 | int inverse_pairs(int *data,int length) 36 | { 37 | if(data==NULL||length<0) 38 | return 0; 39 | int *copy = new int[length]; 40 | for(int i=0;i 2 | typedef struct node{ 3 | int key; 4 | struct node *next; 5 | }ListNode; 6 | 7 | unsigned int get_list_length(ListNode *pHead) 8 | { 9 | unsigned int length=0; 10 | ListNode *pNode=pHead; 11 | while(pNode!=NULL) 12 | { 13 | length++; 14 | pNode=pNode->next; 15 | } 16 | return length; 17 | } 18 | 19 | ListNode *first_common_node(ListNode *pHead1,ListNode *pHead2) 20 | { 21 | unsigned int length1=get_list_length(pHead1); 22 | unsigned int length2=get_list_length(pHead2); 23 | int length_diff=length1-length2; 24 | ListNode *phead_long = pHead1; 25 | ListNode *phead_short = pHead2; 26 | if(length2>length1) 27 | { 28 | phead_long = pHead2; 29 | phead_short= pHead1; 30 | length_diff = length2-length1; 31 | } 32 | int i=0; 33 | for(i=0;inext; 35 | while(phead_long && phead_short &&(phead_long!=phead_short)) 36 | { 37 | phead_long = phead_long->next; 38 | phead_short = phead_short->next; 39 | } 40 | ListNode *firstcommon=phead_long; 41 | return firstcommon; 42 | } 43 | 44 | //以上代码是函数实现,main里没有举例验证 45 | int main() 46 | { 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /38-number_of_k.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int get_firstK(int *data,int length,int k,int start,int end) 4 | { 5 | if(start>end) 6 | return -1; 7 | int mid_index=(start+end)/2; 8 | int mid_data=data[mid_index]; 9 | if(mid_data==k) 10 | { 11 | if((mid_index>0 && data[mid_index-1]!=k)||mid_index==0) 12 | return mid_index; 13 | else 14 | end=mid_index-1; 15 | } 16 | else if(mid_data>k) 17 | end=mid_index-1; 18 | else 19 | start = mid_index+1; 20 | return get_firstK(data,length,k,start,end); 21 | } 22 | int get_lastK(int *data,int length,int k,int start,int end) 23 | { 24 | if(start>end) 25 | return -1; 26 | int mid_index=(start+end)/2; 27 | int mid_data=data[mid_index]; 28 | if(mid_data==k) 29 | { 30 | if((mid_index0) 46 | { 47 | int first = get_firstK(data,length,k,0,length-1); 48 | int last = get_lastK(data,length,k,0,length-1); 49 | if(first>-1 &&last>-1) 50 | number = last-first+1; 51 | } 52 | return number; 53 | } 54 | int main() 55 | { 56 | int data[]={1,2,3,3,3,3,4,5}; 57 | printf("count:%d\n",get_number_of_k(data,8,3)); 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /39-is_balanced_tree.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef struct tNode{ 4 | int data; 5 | struct tNode *lchild; 6 | struct tNode *rchild; 7 | }treeNode; 8 | 9 | int is_balanced(treeNode *proot,int *p_depth) 10 | { 11 | if(proot==NULL) 12 | { 13 | *p_depth=0; 14 | return 1; 15 | } 16 | int left,right; 17 | if(is_balanced(proot->lchild,&left)&&is_balanced(proot->rchild,&right)) 18 | { 19 | int diff = left-right; 20 | if(diff<=1 &&diff>=-1) 21 | { 22 | *p_depth=1+(left>right?left:right); 23 | return 1; 24 | } 25 | } 26 | return 0; 27 | } 28 | 29 | //按照先序遍历的方式创建二叉树 30 | treeNode *createTree() 31 | { 32 | int key; 33 | treeNode *p=NULL; 34 | scanf("%d",&key); 35 | if(key!=-1) 36 | { 37 | p=(treeNode *)malloc(sizeof(treeNode)); 38 | p->data=key; 39 | p->lchild=createTree(); 40 | p->rchild=createTree(); 41 | } 42 | else 43 | return NULL; 44 | return p; 45 | } 46 | 47 | int main() 48 | { 49 | treeNode *proot=createTree(); 50 | int depth=0; 51 | if(is_balanced(proot,&depth)) 52 | { 53 | printf("is balanced.\n"); 54 | printf("depth:%d\n",depth); 55 | } 56 | //如果不是平衡的,则在判断到不平衡的子树时就退出了,不会求出原树的深度 57 | //此时depth的值应该是初始值0 58 | else 59 | printf("not balanced.\n"); 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /39-tree_depth.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef struct tNode{ 4 | int data; 5 | struct tNode *lchild; 6 | struct tNode *rchild; 7 | }treeNode; 8 | 9 | int tree_depth(treeNode *proot) 10 | { 11 | if(proot==NULL) 12 | return 0; 13 | int left_depth=tree_depth(proot->lchild); 14 | int right_depth=tree_depth(proot->rchild); 15 | return (left_depth>right_depth)?(left_depth+1):(right_depth+1); 16 | } 17 | 18 | //按照先序遍历的方式创建二叉树 19 | treeNode *createTree() 20 | { 21 | int key; 22 | treeNode *p=NULL; 23 | scanf("%d",&key); 24 | if(key!=-1) 25 | { 26 | p=(treeNode *)malloc(sizeof(treeNode)); 27 | p->data=key; 28 | p->lchild=createTree(); 29 | p->rchild=createTree(); 30 | } 31 | else 32 | return NULL; 33 | return p; 34 | } 35 | 36 | int main() 37 | { 38 | treeNode *proot=createTree(); 39 | printf("depth:%d\n",tree_depth(proot)); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /4-replaceblank.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAXLEN 1024 3 | void replaceBlank(char *str,int length) 4 | { 5 | int len1=0,len2=0,space_num=0; 6 | while(str[len1]!='\0') 7 | { 8 | if(str[len1]==' ') 9 | space_num++; 10 | len1++; 11 | } 12 | len2=len1+space_num*2; 13 | if(len2>length) 14 | return; 15 | len1--; 16 | len2--; 17 | while(len1>=0) 18 | { 19 | if(str[len1] ==' ') 20 | { 21 | str[len2--]='0'; 22 | str[len2--]='2'; 23 | str[len2--]='%'; 24 | } 25 | else 26 | str[len2--]=str[len1]; 27 | len1--; 28 | } 29 | printf("%s\n",str); 30 | } 31 | 32 | int main() 33 | { 34 | char c,str[MAXLEN]; 35 | int i=0; 36 | while((c=getchar())!=EOF && c!='\n') 37 | str[i++]=c; 38 | str[i]='\0'; 39 | replaceBlank(str,MAXLEN); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /40-appear_once_num.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int is_bit1(int num,unsigned int index) 4 | { 5 | num=num>>index; 6 | return (num &1); 7 | } 8 | 9 | unsigned int find_first_bit1(int num) 10 | { 11 | int index=0; 12 | while((num&1)==0 && index <=8*sizeof(int)) 13 | { 14 | num>>=1; 15 | index++; 16 | } 17 | return index; 18 | } 19 | 20 | void num_appear_once(int *data,int length ,int *num1,int *num2) 21 | { 22 | if(data==NULL||length<2) 23 | return; 24 | int result_after_XOR=0; 25 | int i=0,j=0; 26 | for(;i 2 | 3 | void print_sequence(int small,int big) 4 | { 5 | int i=0; 6 | for(i=small;i<=big;i++) 7 | printf("%d ",i); 8 | printf("\n"); 9 | } 10 | 11 | void find_sequence(int sum) 12 | { 13 | if(sum<3) 14 | return; 15 | int small=1; 16 | int big=2; 17 | int middle=(1+sum)/2; 18 | int cur_sum=small+big; 19 | while(small 2 | 3 | int find_num_withsum(int *data,int length,int sum,int *num1,int *num2) 4 | { 5 | int found=0; 6 | if(length<1||num1==NULL||num2==NULL) 7 | return found; 8 | int big=length-1; 9 | int small=0; 10 | while(big>small) 11 | { 12 | int cur_sum = data[small]+data[big]; 13 | if(cur_sum == sum) 14 | { 15 | *num1 = data[small]; 16 | *num2 = data[big]; 17 | found = 1; 18 | break; 19 | } 20 | else if(cur_sum>sum) 21 | big--; 22 | else 23 | small++; 24 | } 25 | return found; 26 | } 27 | 28 | int main() 29 | { 30 | int data[]={1,2,4,7,11,15}; 31 | int number1,number2; 32 | if(find_num_withsum(data,6,15,&number1,&number2)) 33 | printf("%d+%d\n",number1,number2); 34 | else 35 | printf("not found\n"); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /42-reverse_words.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void reverse(char *pbegin,char *pend) 4 | { 5 | char temp; 6 | if(pbegin==NULL||pend==NULL) 7 | return; 8 | while(pbegin 2 | #include 3 | 4 | void reverse(char *pbegin,char *pend) 5 | { 6 | char temp; 7 | if(pbegin==NULL||pend==NULL) 8 | return; 9 | while(pbeginlength||pdata==NULL) 23 | return ; 24 | char *pbegin=pdata; 25 | char * pend=pdata+n-1; 26 | reverse(pbegin,pend); 27 | pbegin=pend+1; 28 | pend=pdata+length-1; 29 | reverse(pbegin,pend); 30 | reverse(pdata,pdata+length-1); 31 | } 32 | 33 | int main() 34 | { 35 | char sentence[256]; 36 | char *result=NULL; 37 | // fgets(sentence,256,stdin); 38 | scanf("%[^\n]",sentence); 39 | int length=strlen(sentence); 40 | int n; 41 | scanf("%d",&n); 42 | rotate_left(sentence,length,n); 43 | printf("%s\n",sentence); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /43-dices_probability.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define MAXVALUE 6 5 | void probability(int number) 6 | { 7 | if(number<1) 8 | return; 9 | int *prob[2]; 10 | prob[0]=(int *)malloc(sizeof(int)*MAXVALUE*number+1); 11 | prob[1]=(int *)malloc(sizeof(int)*MAXVALUE*number+1); 12 | int i=0; 13 | for(i=0;i 2 | #define CARD_NUM 5 3 | 4 | int compare(void *arg1,void *arg2) 5 | { 6 | return *(int *)arg1 - *(int *)arg2; 7 | } 8 | 9 | int is_continuous(int *number,int length) 10 | { 11 | if(number==NULL||length<1) 12 | return 0; 13 | qsort(number,length,sizeof(int),compare); 14 | int num_zero = 0; 15 | int num_gap = 0; 16 | int i=0; 17 | for(i=0;inum_zero)?0:1; 30 | } 31 | 32 | int main(int argc,char *argv[]) 33 | { 34 | int cards[CARD_NUM]; 35 | int i=0; 36 | while(i 2 | #include 3 | using namespace::std; 4 | 5 | int lastRemaining(unsigned int n,unsigned int m) 6 | { 7 | if(n<1||m<1) 8 | return -1; 9 | unsigned int i=0; 10 | list numbers; 11 | for(i=0;i::iterator current = numbers.begin(); 16 | while(numbers.size()>1) 17 | { 18 | for(int i=1;i::iterator next = ++current; 25 | if(next==numbers.end()) 26 | next=numbers.begin(); 27 | --current; 28 | numbers.erase(current); 29 | current=next; 30 | } 31 | return *current; 32 | } 33 | 34 | int main() 35 | { 36 | cout< 2 | typedef unsigned int (*fun)(unsigned int); 3 | 4 | unsigned int terminator(unsigned int n) 5 | { 6 | return 0; 7 | } 8 | 9 | unsigned int sum(unsigned int n) 10 | { 11 | static fun f[2]={terminator,sum}; 12 | return n+f[!!n](n-1); 13 | } 14 | 15 | int main() 16 | { 17 | int n; 18 | scanf("%d",&n); 19 | printf("%d\n",sum(n)); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /47-add_two_num.c: -------------------------------------------------------------------------------- 1 | #include 2 | int add(int num1,int num2) 3 | { 4 | int sum,carry; 5 | do 6 | { 7 | sum = num1^num2; 8 | carry = (num1&num2)<<1; 9 | num1=sum; 10 | num2=carry; 11 | }while(num2!=0); 12 | return num1; 13 | } 14 | 15 | int main() 16 | { 17 | int num1,num2; 18 | scanf("%d %d",&num1,&num2); 19 | printf("(%d)+(%d)= %d\n",num1,num2,add(num1,num2)); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /49-string_to_int.c: -------------------------------------------------------------------------------- 1 | #include 2 | enum status{ 3 | kValid=0, 4 | kInvalid 5 | }; 6 | 7 | int g_nstatus=kValid; 8 | long long str_to_int_core(const char *digit ,int minus) 9 | { 10 | long long num = 0; 11 | while(*digit!='\0') 12 | { 13 | if(*digit>='0' && *digit<='9') 14 | { 15 | int flag = minus?-1:1; 16 | num = num*10 + flag*(*digit - '0'); 17 | if((!minus&& num>0x7FFFFFFF)|| 18 | (minus && num<(signed int )0x80000000)) 19 | { 20 | num = 0; 21 | break; 22 | } 23 | digit++; 24 | } 25 | else 26 | { 27 | num=0; 28 | break; 29 | } 30 | } 31 | if(*digit=='\0') 32 | g_nstatus = kValid; 33 | return num; 34 | } 35 | 36 | int str_to_int(const char *str) 37 | { 38 | long long num=0; 39 | g_nstatus=kInvalid; 40 | if(str!=NULL &&*str!='\0') 41 | { 42 | int minus = 0; 43 | if(*str=='+') 44 | str++; 45 | else if(*str=='-') 46 | { 47 | str++; 48 | minus=1; 49 | } 50 | if(*str!='\0') 51 | num = str_to_int_core(str,minus); 52 | } 53 | return (int)num; 54 | } 55 | 56 | 57 | int main() 58 | { 59 | char str[20]; 60 | scanf("%s",str); 61 | int result_num = str_to_int(str); 62 | if(g_nstatus == kValid) 63 | printf("result_num:%d\n",result_num); 64 | else 65 | printf("invalid input.\n"); 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /5-printListReversingly.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef struct node{ 4 | int key; 5 | struct node *next; 6 | }ListNode; 7 | 8 | ListNode *createList() 9 | { 10 | int key; 11 | ListNode *p=NULL; 12 | ListNode *pHead=NULL, *tail=NULL; 13 | scanf("%d",&key); 14 | while(key!=-1) 15 | { 16 | p=(ListNode *)malloc(sizeof(ListNode)); 17 | p->key=key; 18 | p->next=NULL; 19 | if(pHead == NULL) 20 | { 21 | pHead=p; 22 | tail=p; 23 | } 24 | else 25 | { 26 | tail->next=p; 27 | tail=tail->next; 28 | } 29 | scanf("%d",&key); 30 | } 31 | return pHead; 32 | } 33 | 34 | void printlist_reverse(ListNode *pHead) 35 | { 36 | if(pHead) 37 | { 38 | if(pHead->next) 39 | printlist_reverse(pHead->next); 40 | printf("%d\n",pHead->key); 41 | } 42 | } 43 | 44 | int main() 45 | { 46 | ListNode *pHead=NULL; 47 | pHead=createList(); 48 | printlist_reverse(pHead); 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /50-lowest_common_parent.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct tNode{ 5 | int data; 6 | struct tNode *lchild; 7 | struct tNode *rchild; 8 | struct tNode *parent; 9 | }treeNode; 10 | 11 | treeNode *pnode1=NULL; 12 | treeNode *pnode2=NULL; 13 | 14 | //按照先序遍历的方式创建二叉树 15 | treeNode *createTree(treeNode *parent) 16 | { 17 | int key; 18 | treeNode *p=NULL; 19 | scanf("%d",&key); 20 | if(key!=-1) 21 | { 22 | p=(treeNode *)malloc(sizeof(treeNode)); 23 | p->data=key; 24 | p->parent=parent; 25 | parent=p; 26 | p->lchild=createTree(parent); 27 | p->rchild=createTree(parent); 28 | } 29 | else 30 | return NULL; 31 | return p; 32 | } 33 | 34 | //二叉树的先序遍历递归方法 35 | void PreOrderTraverse(treeNode *T,int node1,int node2) 36 | { 37 | if(!T) 38 | return; 39 | if(T->data==node1) 40 | pnode1=T; 41 | if(T->data==node2) 42 | pnode2=T; 43 | PreOrderTraverse(T->lchild,node1,node2); 44 | PreOrderTraverse(T->rchild,node1,node2); 45 | } 46 | 47 | unsigned int get_list_length(treeNode *pHead) 48 | { 49 | unsigned int length=0; 50 | treeNode *pNode=pHead; 51 | while(pNode!=NULL) 52 | { 53 | length++; 54 | pNode=pNode->parent; 55 | } 56 | return length; 57 | } 58 | 59 | treeNode *first_common_node(treeNode *pnode1,treeNode *pnode2) 60 | { 61 | unsigned int length1=get_list_length(pnode1); 62 | unsigned int length2=get_list_length(pnode2); 63 | int length_diff=length1-length2; 64 | treeNode *phead_long = pnode1; 65 | treeNode *phead_short = pnode2; 66 | if(length2>length1) 67 | { 68 | phead_long = pnode2; 69 | phead_short= pnode1; 70 | length_diff = length2-length1; 71 | } 72 | int i=0; 73 | for(i=0;iparent; 75 | while(phead_long && phead_short &&(phead_long!=phead_short)) 76 | { 77 | phead_long = phead_long->parent; 78 | phead_short = phead_short->parent; 79 | } 80 | treeNode *firstcommon=phead_long; 81 | return firstcommon; 82 | } 83 | 84 | int main(int argc,char *argv[]) 85 | { 86 | //第一步:创建带父节点的二叉树 87 | treeNode *pRoot=createTree(NULL); 88 | treeNode *lowest_parent=NULL; 89 | int node1,node2; 90 | printf("input two nodes:"); 91 | scanf("%d %d",&node1,&node2); 92 | printf("node1:%d,node2:%d\n",node1,node2); 93 | //第二步:获得待查找的两个节点指针 94 | PreOrderTraverse(pRoot,node1,node2); 95 | //第三步:查找两个节点在链表中的第一个公共节点 96 | lowest_parent=first_common_node(pnode1,pnode2); 97 | if(lowest_parent) 98 | printf("lowest parent:%d\n",lowest_parent->data); 99 | else 100 | printf("not exist\n"); 101 | return 0; 102 | } 103 | -------------------------------------------------------------------------------- /6-buildBinarytree.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef struct tNode{ 4 | int data; 5 | struct tNode *lchild; 6 | struct tNode *rchild; 7 | }treeNode; 8 | 9 | //先序序列和中序序列的长度必须相同,为orderLen 10 | treeNode *buildNode(int *preOrder,int *inOrder,int orderLen) 11 | { 12 | if(preOrder == NULL||inOrder == NULL||orderLen==0) 13 | return NULL; 14 | //inParent是当前parent在中序里的下标 15 | int inParent=0; 16 | treeNode *parent=(treeNode *)malloc(sizeof(treeNode)); 17 | parent->data = preOrder[0]; 18 | parent->lchild = NULL; 19 | parent->rchild = NULL; 20 | if(orderLen==1) 21 | return parent; 22 | while(inParentdata) 25 | inParent ++; 26 | else 27 | break; 28 | } 29 | if(inParent==orderLen && inOrder[inParent]!=parent->data) 30 | { 31 | printf("Invalid Order list.\n"); 32 | exit(1); 33 | } 34 | if(inParent > 0) 35 | { 36 | parent->lchild = buildNode(preOrder+1,inOrder,inParent); 37 | } 38 | if(inParent+1 < orderLen) 39 | { 40 | parent->rchild = buildNode(preOrder+inParent+1,inOrder+inParent+1,orderLen-inParent-1); 41 | } 42 | return parent; 43 | } 44 | 45 | //递归输出后序序列 46 | void postOrder(treeNode *root) 47 | { 48 | if(root) 49 | { 50 | postOrder(root->lchild); 51 | postOrder(root->rchild); 52 | printf("%d ",root->data); 53 | } 54 | } 55 | 56 | //释放掉整个二叉树 57 | void freeTree(treeNode **pT) 58 | { 59 | if(*pT != NULL) 60 | { 61 | freeTree(&((*pT)->lchild)); 62 | freeTree(&((*pT)->rchild)); 63 | free(*pT); 64 | *pT=NULL; 65 | } 66 | return; 67 | } 68 | 69 | int main() 70 | { 71 | int preOrder[] = {1,2,4,7,3,5,6,8}; 72 | int inOrder[] = {4,7,2,1,5,3,6,8}; 73 | treeNode *root = buildNode(preOrder,inOrder,8); 74 | postOrder(root); 75 | printf("\n"); 76 | freeTree(&root); 77 | } 78 | -------------------------------------------------------------------------------- /7-stackQueue.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #define MAX 10001 6 | int stack1[MAX]; 7 | int stack2[MAX]; 8 | int main() 9 | { 10 | char operate[5]; 11 | int top1=0,top2=0,i=0,n=0; 12 | int num; 13 | //for(;i<40;i++) 14 | // printf("%d\t",stack1[i]); 15 | scanf("%d",&n); 16 | while(i 2 | //使用二分查找的方式,使得时间复杂度为O(logn) 3 | int minInOrder(int *num,int index1,int index2) 4 | { 5 | int result=num[index1]; 6 | int i=0; 7 | while(i<=index2) 8 | { 9 | if(result>num[i]) 10 | result=num[i]; 11 | i++; 12 | } 13 | return result; 14 | } 15 | 16 | int min(int *num,int length) 17 | { 18 | if(num==NULL||length<=0) 19 | return -1; 20 | int index1=0,index2=length-1; 21 | int indexMid=index1; 22 | while(num[index1]>=num[index2]) 23 | { 24 | if(index2-index1 == 1) 25 | { 26 | indexMid = index2; 27 | break; 28 | } 29 | indexMid=(index1+index2)/2; 30 | if(num[indexMid]==num[index1] && num[index1]==num[index2]) 31 | return minInOrder(num,index1,index2); 32 | if(num[indexMid]>num[index1]) 33 | index1=indexMid; 34 | else if(num[indexMid] 2 | long long fibonacci(unsigned n) 3 | { 4 | int i=0; 5 | long long fibOne,fibTwo,fibThree; 6 | if(n==0) 7 | return 0; 8 | if(n==1) 9 | return 1; 10 | fibOne=0; 11 | fibTwo=1; 12 | for(i=2;i<=n;i++) 13 | { 14 | fibThree = fibOne+fibTwo; 15 | fibOne=fibTwo; 16 | fibTwo=fibThree; 17 | } 18 | return fibThree; 19 | 20 | } 21 | int main() 22 | { 23 | int n; 24 | scanf("%d",&n); 25 | printf("%lld\n",fibonacci(n)); 26 | return 0; 27 | } 28 | 29 | 30 | -------------------------------------------------------------------------------- /9-frogjump2.c: -------------------------------------------------------------------------------- 1 | //青蛙一次可以跳1级或者2级台阶,那么N级台阶有几种跳法 2 | #include 3 | long long frogjump(unsigned n) 4 | { 5 | if(n<=1) 6 | return 1; 7 | return frogjump(n-1)+frogjump(n-2); 8 | } 9 | 10 | int main() 11 | { 12 | int n; 13 | scanf("%d",&n); 14 | printf("%lld\n",frogjump(n)); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /9-frogjumpN.c: -------------------------------------------------------------------------------- 1 | //青蛙跳台阶,每次可跳任意步 2 | #include 3 | long long frogjump(unsigned n) 4 | { 5 | int i=0; 6 | long long result=0; 7 | if(n<=1) 8 | return 1; 9 | for(i=0;i 2 | #include 3 | #include 4 | 5 | using namespace::std; 6 | 7 | int fib(int n) 8 | { 9 | int fib_n=1; 10 | for(int i=2;i<=n;i++) 11 | fib_n*=i; 12 | return fib_n; 13 | } 14 | 15 | 16 | 17 | //Time Limit Exceeded. 18 | string getPermutation(int n,int k) 19 | { 20 | string result; 21 | if(k>fib(n)) return result; 22 | for(int i=0;ifib(n)) return s; 36 | for(int i=0;ik) 49 | break; 50 | int cur=s.size()-1-i; 51 | while(k) 52 | { 53 | int res=k/fib(i); 54 | if(k%=fib(i)) 55 | { 56 | char temp=s[cur+res]; 57 | s.erase(cur+res,1); 58 | s.insert(s.begin()+cur,temp); 59 | cur++; 60 | } 61 | else 62 | { 63 | char temp=s[cur+res-1]; 64 | s.erase(cur+res-1,1); 65 | s.insert(s.begin()+cur,temp); 66 | cur++; 67 | reverse(s.begin()+cur,s.end()); 68 | } 69 | i--; 70 | } 71 | return s; 72 | } 73 | int main() 74 | { 75 | int n,k; 76 | cin>>n>>k; 77 | //cout<'9'||str[i]<'0') 22 | { 23 | i++; 24 | break; 25 | } 26 | else 27 | { 28 | if(result>INT_MAX/10||(result==INT_MAX/10 && (str[i]-'0')>INT_MAX%10)) 29 | return flag==1?INT_MAX:INT_MIN; 30 | result=result*10+str[i]-'0'; 31 | i++; 32 | } 33 | } 34 | return (result*flag); 35 | } 36 | }; 37 | -------------------------------------------------------------------------------- /count_and_say.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace::std; 4 | 5 | string countAndSay(int n) 6 | { 7 | string result[2]; 8 | int cur=0; 9 | result[cur]="1"; 10 | for(int count=1;count>n; 39 | countAndSay(n); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /decodeWays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace::std; 4 | int decodeWays(string &s) 5 | { 6 | if(s.empty()||s[0]=='0') 7 | return 0; 8 | int i=0; 9 | vector f(s.size(),0); 10 | //f[i]表示s到下标为i时解码的种数 11 | f[0]=1; 12 | if((s[0]=='1' && s[1]>'0')||(s[0]=='2' && s[1]<='6'&& s[1]>'0')) 13 | f[1]=2; 14 | else if(s[0]>='3' && s[1]=='0') 15 | return 0; 16 | else 17 | f[1]=1; 18 | for(i=2;i='3') 23 | return 0; 24 | f[i]=f[i-2]; 25 | } 26 | else if(s[i-1]=='1'||(s[i-1]=='2' && s[i]<='6')) 27 | f[i]=f[i-1]+f[i-2]; 28 | else 29 | f[i]=f[i-1]; 30 | } 31 | return f[s.size()-1]; 32 | } 33 | 34 | int main() 35 | { 36 | string s; 37 | cin>>s; 38 | cout< 2 | #include 3 | using namespace::std; 4 | 5 | int minDistance(string word1,string word2) 6 | { 7 | const int m=word1.size(); 8 | const int n=word2.size(); 9 | if(m==0) 10 | return n; 11 | if(n==0) 12 | return m; 13 | int f[m+1][n+1]; 14 | for(int i=0;i<=m;i++) 15 | f[i][0]=i; 16 | for(int j=0;j<=n;j++) 17 | f[0][j]=j; 18 | for(int i=1;i<=m;i++) 19 | { 20 | for(int j=1;j<=n;j++) 21 | { 22 | if(word1[i-1]==word2[j-1]) 23 | f[i][j]=f[i-1][j-1]; 24 | else 25 | f[i][j]=min(f[i-1][j-1]+1,min(f[i-1][j]+1,f[i][j-1]+1)); 26 | } 27 | } 28 | return f[m][n]; 29 | } 30 | 31 | int main() 32 | { 33 | string a,b; 34 | cin>>a>>b; 35 | cout< 2 | #include 3 | #include 4 | using namespace::std; 5 | 6 | void generate_parenthesis(int n,int left,int right,string &str,vector &result) 7 | { 8 | 9 | if(n==left && n==right) 10 | result.push_back(str); 11 | if(left result; 28 | string str; 29 | int n; 30 | cin>>n; 31 | generate_parenthesis(n,0,0,str,result); 32 | for(int i=0;i 23 | #include 24 | 25 | int find_longest(char *ptr) 26 | { 27 | int cur_longest=0; 28 | int length=0; 29 | char *hash[150]; 30 | int i=0; 31 | memset(hash,0,sizeof hash); 32 | while(*ptr!='\0') 33 | { 34 | if(hash[*ptr]==0) 35 | { 36 | hash[*ptr++]=ptr; 37 | length++; 38 | } 39 | else 40 | { 41 | if(length>cur_longest) 42 | cur_longest=length; 43 | ptr=hash[*ptr]+1; 44 | memset(hash,0,sizeof hash); 45 | length=0; 46 | } 47 | } 48 | if(length>cur_longest) 49 | cur_longest=length; 50 | return cur_longest; 51 | } 52 | 53 | int main() 54 | { 55 | char buf[10001]; 56 | while(scanf("%s",buf)!=EOF) 57 | { 58 | printf("%d\n",find_longest(buf)); 59 | } 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /jobdu1531-cash_value.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int compare(void *arg1,void *arg2) 5 | { 6 | return *(int *)arg1 - *(int *)arg2; 7 | } 8 | 9 | int main() 10 | { 11 | int N=0; 12 | int value[100]; 13 | int total=0; 14 | int i=0; 15 | while(scanf("%d",&N)!=EOF) 16 | { 17 | for(i=0;itotal+1) 24 | break; 25 | else 26 | total+=value[i]; 27 | } 28 | printf("%d\n",total+1); 29 | } 30 | return 0; 31 | } 32 | //动态规划思想:对于1~i的面值之和total,如果第i+1个数大于total+1则不会组成total+1的面值. 33 | -------------------------------------------------------------------------------- /linkedListCycle.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode (int x):val(x),next(NULL){} 7 | * }; 8 | */ 9 | 10 | class Solution { 11 | public: 12 | ListNode *detectCycle(ListNode *head) { 13 | if(head==NULL||head->next==NULL) 14 | return NULL; 15 | ListNode *fast=head; 16 | ListNode *slow=head; 17 | ListNode *meet=NULL; 18 | ListNode *cycle_start=NULL; 19 | bool hasCycle=false; 20 | while(fast!=NULL && fast->next!=NULL) 21 | { 22 | fast=fast->next; 23 | fast=fast->next; 24 | slow=slow->next; 25 | if(slow==fast) 26 | { 27 | meet=slow; 28 | hasCycle=true; 29 | break; 30 | } 31 | } 32 | if(has==false) 33 | return NULL; 34 | slow=head; 35 | while(slow!=meet) 36 | { 37 | slow=slow->next; 38 | meet=meet->next; 39 | } 40 | cycle_start=slow; 41 | return cycle_start; 42 | } 43 | }; 44 | -------------------------------------------------------------------------------- /longest_common_substr.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define MAXLEN 1000 5 | int longest_common_substr(char *str1,char *str2) 6 | { 7 | int len1=strlen(str1); 8 | int len2=strlen(str2); 9 | int **c=NULL; 10 | int i=0,j=0,k=0,max=-1; 11 | int x,y; 12 | char s[MAXLEN]; 13 | c=(int**)malloc(sizeof(int*)*(len1+1)); 14 | for(i=0;i<=len1;i++) 15 | c[i]=(int *)malloc(sizeof(int)*(len2+1)); 16 | for(i=0;i<=len1;i++) 17 | c[i][0]=0; 18 | for(j=0;jmax) 29 | { 30 | max=c[i][j]; 31 | x=i; 32 | y=j; 33 | } 34 | } 35 | } 36 | //输出公共子串 37 | k=max; 38 | i=x-1; 39 | j=y-1; 40 | s[k--]='\0'; 41 | while(i>=0&&j>=0) 42 | { 43 | if(str1[i]==str2[j]) 44 | { 45 | s[k--]=str1[i]; 46 | i--; 47 | j--; 48 | } 49 | 50 | else 51 | break; 52 | } 53 | fputs(s,stdout); 54 | for(i=0;i<=len1;i++) 55 | free(c[i]); 56 | free(c); 57 | return max; 58 | } 59 | 60 | int main() 61 | { 62 | char str1[MAXLEN]; 63 | char str2[MAXLEN]; 64 | scanf("%s%s",str1,str2); 65 | int common_len=longest_common_substr(str1,str2); 66 | printf("\ncommon_length:%d\n",common_len); 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /minStack.cpp: -------------------------------------------------------------------------------- 1 | class MinStack { 2 | private: 3 | stack min; 4 | stack data; 5 | public: 6 | void push(int x) 7 | { 8 | data.push(x); 9 | if(min.empty()||x<=min.top()) 10 | { 11 | min.push(x); 12 | } 13 | 14 | } 15 | void pop() 16 | { 17 | if(data.empty()) 18 | return; 19 | if(data.top()==min.top()) 20 | { 21 | data.pop(); 22 | min.pop(); 23 | } 24 | else 25 | data.pop(); 26 | } 27 | 28 | int top() 29 | { 30 | return data.top(); 31 | } 32 | 33 | int getMin() 34 | { 35 | return min.top(); 36 | } 37 | }; 38 | -------------------------------------------------------------------------------- /nextPermutation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace::std; 5 | 6 | void printNum(vector num) 7 | { 8 | for(int i=0;i &num) 14 | { 15 | if(num.size()==0||num.size()==1) 16 | return; 17 | vector::iterator iter1=num.end()-1; 18 | vector::iterator iter2=num.end()-2; 19 | while(*iter2>=*iter1 && iter2>=num.begin()) 20 | { 21 | iter2--; 22 | iter1--; 23 | } 24 | if(*iter2>=*iter1) 25 | { 26 | reverse(num.begin(),num.end()); 27 | return; 28 | } 29 | vector::iterator iter3=num.end()-1; 30 | while(*iter2>=*iter3) 31 | { 32 | if(iter3==num.begin()) 33 | iter3=num.end()-1; 34 | else 35 | iter3--; 36 | } 37 | swap(*iter2,*iter3); 38 | reverse(iter1,num.end()); 39 | } 40 | int main() 41 | { 42 | vector num; 43 | int temp; 44 | while(cin>>temp) 45 | num.push_back(temp); 46 | nextPermutation(num); 47 | printNum(num); 48 | //C++自带的库函数使用如下: 49 | next_permutation(num.begin(),num.end()); 50 | printNum(num); 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /validPalindrome.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | bool isPalindrome(string s) 5 | { 6 | if(s.empty()) 7 | return true; 8 | string::size_type idx1=0; 9 | string::size_type idx2=s.size()-1; 10 | while(idx1