随笔分类 - 算法面试
摘要:题目:我们把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第1500个丑数。答:#include "stdafx.h"#include <iostream>using namespace std;//1、从1开始穷举,直到第1500个丑数,效率太低//判断number是否为丑数bool IsUgly(int number){ while (number % 2 == 0) { number = number / 2; } while (num...
阅读全文
摘要:题目:写一个函数,它的原型是如下,在字符串中找出连续最长的数字串,并把这个串的长度返回,并把这个最长数字串付给其中一个函数参数outputstr所指的内存。int continuemax(char *outputstr, char *inputstr)举例:intputstr被赋予"abcd12345ed125ss123456789",函数将返回9,outputstr所指的值为"123456789"。答:#include "stdafx.h"#include <iostream>using namespace std;//
阅读全文
摘要:题目:一个整数数组里除了两个数字之外,其他的数字都出现了两次,找出这两个只出现一次的数字。要求:时间复杂度是O(n),空间复杂度是O(1)。答:#include "stdafx.h"#include <iostream>using namespace std;bool isBitOne(int number, int index){ number = number >> index; if (number & 1) { return true; } return false;}//找出数组中两个只出现一次的数字void FindNumber(
阅读全文
摘要:题目:给定单链表,如果有环的话请返回从头结点进入环的第一个结点。答:#include "stdafx.h"#include <iostream>#include <fstream>#include <ctime>using namespace std;struct ListNode{ int m_nKey; ListNode* m_pNext;};//构造链表void CreateList(ListNode *&pHead, ListNode *&pFirstNode){ fstream fin("list.tx
阅读全文
摘要:题目:输入一个字符串,打印出该字符串的所有子集。举例:输入字符串ab,则输出ab所有的子集{a b},{a},{b},{}。答:#include "stdafx.h"#include <iostream>using namespace std;//获取n个元素的子集void GetAllSubset(char *a, int *mask, int size, int c){ if (size == c) { cout<<"{ "; for (int i = 0; i < size; i++) { if (mask[i]...
阅读全文
摘要:题目:输入一个字符串,打印该字符串的所有排列。举例:输入字符串abc,则输出由字符a、b、c所能排列出来的所有字符串abc、acb、bac、bca、cab和cba。答:#include "stdafx.h"#include <iostream>using namespace std;//交换两个数void swap(char &a, char &b){ char tmp = a; a= b; b = tmp;}//获取数组a的全排列void GetAllRange(char *a, int size, int c){ if (size == c)
阅读全文
摘要:题目:用递归颠倒一个栈,例如输入栈{1, 2, 3, 4, 5},1在栈顶。颠倒之后的栈为{5, 4, 3, 2, 1},5处在栈顶。答:#include "stdafx.h"#include <iostream>#include <stack>using namespace std;void AddStackBottom(stack<int> &s, int top){ if (s.empty()) { s.push(top); } else { int tmp = s.top(); s.pop(); Ad...
阅读全文
摘要:题目:编码完成处理函数:函数将字符串中的字符'*'移到串的前面,前面的非'*'字符后移,但不能改变非'*'字符的先后顺序,函数返回串中字符'*'的数量。举例:原始串:ab**cd**e*12,处理后为*****abcde12,函数返回值为5。要求:使用尽量少的时间和辅助空间。答:#include "stdafx.h"#include <iostream>using namespace std;//将字符串中的*移到串的前部分int RemoveStarToFront(char *str){ if (
阅读全文
摘要:题目:删除字符串中的数字并压缩字符串。举例:输入字符串"abc123de4fg56", 输出"abcdefg"。要求:不开辟新空间,时间复杂度为O(n)。答:#include "stdafx.h"#include <iostream>using namespace std;//删除字符串中的数字并压缩字符串void RemoveNumberChar(char *str){ if (NULL == str) { return; } char *p = str; char *pNewStr = str; while (*p !
阅读全文
摘要:题目:给定链表的头指针和一个结点指针,在O(1)时间内删除该结点,链表结点的定义如下:struct ListNode{ int m_nKey; ListNode* m_pNext;};函数的声明如下:void DeleteNode(ListNode *pListHead, ListNode *pToBeDeleted);答:#include "stdafx.h"#include <iostream>#include <fstream>#include <ctime>using namespace std;struct ListNode{
阅读全文
摘要:题目:输入两个字符串,从第一个字符串中删除第二个字符串中所有的字符。举例:输入"They are students."和"aeiou",则输出之后的第一个字符串变成"Thy r stdnts."。答:#include "stdafx.h"#include <iostream>using namespace std;//在字符串中删除特定的字符void DeleteSpecialChar(char *pStr, const char *pDelStr){ const int length = 256; i
阅读全文
摘要:题目:数组中有一个数组出现的次数超过了数组长度的一半,找出这个数字。答:#include "stdafx.h"#include <iostream>using namespace std;//查找数组中超过出现次数超过一半的数字int FindNumber(int arr[], int length){ if (NULL == arr || length <= 0) { return -1; } int nValue = arr[0]; int count = 1; for (int i = 1; i < length; i++) { ...
阅读全文
摘要:题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个排好序的数组的一个旋转。输出旋转数组的最小值。举例:输入数组{3, 4, 5, 1, 2}为{1, 2, 3, 4, 5}的一个旋转,该数组的最小值为1。答:#include "stdafx.h"#include <iostream>using namespace std;//查找旋转数组中的最小元素int FindMinValue(int arr[], int length){ int low = 0; int high = length - 1; while (low <
阅读全文
摘要:题目:输入一个正数n,输出所有和为n的连续正数序列举例:输入15,由于1+2+3+4+5=4+5+6=7+8=15,所以输出3个连续序列1,2,3,4,5;4,5,6;7,8。答:#include "stdafx.h"#include <iostream>using namespace std;//求和为n的连续正数序列void FindSequenceNumber(int n){ if (n <= 0) { return; } int first = 1; int last = 2; int end = (n + 1) / 2; int...
阅读全文
摘要:题目:输入一个整数数组,调整数组中数字的顺序,使得所有奇数位于数组的前半部分,所有偶数位于数组的后半部分。要求:时间复杂度为O(n)答:#include "stdafx.h"#include <iostream>using namespace std;bool IsOddNumber(int n){ return (n & 1) != 0;}void AdjustArray(int arr[], int length, bool (*Func)(int)){ int begin = 0; int end = length - 1; while (begi
阅读全文
摘要:题目:两个单链表,找出它们的第一个公共结点。链表的结点定义为: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, fstream &fin, ListNode
阅读全文
摘要:题目:输入一个链表头结点,从尾到头反过来输出每个结点的值。链表结点定义如下:struct ListNode{ int m_nKey; ListNode* m_pNext;};答:1、可以先把链表逆置,然后再输出,具体参考 http://www.cnblogs.com/venow/archive/2012/08/26/2657559.html 这里我们使用另一种更为简单的方法:递归#include "stdafx.h"#include <iostream>#include <fstream>using namespace std;struct List
阅读全文
摘要:题目:输入一个单向链表,将该单链表逆置。举例:原来链表为1->2->3->4->5翻转为5->4->3->2->1链表结点定义如下: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 C
阅读全文
摘要:题目:输入一个整数n,求1到n这n个整数十进制表示中1出现的次数。举例:输入12,从1到12这些整数中包含1的数字有1, 10, 11,和12,1一共出现了5次。答:#include "stdafx.h"#include <iostream>using namespace std;int Count(int num){ int count = 0; while (num) { if (1 == num % 10) { count++; } num = num /10; } return ...
阅读全文
摘要:题目:输入一个整数,求该整数的二进制表达中有多少个1。举例:输入10,其二进制表示为1010,有两个1,因此输出2。答:#include "stdafx.h"#include <iostream>using namespace std;//只限于正数(输入负数会陷入死循环)int CountNonnegativeOneNumber(int n){ int count = 0; while (n) { if (n & 1) { count++; } n = n>>1; } return c...
阅读全文
浙公网安备 33010602011771号