![](https://img-blog.csdnimg.cn/20201014180756757.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
AlgorithmDesign
L_Aster
..
展开
-
查找一个数组中第k小的数
时间复杂度O(n)#include <stdio.h>int SearchKth(int a[],int L,int R,int k){ int i=L,j=R; int pivot=a[i]; while(i<j) { while(i<j&&a[j]>=pivot) --j; a[i]=a[j]; while(i<原创 2017-05-28 15:51:05 · 789 阅读 · 0 评论 -
34. Search for a Range
34. Search for a Rangeint findLeft(int *nums,int s,int e,int target){ int i=s,j=e,m=0,idx=-1; while(i<=j) { m=(i+j)/2; if(nums[m]<target) i=m+1; else j=m-1;原创 2017-08-14 13:59:32 · 214 阅读 · 0 评论 -
移去链表倒数第n个节点
19. Remove Nth Node From End of Liststruct ListNode* removeNthFromEnd(struct ListNode* head, int n) { struct ListNode *L=(struct ListNode *)malloc(sizeof(struct ListNode)); L->next=head; st原创 2017-08-07 11:17:10 · 334 阅读 · 0 评论 -
合并两个有序链表,递归实现
21. Merge Two Sorted Lists 注:代码来自leetcodestruct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) { if(l1==NULL) return l2; if(l2==NULL) return l1; if(l1->val<l2->val)原创 2017-08-07 12:00:43 · 488 阅读 · 0 评论 -
将有序单链表转化为平衡二叉树
109. Convert Sorted List to Binary Search Treestruct TreeNode* constr(struct ListNode* head,struct ListNode* tail){ if(head==tail) return NULL; struct ListNode *mid=head,*temp=head; while(原创 2017-08-14 17:07:47 · 1330 阅读 · 0 评论 -
一些有趣的算法题收集-Category
目录 1. 查找一个数组中第k小的数 2. 移去链表倒数第n个节点-LeetCode 3. 合并两个有序链表,递归实现-LeetCode 4. 合并k个有序链表-LeetCode 5. 将有序单链表转化为平衡二叉树原创 2017-05-28 15:47:40 · 660 阅读 · 0 评论 -
合并k个有序链表
23. Merge k Sorted Listsstruct ListNode* mergeTwoLists(struct ListNode *l1,struct ListNode *l2){ if(l1==NULL) return l2; if(l2==NULL) return l1; if(l1->val<l2->val) { l1->next=原创 2017-08-07 13:00:45 · 365 阅读 · 0 评论 -
数列最大连续子数组和
分治法:#include <iostream>#include <vector>using namespace std;int A[]={-2,1,-3,4,-1,2,1,-5,4};int CrossSubArray(int A[],int low,int high,int mid){ int left_max=0x80000000; int right_max=0x80原创 2017-08-27 10:39:49 · 276 阅读 · 0 评论