![](https://img-blog.csdnimg.cn/20201014180756754.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
面试题操练场
文章平均质量分 68
濯茶的前端思考
知乎/公号:前端疯
展开
-
调整数组顺序使奇数位于偶数前面
题目:输入一个整数数组,调整数组中数字的顺序,使得所有奇数位于数组的前面部分,偶数位于数组的后面部分。思路:要求时间复杂度是Ο(n)。1:从头开始扫描数组,确定数是奇数不动,偶数的话就移动数组最后。这样每个偶数都要进行移动,最坏的时间复杂度达到Ο(n^2)。2:用两个指针,头指针指向数组开始,尾指针指向数组末尾。它们分别向中间移动,如果头尾指针不是奇数,尾指针不是偶数,则交换它们。原创 2013-09-08 15:18:24 · 695 阅读 · 0 评论 -
冒泡排序 一个简单示例
sort.cvoid swap(int iList[], int a, int b){ int temp; temp = iList[a]; iList[a] = iList[b]; iList[b] = temp;}void bubbleSort(int iList[], int iLength){ int i, j; j = 1; while(1) {原创 2013-03-10 20:50:25 · 1158 阅读 · 0 评论 -
面试题——字符串类型001
#include void remove_repeat(char *s,int iLen){ for(int i=0;s[i];i++){ for(int j=i+1;s[j];){ if(s[i]==s[j]){ for(int m=j;s[m];m++){ s[m]=s[m+1]; } }else{ j++; } } }} int main(){原创 2014-11-13 23:45:09 · 648 阅读 · 0 评论 -
表插入排序
TableInsertSort.c#include #include #include typedef struct//data struction{ int data; int next;}Node, *NodePtr;static NodePtr nodePtr = NULL;static int *iArrayTemp = NULL; static int iAr原创 2013-03-10 19:40:03 · 609 阅读 · 0 评论 -
2-路插入排序 一个简单示例
//a example of 2-way insertion sort#include #include int iList[] = {1, 4, 2, 12, 4, 34, 243, 11, 35};void TwoWayInsert(int iList[], int iLen)//iLen is the length of iList{ int i, j; int firs原创 2013-03-09 22:04:44 · 695 阅读 · 0 评论 -
简单选择排序 一个简单示例
#include int iList[] = {152, 11, 98, 23, 33};void sort(int iList[], int iLength){ int iMostSmallIndex;//save the index of last number int i, j; int iTemp;//for temporary use for(i = 1; i < i原创 2013-03-10 21:48:54 · 958 阅读 · 0 评论 -
折半插入排序 一个简单示例
#include /** a example of binary insertion sort*/int iList[] = { 0, 99, 3, 8, 123, 4, 6, 2, 9,11 };//init a array for sort and iList[0] is not usedvoid BinaryInsertSort(int iList[], int iLen)/原创 2013-03-03 23:39:32 · 1157 阅读 · 0 评论 -
直接插入排序 一个简单示例
#include int iList[11] = {0,4,5,2,4,6,92,37,7,29,8};//iList[0] is not used//直接插入排序void InsertDirect(int iList[], int iLen)//iLen is the Length of iList{ int i; int j; for(i = 2; i < iLen; i++原创 2013-03-03 18:06:56 · 763 阅读 · 0 评论 -
快速排序的单链表递归实现
// C++ program for Quick Sort on Singly Linled List#include #include using namespace std; /* a node of the singly linked list */struct node{ int data; struct node *next;}; /* A utili原创 2014-11-26 15:50:23 · 1349 阅读 · 0 评论 -
已排序的数组中找到k个距离x最近的元素
比如数组 arr[] = {12, 16, 22, 30, 35, 39, 42, 45, 48, 50, 53, 55, 56}原创 2014-11-26 16:29:25 · 2003 阅读 · 0 评论 -
快速排序 一个简单示例
sort.cvoid swap(int iList[], int a, int b){ int temp; temp = iList[a]; iList[a] = iList[b]; iList[b] = temp;}void quickSort(int iList[],int left, int right){ int last; int i; if(left原创 2013-03-10 20:17:34 · 739 阅读 · 0 评论 -
最大流 C++实现
#include #include using namespace std;#define VERTEX 7 //顶点的个数#define UINT_MAX 0xFFFF//图/////////////////////////////////////////// S A B C D E T// S 0 3 1 0 0原创 2014-11-23 10:08:39 · 1627 阅读 · 0 评论 -
求二元树的深度
题目:输入一颗二元树的根节点,求该树的深度。从根节点到叶子节点形成的一条路径,最长路径就是树的深度。思路:1.遍历该树,得到所有路径长度,求出最大的路径长度,即为树的深度2.采用递归思想,先求左子树的深度A,再求右子树的深度B,树的深度为max(A,B) + 1代码如下:思路2typedef struct tagTree{ struct tagTree *left;原创 2013-09-07 16:56:40 · 1393 阅读 · 0 评论 -
字符串的排列
题目:输入一个字符串,打印该字符串的所有排列。例如输入abc,打印它的所有排列为abc,acb,bac,bca,cab,cba。思路:原创 2013-09-07 20:07:43 · 615 阅读 · 0 评论 -
整型数组中只出现一次的数字
一、数组中一个只出现一次的整数题目:一个数组中,有一个数字只出现一次,其他数字都出现两次,写出程序找出这个数字。思路:利用异或运算,如果是两个相同的数,那么他们的结果就是0,剩下的就是只出现一次的数。代码如下:#include //find a number,which appearing once in an arrayint FindNumAppearOnce(int a原创 2013-09-09 16:23:19 · 1843 阅读 · 0 评论 -
程序员面试题精选100题(61)-数对之差的最大值[算法]
题目:在数组中,数字减去它右边的数字得到一个数对之差。求所有数对之差的最大值。例如在数组{2, 4, 1, 16, 7, 5, 11, 9}中,数对之差的最大值是11,是16减去5的结果。分析:看到这个题目,很多人的第一反应是找到这个数组的最大值和最小值,然后觉得最大值减去最小值就是最终的结果。这种思路忽略了题目中很重要的一点:数对之差是一个数字减去它右边的数字。由于我们无法保证最大值转载 2013-09-10 10:59:24 · 731 阅读 · 0 评论 -
运行如下结果 输出时什么?
运行如下代码,输出时什么?一、char* GetString1(){ char p[] = "Hello World"; return p;} char* GetString2(){ char *p = "Hello World"; return p;} int _tmain(int argc, _TCHAR* argv[]){转载 2013-09-15 14:14:45 · 784 阅读 · 0 评论 -
写一个函数,求两个整数的之和,要求在函数体内不得使用+、-、×、÷。
题目:写一个函数,求两个整数的之和,要求在函数体内不得使用+、-、×、÷。思路是:第一步,求不进位相加和sum第二步,求进位carry第三部,sum和carry*10的和比如:23 8的和第一步后是:21第二步后是:1第三步后是:31代码如下:递归解法:int AddWithoutArithmetic(int num1, int num2){转载 2013-09-15 16:05:39 · 2024 阅读 · 0 评论 -
笔记摘录——Cracking the code interview
纸上写代码简历:对你参与的任何项目或者工作做一个总结,并阐述最难的部分以及最有趣的部分。不要记忆解决方案:不是说让你不要记忆,而是要想清楚它的思路,也就不需要记忆了大声地说出来你的想法微软面试:聪明,对技术有激情,极客Be nice to your recruiter 对雇佣你的人要好 因为他们决定了要不要雇佣你4到原创 2014-11-12 23:42:47 · 3641 阅读 · 0 评论 -
面试题2——字符串类型
#include int isUnique(char *s){ int i=0; char *tmp=s; for(;*tmp;tmp++){ for(s=tmp+1;*s;s++){ if(*tmp==*s){ return 0; } } } return 1;} int main(){ char s[]="asdf"; printf("%d\n",isUnique(s))原创 2014-11-14 00:00:13 · 517 阅读 · 0 评论 -
希尔排序 一个简单示例
sort.c//shell sort#include static int iAddArray[] = {5, 3, 2, 1};static int iArrLength;void ShellInsert(int iList[], int addSize)//using insert directly sort{ int i, j; int iSaveInsNum;//us原创 2013-03-10 19:41:46 · 801 阅读 · 0 评论 -
广度优先搜索二叉树,并返回节点深度
见代码:/* * prototype.c * * Search a binary tree and tell the result and its depth. * * Copyright (C) 2012-10-22 liutos */#include #include typedef struct treeNode { char value; stru原创 2014-10-22 00:05:56 · 525 阅读 · 0 评论