随笔分类 - 算法面试
摘要:题目:将一个4字节整数的二进制表示中的001替换为011答:int replace(int num){ unsigned int mode3bit = 7; unsigned int mode1bit = 1; int shift = 0; int result = 0; while (shift < 32) { while (shift < 32 && (num & (mode3bit<<shift)) != (1<<shift)) { result += (num & (mode1bit<<shift))...
阅读全文
摘要:题目:在一个int数组里查找这样的数,它大于等于左侧所有数,小于等于右侧所有数。答:#include "stdafx.h"#include <iostream>using namespace std;//数组中大于等于左侧所有数,小于等于右侧所有数的数void FindSpecialNumber(int *arr, int length){ if (NULL == arr || length <= 0) { return; } int *rightMin = new int[length]; rightMin[length - 1] = arr[le...
阅读全文
摘要:题目:求最大连续递增数字串(如“ads3sl456789DF3456ld345AA”中的“456789”。答:#include "stdafx.h"#include <iostream>#include <string>using namespace std;//求最大连续递增数字串string FindMaxIncreNumberSeq(string str){ if ("" == str || str.length() <= 0) { return NULL; } int maxlength = 0; int start
阅读全文
摘要:题目:一个长度为n的数组a[0],a[1],...,a[n-1]。现在更新数组的各个元素,即a[0]变为a[1]到a[n-1]的积,a[1]变为a[0]和a[2]到a[n-1]的积,...,a[n-1]为a[0]到a[n-2]的积(就是除掉当前元素,其他所有元素的积)。要求:具有线性复杂度,且不能使用除法运算符。答:#include "stdafx.h"#include <iostream>using namespace std;void PrintArray(int *a, int length){ for (int i = 0; i < length;
阅读全文
摘要:题目:求两个串中的第一个最长子串(神州数码以前试题)。如"abractyeyt","dgdsaeactyey"的最大子串为"actyey"。答:#include "stdafx.h"#include <iostream>#include <string>using namespace std;//两个串中的第一个最长子串string FindLongestCommonSubString(string strOne, string strTwo){ if ("" == st
阅读全文
摘要:题目:给定一个长度为N的整数数组,只允许用乘法,不能用乘法,计算任意(N-1)个数的组合乘积中最大的一组,并写出算法的时间复杂度。答:#include "stdafx.h"#include <iostream>using namespace std;#define INFINITY_MAX 32767#define INFINITY_MIN -32767void PrintSubArray(int *arr, int length, int key){ bool first = true; for (int i = 0; i < length; i++)
阅读全文
摘要:题目:如下:int A[nSize],其中隐藏着若干0,其余非0整数,写一个函数int Func(int* A, int nSize),使A把0移至后面,非0整数移至数组前面并保持有序,返回值为原数据中第一个元素为0的下标。(尽可能不使用辅助空间且考虑效率及异常问题,注释规范且给出设计思路)。答:#include "stdafx.h"#include <iostream>using namespace std;int Func(int *A, int nSize){ if (NULL == A || nSize <= 0) { return -1; } i
阅读全文
摘要:题目:将一个随机的整数转换成一个按各位上数值大小排序的整数,例如整数2541转换成1245,随机整数521368转换成123568,要求不能使用一步到位的库函数.答:#include "stdafx.h"#include <iostream>using namespace std;//字符串数字从小到大输出void Solution(char *str){ if (NULL == str) { return; } unsigned int hashTab[10] = {0}; char *p = str; while (*p != '\0') .
阅读全文
摘要:题目:对于一颗完全二叉树,要求给所有节点加上一个pNext指针,指向同一层的相邻节点;如果当前节点已经是该层的最后一个节点,则将pNext指针指向NULL;给出程序实现,并分析时间复杂度和空间复杂度。答:时间复杂度为O(n),空间复杂度为O(1)。#include "stdafx.h"#include <iostream>#include <fstream>#include <vector>using namespace std;struct TreeNode { int m_nValue; TreeNode *m_pLeft; Tree
阅读全文
摘要:题目:输入两个整数n和m,从数列1,2,3.....n中随意曲几个数,使其和等于m,要求将其中所有的可能组合列出来。答:#include "stdafx.h"#include <iostream>#include<list> using namespace std;//1到N中所有和为M的组合void FindAllCombinationEqualM(list<int> &l, int m, int n){ if (m <= 0 || n <= 0) { return; } if (m == n) { for (lis
阅读全文
摘要:题目:请使用代码计算1234567891011121314151617181920*2019181716151413121110987654321。答:#include "stdafx.h"#include <iostream>#include <string>using namespace std;int _tmain(int argc, _TCHAR* argv[]){ string strOne; string strTwo; cout<<"输入第一个乘数:"; cin>>strOne; cout&l
阅读全文
摘要:题目:将字符串原地压缩,比如"eeeeeaaaff"压缩为 "e5a3f2"。答:#include "stdafx.h"#include <iostream>using namespace std;//字符串原地压缩void CompressString(char *str){ if (NULL == str) { return; } int count = 0; char *newStr = str; char ch = *str; while (*str != '\0') { if (*str != c
阅读全文
摘要:题目:给定两个字符串s1和s2,要求判定s2是否能够被s1做循环移位得到的字符串包含。例如,给定s1=AABCD和s2=CDAA,返回true;给定s1=ABCD和s2=ACBD,返回false。答:#include "stdafx.h"#include <iostream>using namespace std;//获取next数组的值void GetNext(const char* pattern, int length, int *next){ int i = 0; next[i] = -1; int j = -1; while (i < lengt
阅读全文
摘要:题目:已知一个字符串,比如asderwsde,寻找其中的一个子字符串比如sde的个数,如果没有返回0,有的话返回子字符串的个数。答:#include "stdafx.h"#include <iostream>using namespace std;//获取next数组的值void GetNext(const char* pattern, int length, int *next){ int i = 0; next[i] = -1; int j = -1; while (i < length - 1) { if (-1 == j || pattern...
阅读全文
摘要:题目:给定一棵二叉树,要求按分层遍历该二叉树,即从上到下按层次访问该二叉树(每一层将单独输出一行),每一层要求访问的顺序从左到右。答:#include "stdafx.h"#include <fstream>#include <iostream>#include <vector>using namespace std;struct TreeNode { int m_nValue; TreeNode *m_pLeft; TreeNode *m_pRight;};//假定所创建的二叉树如下图所示/* ...
阅读全文
摘要:题目:类CMyString的声明如下,请实现其赋值运算符的重载函数,要求异常安全,即当对一个对象进行赋值时发生异常,对象的状态不能改变。class CMyString{public: CMyString(char* pData = NULL); CMyString(const CMyString& str); CMyString& operator = (const CMyString& str); ~CMyString();private: char* m_pData;};答://1、可能有异常CMyString& CMyString::operator =
阅读全文
摘要:题目:输入一个正整数数组,将它们连接起来排成一个数,输出能排出的所有数字中最小的一个。举例:输入数组{32, 321},则输出这两个能排成的最小数字32132。请给出解决问题的算法,并证明该算法。答:算法如下,证明略。答:#include "stdafx.h"#include <iostream>#include <string>#include <sstream>using namespace std;//把int转化为stringstring int2str(int i) { string s; stringstream ss(s);
阅读全文
摘要:题目:把一个有序整数数组放到二叉树。答:#include "stdafx.h"#include <iostream>using namespace std;struct TreeNode { int m_nValue; TreeNode *m_pLeft; TreeNode *m_pRight;};//把一个有序整数数组放到二叉树void RecurCreateTree(int *p, int length, TreeNode *&pHead){ if (length > 0) { pHead = new TreeNode; int m...
阅读全文
摘要:题目:从扑克牌中随机抽5张牌,判断是不是一个顺子,即这5张牌是不是连续的。2-10为数字本身,A为1,J为11,Q为12,K为13,而大小王可以看成任意数字。答:#include "stdafx.h"#include <iostream>#include <ctime>using namespace std;#define MAXVALUE 10000#define MINVALUE -1#define SIZE 14 #define NUMBER 5//扑克牌的顺子bool IsSort(int arr[], int length){ int pl
阅读全文
摘要:题目:二叉树的结点的定义如下:struct TreeNode { int m_nValue; TreeNode *m_pLeft; TreeNode *m_pRight;};输入二叉树中的两个结点,输出这两个结点在数中最低的共同父结点。答:#include "stdafx.h"#include <iostream>#include <fstream>#include <ctime>using namespace std;struct TreeNode { int m_nValue; TreeNode *m_pLeft; TreeNode
阅读全文