- 博客(13)
- 收藏
- 关注
原创 时间O(n)/空间O(1)实现长n字符串前m个移到末尾
#includeusing namespace std;//分析空间O(1)实现前m个字符移动末尾,如abcdef前三个移到末尾后:defabc,分为两部分XY,先逆转X,再逆转Y,再把整个字符串逆转即可,见rotateFromLeft2Right函数。void reverse(char *start,char *end){//字符串首尾逆转while(startchar
2013-06-10 10:32:30
1137
原创 二叉排序树转化为双向链表
#includeusing namespace std;struct BTnode{int value;BTnode *left;BTnode *right;}*head=NULL,*tail=NULL;BTnode *newNode(int value){//初始化一个新的节点BTnode *Node=(BTnode *)malloc(sizeof(BTnod
2013-06-06 15:03:06
722
原创 和为n 连续正数序列
#include#define MAX 999using namespace std;struct subStr{int start,end;};//例如输入15,由于1+2+3+4+5=4+5+6=7+8=15,所以输出3 个连续序列1-5、4-6 和7-8。void subStringSumOfn(int n,subStr arr[],int &count){i
2013-05-23 16:12:13
609
原创 整数二进制表示中1的个数
#include#includeusing namespace std;//末尾与1做位与运算,如果为1则表明末位是1,否则为0;此末位与10做位与运算,如果为1表明次末位是1,否则为0,倒数第三位。。。//负数在计算机中补码表示,如-1在32位机中表示:1111 1111 1111 1111 1111 1111 1111 1111(原码为1000 0000 0
2013-05-23 00:27:04
534
原创 找到第一个只出现一次的字符
#include#includeusing namespace std;char* firstNotRepeatingChar(char *str){ //在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b。 if(!str) return NULL; int count[256]; char *p
2013-05-22 15:05:29
554
原创 排序数组中查找和为给定值的两个数O(n)
#includeusing namespace std;//排序数组中查找和为给定值的两个数,时间复杂度O(n)bool findTwoNumbersWithSum(int data[],int length,int sum,int &num1,int &num2){int ahead=0,behind=length-1;if(behindreturn false;
2013-05-22 10:58:37
482
原创 二叉树镜像之递归、栈、队列实现
#include#include#include#define N 10using namespace std;struct BTnode{int value;BTnode* left;BTnode* right;};BTnode *mirror(BTnode *root){if(!root)return NULL;BTnode *tem
2013-05-19 01:46:47
738
原创 选择排序 selection sort c++
#include#include#define MAX 10using namespace std;//selection sort选择排序,不稳定,最差最好时间复杂度=o(n^2),空间复杂度O(1)int main(){ifstream cin("in.txt");int arr[MAX],i,j,temp;for(i=0;icin>>arr[i
2013-05-17 07:52:07
577
原创 堆排序 heapsort(大根堆) c++
//堆排序heapSort:分为建堆和利用堆排序两部分,每次输出堆顶元素,然后把堆顶和堆末元素交换再对前n-1个元素(因为最大元素已经输出不再参与下次建堆过程)的堆排序(只需调整arr[1])。//不稳定 时间O(n)=nlog2 (n) 空间O(1),在交换堆首位元素用到 大文件作用明显#include#include#define MAX 11using namespa
2013-05-15 23:54:10
704
原创 快速排序 c++
//快速排序:第一次选取第一个元素做中枢值pivot,大的排在该元素右边,小的排在左边//不稳定 快 T(n)=nlog2 (n) 文件平均划分O(n)=log(n)/倒序排列O(n)=n#include#include#define N 10using namespace std;void quicksort(int arr[],int left,int righ
2013-05-14 20:44:00
607
原创 冒泡排序(倒序)C++
//冒泡排序:每一轮排序后最大(或最小)的数将移动到数据序列的最后//理论上总共要进行n(n-1)/2次交换。稳定 慢#include#include#define N 10using namespace std;int main(){ifstream cin("content.txt");int arr[N];for(int i=0;icin>
2013-05-14 20:23:02
1614
原创 反转链表与指针传递 c++
#include#includeusing namespace std;//链表结构struct Node{int data;Node *next;};Node *head;Node *create(int &n){//创建链表ifstream cin("content.txt");Node *curr;head=NULL;for(i
2013-05-14 20:13:20
558
原创 二叉树层次遍历 c++
#include#include#define N 10using namespace std;struct BTnode{int value;BTnode *left;BTnode *right;};void fromTop2Bottom(BTnode *root){if(!root)return;dequeque;que.push_b
2013-05-14 19:27:33
843
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人