随笔分类 - 算法面试
摘要:题目:一个台阶总共有n级,如果一次可以跳1级,也可以跳2级。求总共有多少种跳法,并分析算法的时间复杂度。答:用一个函数f(n)来表示n级台阶总的跳法。 1、只有1个台阶,则f(1) = 1; 2、有2个台阶,则f(2) = 2; 3、当有n个台阶时,如果第一次跳1级,有f(n-1)种跳法,如果第一次跳2级,有f(n - 2)种跳法,即f(n) = f(n-1) + f(n-2)。即为Fibonacci序列。#include "stdafx.h"#include <iostream>using namespace std;//循环int TotalStep(int
阅读全文
摘要:题目:定义字符串的左旋转操作,把字符串前面的若干个字符移动到字符串的尾部。要求:对长度为n的字符串操作的时间复杂度为O(n),辅助内存为O(1)。举例:把字符串abcdef左旋转2位得到字符串cdefab。答:#include "stdafx.h"#include <iostream>using namespace std;void swap(char *str, int begin, int end){ char ch; while (begin < end) { ch = *(str + begin); *(str + begin) = *(s...
阅读全文
摘要:题目:在一个字符串中找到第一个只出现一次的字符。举例:输入abaccdeff,则输出b。答:假设字符占一个字节,则共有256不同的字符,开辟256空间,用查找表。#include "stdafx.h"#include <iostream>using namespace std;void FindFirstOneChar(char *str){ if (NULL == str) { return; } int count[256] = {0}; char *p = str; while (*p != '\0') { count...
阅读全文
摘要:题目:输入一个英文句子,翻转句子中单词的顺序,但单词内的字符的顺序不变。句子中单词以空格符隔开。为简单起见,标点符号和简单字母一样处理。举例:输入"I am a student.",则输出"student. a am I"。答:每个单词先自我翻转,然后整个句子翻转。#include "stdafx.h"#include <iostream>#include <string>using namespace std;/* 翻转 I am a student. --> student. a am I*/void
阅读全文
摘要:题目:输入一个整数和一棵二元树,从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。打印出和与输入整数相等的所有路径。答:二叉树的后序非递归遍历#include "stdafx.h"#include <iostream>#include <fstream>#include <queue>using namespace std;typedef struct _Node{ int data; struct _Node *left; struct _Node *right; bool isVisit; //后序遍历标志(非递归)
阅读全文
摘要:题目:输入一个单向链表,输出该链表中倒数第k个结点。链表的倒数第0个结点为链表的尾指针。链表结点定义如下:struct ListNode{ int m_nKey; ListNode* m_pNext;};答:#include "stdafx.h"#include <iostream>#include <fstream>using namespace std;struct ListNode{ int m_nKey; ListNode* m_pNext;};//构造链表void CreateList(ListNode *&pHead){ fstr
阅读全文
摘要:题目:输入一个已经按升序排序过得数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字。要求:时间复杂度是O(n)。如果有多对数字的和等于输入的数字,输出任意一对即可。举例:输入数组1、2、4、7、11、15和数字15.由于4 + 11 = 15,因此输出4和11。、答:#include "stdafx.h"#include <iostream>using namespace std;void FindNumber(int arr[], int length, int num){ if (NULL == arr || length <= 0)
阅读全文
摘要:#include "stdafx.h"#include <iostream>using namespace std;//*****************************满二叉树先序、中序和后序之间的转换*****************************begin//先序序列转换为后序序列//参数说明: (in) pre ———— 先序数组// (out) post ———— 后序数组// (in) preLow ———— 先序的第一个结点的下标// (in) preH...
阅读全文
摘要:题目:输入一棵二元查找树,将该树转换为它的镜像,即在转换后的二元查找树中,左子树的结点都大于右子树的结点。要求:用递归和循环两种方法完成树的镜像转换。举例: 8 8 / \ 转换 / \ 6 10 --> 10 6 / \ / \ / \ / \ 5 7 9 11 11 9 7 5二叉树定义的结点为:struct BSTreeNode{ int m_nValue; BST...
阅读全文
摘要:题目:求1 + 2 + 3 + ... + n 的和要求:不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A?B:C)答:#include "stdafx.h"#include <iostream>using namespace std;//1、函数查找表法typedef int (*Func)(int n);int Sum1(int n){ return 0;}int Sum2(int n){ Func sum[2] = {Sum1, Sum2}; return sum[n > 0](n - 1) +
阅读全文
摘要:题目:求子数组的最大和要求:1、输入一个整形数组,数组里有正数也有负数。 2、数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和。 3、求所有子数组的和的最大值。要求时间复杂度为O(n)。举例:输入数组为1, -2, 3, 10, -4, 7, 2, -5。和的最大子数组为3, 10, -4, 7, 2。即输出和为18。答:#include "stdafx.h"#include <iostream>using namespace std;int FindMaxSubSum(int arr[], int length){ int sum = 0; in
阅读全文
摘要:题目:输入一个整数数组,判断该数组是不是某二元查找树的后序遍历结果。如果是,返回true,否则返回false。举例:输入5、7、6、9、11、10、8,由于这个整数序列有如下的树的后序遍历结果: 8 / \5、7、6、9、11、10、8 -> 6 10 / \ / \ 5 7 9 11因此返回tr...
阅读全文
摘要:题目:输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。要求:不能创建任何新的结点,只调整指针的指向。举例: 10 / \ 转变 6 14 --> 4=6=8=10=12=14=16 / \ / \ 4 8 12 16定义的二元查找树结点的数据结构如下:struct BSTreeNode { int m_nValue; BSTreeNode *m_pLeft; BSTreeNode *m_pRight;};答:用二叉树的中序遍历#include "std...
阅读全文
浙公网安备 33010602011771号