随笔分类 -  algorithm

摘要:题目要求:将字符串以词为单位进行翻转。如:"a bc def gh ij kml"变成"kml ij gh def bc a"。时间复杂度O(n),空间复杂度O(1)。思路:先将整个字符串按字母进行翻转,然后再逐个单词翻转。代码如下:#include<iostream>using namespace std;void wordturn(char *src, int len){ //按字母翻转 for(int i =0; i < len/2; i++){ char temp = src[i]; src[i] = src[len-i-1]; 阅读全文
posted @ 2013-04-03 22:26 Tiu.G 阅读(186) 评论(0) 推荐(0)
摘要:题目要求:在一个有序数组里面找一对数(两个数),使他们相加等于另外一个数。如,{1,2,4,7,11,16}要求找出和为15的两个数,如果有多组数满足要求,输出其中一组。思路:因为数组已经有序,我们用二分法来寻找这对数。这个题目是二分法的一种应用。代码如下:#include <iostream>using namespace std;int *find_two_digits(int *array, int len, int goal){ int *pi = new int[2]; for(int i = 0 ; i < len; i++){ int low = 0; int 阅读全文
posted @ 2013-04-03 20:34 Tiu.G 阅读(198) 评论(0) 推荐(0)
摘要:题目要求:把一个数组分成左边奇数右边偶数。要求效率尽量高。代码如下:#include <iostream>using namespace std;/*两数交换函数*/void swap(int &a,int &b){ int temp = a; a = b; b = temp;}/*划分函数*/void partition_odd_even(int a[], int count){ int low = 0;int high = count -1;while(low <= high){ while(a[low] % 2 == 1)low++; while(a[h 阅读全文
posted @ 2013-04-03 20:08 Tiu.G 阅读(504) 评论(0) 推荐(0)
摘要:字符串转化为整数需要注意以下几个问题: 1.检测非法输入; 2.空串、空指针; 3.判断数字的正负; 4.处理数字的上下溢出。(因为整数是有范围的。)#include <iostream>long long StrToIntCore(const char* digit,bool minus);enum Status{kValid = 0, kInvalid};int g_nStatus = kValid; //定义一个全局变量来标志是否遇到非法输入。int StrToInt(const char* str){g_nStatus = kInvalid;long long num = 阅读全文
posted @ 2013-03-26 10:37 Tiu.G 阅读(198) 评论(0) 推荐(0)
摘要:quicksort可以说是应用最广泛的排序算法之一,它的基本思想是分治法,选择一个pivot(中轴点),将小于pivot放在左边,将大于pivot放在右边,针对左右两个子序列重复此过程,直到序列为空或者只有一个元素。这篇blog主要目的是关注quicksort可能的改进方法,并对这些改进方法做评测。其目的是为了理解Arrays.sort(int [ ]a)的实现。实现本身有paper介绍。 quicksort一个教科书式的简单实现,采用左端点做pivot(《算法导论》上伪代码是以右端点做pivot):Code highlighting produced by Actipro CodeHigh. 阅读全文
posted @ 2012-12-09 14:59 Tiu.G 阅读(226) 评论(0) 推荐(0)
摘要:#include <iostream>using namespace std;//元素交换函数void lvSwap(int *apiArray, int index1, int index2){ int temp = apiArray[index1]; apiArray[index1] = apiArray[index2]; apiArray[index2] = temp;}//快速排序void lvQuickSort(int *apiArray, int arraySize){ int liLast=0; int i; if(arraySize > 1){ lvSwap( 阅读全文
posted @ 2012-12-09 11:32 Tiu.G 阅读(288) 评论(0) 推荐(0)
摘要:题目:将输入的字符串逆序存储,要求不能改变原字符串。例如:str="Hello world",str2="dlrow olleH".#include <iostream>#include <assert.h>#include <string>using namespace std;void StringReverse(const char* str, int len, char *str2){ assert(str); for(int i = len; i > 0; i--){ *(str2++) = *(str 阅读全文
posted @ 2012-12-09 11:24 Tiu.G 阅读(530) 评论(0) 推荐(0)
摘要:请设计一个算法过滤掉字符串中的某个字母(可能多次出现),比如adecdefg过滤掉e后得到abcdfg。要求时间复杂度为O(n),空间复杂度为O(1)。char*StrPurify(char*src,chardelCh){ if(NULL==src)returnNULL; char*p1,*p2; p1=p2=src; while(*src){ if(*src!=delCh) *(p1++)=*(src++); else src++; } *p1='\0'; returnp2;}用一次循环及两个指针,符合时间复杂度O(n),空间复杂度O(1) 阅读全文
posted @ 2012-12-09 11:20 Tiu.G 阅读(149) 评论(0) 推荐(0)
摘要:题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的谦虚遍历和中序遍历的结果中都不含重复的数字。#include <iostream>#include <string>using namespace std;int find(const string &str, char c) //在字符串中查找字符的函数,参数是string类引用和字符{ for (int i = 0; i < str.size(); ++ i) if (c == str[i]) return i; //找到之后返回 return -1;}void PreMid(c 阅读全文
posted @ 2012-12-09 11:17 Tiu.G 阅读(207) 评论(0) 推荐(0)
摘要:现在有一个整数,对其进行逆序存储。例如,3 (0000 0000 0000 0000 0000 0000 0000 0011)2 3221225472 (1100 0000 0000 0000 0000 0000 0000 0000)2/*整数逆序存储*/#include <stdio.h>unsigned int reverse(unsigned int a){unsigned int b=0;for(int i=0;i<31;i++){if(a & 0x01){b = b | 0x01;}b = b<<1;a = a>>1;}return 阅读全文
posted @ 2012-11-27 23:58 Tiu.G 阅读(395) 评论(0) 推荐(0)
摘要:/*****************************************************//*假设有一个没有头指针的单链表。一个指针指向此单链表**//*中间的一个节点,请将该节点从单链表中删除************//*****************************************************/#include <stdlib.h>#include <stdio.h>#include <assert.h>typedef struct Node{ char value; struct Node *next; 阅读全文
posted @ 2012-08-15 16:51 Tiu.G 阅读(305) 评论(0) 推荐(0)
摘要:/*********************字符串移位包含的问题********************//*给定两个字符串s1和s2,要求判定s2是否能够被s1做循环移位得到的*//*字符串包含。例如,给定s1=AABCD和s2=CDAA,返回true;给定s1= *//*ABCD和s2=ACBD,返回false。 *//*************************************************************/#include <stdio.h>#include <stdlib.h>#include <string.h># 阅读全文
posted @ 2012-08-13 16:40 Tiu.G 阅读(219) 评论(0) 推荐(0)