![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
剑指Offer
seu_nuaa_zc
这个作者很懒,什么都没留下…
展开
-
面试题50—树中两个节点的最低公共祖先
示例代码: 头文件:tree.h#include<iostream>#include<vector>using namespace std;struct TreeNode{ char value; vector<TreeNode*> next;};TreeNode* CreateTreeNode(char data, int n);void ConnectTreeNo原创 2017-06-28 10:14:36 · 200 阅读 · 0 评论 -
面试题36—数组中的逆序对
**题目:在数组中的两个数如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。求一个数组的逆序对的总数。 代码示例:**#include<iostream>using namespace std;int Merge2(int a[], int low, int mid, int high)//二路归并算法{ int InverseNum = 0; int i = lo原创 2017-06-27 21:48:44 · 211 阅读 · 0 评论 -
面试题35—相关题目
**题目:如果两个单词中出现的字母相同,并且每个字幕出现的次数也相同,这两个单词就是互为变位词 代码示例:**#include<iostream>#include<vector>#include<string>using namespace std;int main(){ string str1 = "silent"; string str2 = "listen";原创 2017-06-27 21:47:29 · 197 阅读 · 0 评论 -
面试题35—相关题目1
**题目:删掉字符串中所有重复的字符 代码示例:**#include<iostream>#include<vector>#include<string>using namespace std;int main(){ string str = "gooogllergtigle"; vector<bool> flag(256, false); for (int i =原创 2017-06-27 21:46:19 · 198 阅读 · 0 评论 -
面试题35—相关题目
**题目:输入两个字符串,从第一个字符串中删掉在第二个字符串中出现的所有字符 代码示例:**#include<iostream>#include<vector>#include<string>using namespace std;vector<bool> HashTable(string str){ vector<bool> flag(256, false); for原创 2017-06-27 21:45:22 · 177 阅读 · 0 评论 -
面试题35—第一个只出现一次的字符
题目:在字符串中找到第一个只出现一次的字符。 代码示例:#include<iostream>using namespace std;char FirstChar(char *str){ if (str == NULL) return '\0'; int temp[256]; for (int i = 0; i < 256; i++) t原创 2017-06-27 21:42:40 · 151 阅读 · 0 评论 -
欢迎使用CSDN-markdown编辑器
欢迎使用Markdown编辑器写博客本Markdown编辑器使用StackEdit修改而来,用它写博客,将会带来全新的体验哦:Markdown和扩展Markdown简洁的语法代码块高亮图片链接和图片上传LaTex数学公式UML序列图和流程图离线写博客导入导出Markdown文件丰富的快捷键快捷键加粗 Ctrl + B 斜体 Ctrl + I 引用 Ctrl原创 2017-06-27 21:40:43 · 95 阅读 · 0 评论 -
面试题34—丑数
题目:把只包含因子2,3,5的数称为丑数。求第1500个丑数。数字1为第一个丑数。代码示例:#includeusing namespace std;//丑数:只包含2、3、5的因子。按从小到大的顺序求第1500个丑数。int Min(int x, int y, int z){ int temp = x < y ? x : y; int res = temp < z ? temp原创 2017-06-27 10:45:05 · 227 阅读 · 0 评论 -
面试题33—把数组排成最小的数
题目:输入一个正整数数组,把数组里面所有的数字拼接排成一个数,打印能拼接的所有数中最小的一个。代码示例:#include#include#include#include using namespace std;vector res;bool CompareStr(const string &str1, const string &str2){ string s1 = st原创 2017-06-27 10:38:06 · 301 阅读 · 0 评论 -
面试题32—从1到n整数中1出现的次数
题目:输入一个整数,求从1到n这n个数的十进制中1出现的次数。代码示例:#includeusing namespace std;int NumOfOne(int n){ if (n <= 0) return 0; int n_copy = n; int NumOfBit = 0; while (n_copy) { NumOfBit++; n_copy /= 10原创 2017-06-27 10:30:28 · 248 阅读 · 0 评论 -
面试题31—连续子数组的最大和
题目:输入一个整型数组,数组里面有正数也有负数,数组中连续一个或者多个组成一个子数组。求所有子数组的最大值。代码示例:#includeusing namespace std;bool flag = true;int MaxSubSum(int a[], int n, int &begin, int &end){ if (a == NULL || n <= 0) { fl原创 2017-06-27 10:04:07 · 202 阅读 · 0 评论 -
面试题30—最小的k个数
题目:输入n个整数,找出其中的最小k个数。代码示例:#include#includeusing namespace std;void SwapTwoNum(int &a, int &b){ int temp = a; a = b; b = temp;}int Partion(int a[], int length, int start, int end){ if原创 2017-06-27 09:48:55 · 218 阅读 · 0 评论 -
面试题29—数组中出现次数超过一半的数字
题目:数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。代码示例:#includeusing namespace std;bool MoreThanHalf(int a[], int n, int &res){ if (a == NULL || n <= 0) return false; int times = 1; res = a[0]; for (i原创 2017-06-27 09:47:47 · 175 阅读 · 0 评论 -
面试题37—两个链表的第一个公共结点
**题目:输入两个链表,找出他们第一个公共结点。 代码示例:**#include<iostream>using namespace std;struct Node{ int data; Node *next;};class List{ Node *head;public: List() { head = NULL; }原创 2017-06-27 21:49:49 · 179 阅读 · 0 评论 -
面试题38—数字在排序数组中出现的次数
代码示例:#include<iostream>using namespace std;int First_k(int a[], int s, int t,int k){ if (a == NULL || (s > t)) return -1; int mid = (s + t) / 2; if (a[mid] < k) { re原创 2017-06-27 21:50:31 · 218 阅读 · 0 评论 -
面试题39—二叉树的深度
**题目:输入一棵二叉树的根节点,求该树的深度。根节点到叶节点形成一条路径,最长路径即为深度。 代码示例:** 头文件:BinaryTree.h#pragma once#include<iostream>struct BinaryTreeNode{ int m_nValue; BinaryTreeNode* m_pLef原创 2017-06-27 21:52:46 · 195 阅读 · 0 评论 -
面试题49—把字符串转化成整数
代码示例:#include<iostream>using namespace std;bool flag;int StrToInt(const char *str){ int num = 0; flag = false; if (str == NULL || *str == '\0')//处理空指针和空串 { return num; }原创 2017-06-28 10:11:43 · 295 阅读 · 0 评论 -
面试题47—不用加减乘除做加法
代码示例:#include<iostream>using namespace std;int main(){ int m, n; cout << "输入两个加数:"; cin >> m >> n; int res = m^n; int res_and = m&n; while (res_and) { int temp0 = r原创 2017-06-27 22:02:11 · 151 阅读 · 0 评论 -
面试题46—求1+2+3...+n
**题目:如题,不允许采用循环、if条件、switch、乘除法。 代码示例:**#include<iostream>using namespace std;class MyClass{public: MyClass() { N++; Sum += N; } ~MyClass() { } friend un原创 2017-06-27 22:01:31 · 244 阅读 · 0 评论 -
面试题45—圆圈中最后剩下的数字
**题目:约瑟夫环问题 代码示例:**#include<iostream>using namespace std;const int MaxSize = 100;class MyQueue{ int data[MaxSize]; int front; int rear;public: MyQueue(){ front = 0; rear = 0; }原创 2017-06-27 22:00:41 · 241 阅读 · 0 评论 -
面试题目44—扑克牌的顺子
**题目:从扑克牌中随机抽五张牌,判断是不是一个顺子,即这五张牌是不是连续的。2—10为数字本身,A为1,J为11,Q为12,K为13,大小王可以看成任意数字。 代码示例:**#include<iostream>#include<time.h>using namespace std;const int Num = 14;struct MyStruct{ int n; ch原创 2017-06-27 21:59:59 · 180 阅读 · 0 评论 -
面试题43—n个色子的点数
**题目:把n个色子扔在地上,所有色子朝上一面的点数之和为s。输入n,打印出s的所有可能的值出现的概率。 代码示例:**#include<iostream>#include<cmath>using namespace std;const int maxvalue = 6;void PrintNum(int num){ if (num < 1) return;原创 2017-06-27 21:59:18 · 334 阅读 · 0 评论 -
面试题42—相关题目(字符串的左旋转)
**题目:字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。 代码示例(需要改进,参考前题):**#include<iostream>#include<string>using namespace std;bool LeftCircleShift(string &str, int n){ if (str == ""||n<0) return false原创 2017-06-27 21:58:32 · 187 阅读 · 0 评论 -
面试题42—翻转单词顺序
**题目:输入一个英文句子,翻转句子中单词的顺序,但是单词内字符的顺序不变。为简单起见,标点符号和普通字母一样处理。 代码示例:**#include<iostream>#include<string>using namespace std;bool InverseStence(string &str)//翻转句子{ if (str == "") return fals原创 2017-06-27 21:57:33 · 260 阅读 · 0 评论 -
面试题41—相关题目(和为s的所有连续正数序列)
**题目:输入正数s,求所有和为s的正数连续序列,至少两个数字。 代码示例:**#include<iostream>using namespace std;int main(){ int s; cout << "输入正数s:"; cin >> s; int small = 1; int big = 2; int limit = (s + 1)原创 2017-06-27 21:56:36 · 247 阅读 · 0 评论 -
面试题41—和为s的两个数
**题目:输入一个递增排序的数组和一个数字s,在数组中查找两个数使得他们的和正好是s。如果有多对,输出任意一对即可。 代码示例:**#include<iostream>using namespace std;bool SumIsS(int a[], int n, int s,int &num1, int &num2){ if (a == NULL || n <= 1)原创 2017-06-27 21:55:55 · 195 阅读 · 0 评论 -
面试题40—数组只出现一次的数字
**题目:一个数组除了两个数字以外,其余的都出现了两次,请找出这两个数字。 代码示例:**#include<iostream>using namespace std;int FindFirst_1_FromRight(int n)//找一个数字的二进制最右边为1的序号,其中n不为零{ int flag = 1; int index = 1; while (!(n&fl原创 2017-06-27 21:55:05 · 185 阅读 · 0 评论 -
面试题39—相关题目(判断平衡二叉树)
**题目:输入一颗二叉树的根节点,判断该树是不是平衡二叉树。 代码示例:**#include "BinaryTree.h"#include<iostream>using namespace std;// ====================方法1====================int TreeDepth(BinaryTreeNode* pRoot){ if (pRoo原创 2017-06-27 21:53:59 · 639 阅读 · 0 评论 -
面试题28—相关题目
题目:八皇后问题、代码示例:#include#include#include#includeusing namespace std;int num = 0;void swap(int &c1, int &c2){ int temp = c1; c1 = c2; c2 = temp;}void PrintAllKinds(int a[], int from, in原创 2017-06-27 09:46:53 · 166 阅读 · 0 评论 -
面试题28—相关题目
题目:输入一个含有8个数字的数组,判断有没有可能把这8个数字分别放到正方体8个顶点,使得正方体上三组相对的面上的4个顶点的和相等。代码示例:#include#include#includeusing namespace std;void swap(int &c1,int &c2){ int temp = c1; c1 = c2; c2 = temp;}void P原创 2017-06-27 09:46:09 · 243 阅读 · 0 评论 -
面试题28—相关题目(字符所有的组合)
题目:求字符的所有组合。代码示例:#include #include #include #include using namespace std; void Combination(char *string, int number, vector &result);void Combination(char *string){ assert(string !=原创 2017-06-27 09:45:08 · 170 阅读 · 0 评论 -
面试题13—在O(1)时间删除链表结点
题目:给定单链表的头指针和一个结点指针,在O(1)时间删除该结点。代码示例:#includeusing namespace std;template struct Node{ T data; Node *next;};template class LinkList{ Node *head;public: LinkList() { head = new Nod原创 2017-06-26 14:45:42 · 186 阅读 · 0 评论 -
面试题12—相关题目(任意两个整数相加)
题目:定义一个实现任意两个整数的加法函数。代码示例:#includeusing namespace std;char *BigNunAdd(char *str1, char *str2){ int len1 = strlen(str1); int len2 = strlen(str2); int len = len1 > len2?len1 : len2; char *res原创 2017-06-26 14:43:41 · 1115 阅读 · 0 评论 -
面试题12—打印1到最大的n位数
题目:输入数字n,按顺序打印出从1到最大的n位十进制数。代码示例:#includeusing namespace std;int FirstIndexNoZero(int a[], int n){ for (int i = 0; i < n; i++) { if (a[i] != 0) return i; } return -1;}void PrintFromo原创 2017-06-26 14:31:53 · 188 阅读 · 0 评论 -
面试题11—数值的整数次方
题目:实现函数double Power(double num,int n),求num的n次方,不得使用库函数,不需要考虑大数问题。代码示例:#includeusing namespace std;const double eps = 0.00000001;long double Power(double num, int n){ if (n > 0) { long doub原创 2017-06-26 13:57:19 · 170 阅读 · 0 评论 -
面试题目10—相关题目
题目:输入两个整数m和n,计算需要改变m的二进制中的多少位才能得到n。巧解:先异或,载统计异或结果中1的个数。代码示例(采用的蛮办法。。。):#includeusing namespace std;//改变m的二进制中的多少位可以得到n-----巧解:m和n异或,再求1的个数int ChangeNumBits(int m, int n){ int count = 0; un原创 2017-06-26 13:30:27 · 201 阅读 · 0 评论 -
面试题10—二进制中1的个数
题目:输入一个整数,输出该整数二进制中1的个数。代码示例:#includeusing namespace std;//该题也可采用常规解法,循环次数较多.void main(){ int x = 0x80000000; int n = 0; while (x != 0) { x = x&(x - 1); n++; } cout << n << "的二进制1的个数原创 2017-06-26 13:20:59 · 148 阅读 · 0 评论 -
面试题9—斐波那列数列
题目:f(n)=0;(n=0)1;(n=1)f(n-1)+f(n-2);(n>1)代码示例:#includeusing namespace std;long long Fabi(int n){ if (n == 0) return 0; if (n == 1) return 1; long long pre1 = 1; long long pre2原创 2017-06-26 13:01:02 · 709 阅读 · 0 评论 -
面试题7—相关题目(两个队列实现栈)
代码示例:#include#includeusing namespace std;//用两个队列模拟栈的操作class CStack{public: CStack() { } ~CStack() { } void PushStack(int data); bool PopStack(int &temp);private: queueq1; queueq2原创 2017-06-26 11:24:33 · 213 阅读 · 0 评论 -
面试题5—从尾到头打印链表
题目:输入一个链表的头结点,从尾到头反过来打印每个节点的值。代码示例(用栈也行):#includeusing namespace std;struct Node{ int data; Node *next;};class LinkList{ Node *head;public: LinkList(); ~LinkList(); bool CreateList(in原创 2017-06-26 10:17:52 · 150 阅读 · 0 评论