剑指offer-面试题66-构建乘积数组-发散思维
摘要:/* 题目: 链接:https://www.nowcoder.com/questionTerminal/94a4d381a68b47b7a8bed86f2975db46 来源:牛客网 给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1], 其中B中的元素B[i]=A[
阅读全文
posted @
2020-01-02 20:20
笨宝宝
阅读(143)
推荐(0)
剑指offer-面试题65-不用加减乘除做加法-位运算
摘要:/* 题目: 在不使用加减乘除的前提下,计算两个整数之和。 思路: 不能使用加减乘除则只能考虑位运算。 x=num1^num2,则为抹掉进位的结果。 y=num1&num2,为只有进位的结果。 (y<<1)&x,直到不产生进位。 */ #include<iostream> #include<cstr
阅读全文
posted @
2020-01-02 19:50
笨宝宝
阅读(166)
推荐(0)
剑指offer-面试题63-股票的最大利润-数组
摘要:/* 题目: 给定一个股价序列,求一次交易的最大利润。 */ #include<iostream> #include<vector> using namespace std; int MaxProfit(vector<int> numbers){ int length = numbers.size(
阅读全文
posted @
2020-01-01 23:44
笨宝宝
阅读(131)
推荐(0)
剑指offer-面试题62-圆圈中最后剩下的数字-约瑟夫环-解法2
摘要:/* 题目: 约瑟夫环问题。 思路: 数学规律 f(n)=0(n=1),[f(n-1,m)+m]%n(n>1) */ #include<iostream> #include<list> using namespace std; int LastRemaining(unsigned int n,uns
阅读全文
posted @
2020-01-01 23:16
笨宝宝
阅读(154)
推荐(0)
剑指offer-面试题62-圆圈中最后剩下的数字-约瑟夫环-解法1
摘要:/* 题目: 约瑟夫环问题。 思路: 用链表list实现,注意判断链表在末尾时,使其指向开头, 每次循环删除一个数字,直到只剩下一个数字。 */ #include<iostream> #include<list> using namespace std; int LastRemaining(unsi
阅读全文
posted @
2020-01-01 22:11
笨宝宝
阅读(134)
推荐(0)
剑指offer-面试题61-扑克牌中的顺子-数组
摘要:/* 题目: 从扑克牌中随机抽取n个数字,判断他们是否连续,扑克牌从A~K,大小王可代替任意数字。 */ #include<iostream> #include<cstdlib> #include<stack> #include<cstring> #include<vector> #include<
阅读全文
posted @
2019-12-29 21:26
笨宝宝
阅读(130)
推荐(0)
剑指offer-面试题60-n个骰子的点数-动态规划
摘要:/* 题目: 计算n个骰子,出现和s的概率。 */ #include<iostream> #include<cstdlib> #include<stack> #include<cstring> #include<vector> #include<deque> #include<cmath> usin
阅读全文
posted @
2019-12-29 18:07
笨宝宝
阅读(269)
推荐(0)
剑指offer-面试题59_2-队列的最大值-队列
摘要:/* 题目: 定义一个含max函数的队列类,并实现pop_front()、push_back()、max()函数。 */ #include<iostream> #include<cstdlib> #include<stack> #include<string> #include<vector> #i
阅读全文
posted @
2019-12-29 15:17
笨宝宝
阅读(166)
推荐(0)
剑指offer-面试题59_1-滑动窗口的最大值-数组
摘要:/* 题目: 链接:https://www.nowcoder.com/questionTerminal/1624bc35a45c42c0bc17d17fa0cba788 来源:牛客网 给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。例如,如果输入数组{2,3,4,2,6,2,5,1}及
阅读全文
posted @
2019-12-29 14:36
笨宝宝
阅读(150)
推荐(0)
剑指offer-面试题64-求1+2+...+n-发散思维
摘要:/* 题目: 求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C) */ /* 思路: 递归。 */ #include<iostream> #include<cstring> #include<vector> #
阅读全文
posted @
2019-12-27 21:25
笨宝宝
阅读(158)
推荐(0)
剑指offer-面试题58_2-左旋转字符串-字符串
摘要:/* 题目: 将字符串的前sep个字符转移到字符串尾部。 */ /* 思路: 更好的方法: 先翻转前sep个字符,再翻转后面的字符,最后全体翻转。 */ #include<iostream> #include<cstring> #include<vector> #include<algorithm>
阅读全文
posted @
2019-12-27 20:58
笨宝宝
阅读(155)
推荐(0)
剑指offer-面试题58_1-翻转单词顺序-字符串
摘要:/* 题目: 输入一个英文句子,翻转单词顺序,但单词内部顺序不变。 */ /* 思路: 先翻转整个句子,再将每个单词分别翻转一次。 */ #include<iostream> #include<cstring> #include<vector> #include<algorithm> #includ
阅读全文
posted @
2019-12-27 20:40
笨宝宝
阅读(114)
推荐(0)
剑指offer-面试题57_2-和为s的连续正数序列-穷举法
摘要:/* 题目: 输入一个整数s,输出所有和为s的连续整数序列。 */ /* 思路: 穷举法。 */ #include<iostream> #include<cstring> #include<vector> #include<algorithm> #include<map> using namespa
阅读全文
posted @
2019-12-27 17:45
笨宝宝
阅读(307)
推荐(0)
剑指offer-面试题57_1-和为s的两个数字-双指针
摘要:/* 题目: 输入一个递增数组和一个s,求和等于s的两个数组中的数字。 */ /* 思路: 双指针问题。 */ #include<iostream> #include<cstring> #include<vector> #include<algorithm> #include<map> using
阅读全文
posted @
2019-12-27 17:15
笨宝宝
阅读(143)
推荐(0)
剑指offer-面试题56_2-数组中唯一只出现一次的数字-位运算
摘要:/* 题目: 数组中除一个数字只出现一次外,其余数字都出现3次。 */ /* 思路: 位运算。 */ #include<iostream> #include<cstring> #include<vector> #include<algorithm> #include<map> using names
阅读全文
posted @
2019-12-27 16:56
笨宝宝
阅读(175)
推荐(0)
剑指offer-面试题56_1-数组中只出现一次的两个数字-位运算
摘要:/* 题目: 求数组A中只出现一次的数字,该数组中有2个数字a、b仅出现一次,其余均出现两次 */ /* 思路: 两个相同的数字异或为0. 遍历数组,得到数组中各数字异或后的结果x,结果x=a^b。 x中肯定存在一位不为0,找到左起第一位为1的位。 根据该位,将数组分为两拨,再进行异或,得到的结果即
阅读全文
posted @
2019-12-25 18:02
笨宝宝
阅读(151)
推荐(0)
剑指offer-面试题55-平衡二叉树-递归
摘要:/* 题目: 判断二叉树是否为平衡二叉树。 */ /* 思路: 判断二叉树的左子树和右子树高度相差是否为1. */ #include<iostream> #include<cstring> #include<vector> #include<algorithm> #include<map> usin
阅读全文
posted @
2019-12-25 17:07
笨宝宝
阅读(172)
推荐(0)
剑指offer-面试题55-二叉树的深度-递归
摘要:/* 题目: 二叉树的深度 */ /* 思路: 根节点高度(0或1)+左子树的深度+右子树的深度 */ #include<iostream> #include<cstring> #include<vector> #include<algorithm> #include<map> using name
阅读全文
posted @
2019-12-25 16:10
笨宝宝
阅读(106)
推荐(0)
剑指offer-面试题54-二叉搜索树的第k大节点-中序遍历
摘要:/* 题目: 求二叉搜索树的第k大节点。 */ /* 思路: 中序遍历。 */ #include<iostream> #include<cstring> #include<vector> #include<algorithm> #include<map> using namespace std; s
阅读全文
posted @
2019-12-23 22:24
笨宝宝
阅读(241)
推荐(0)
剑指offer-面试题53_3-数组中数值和下标相等的元素-二分查找
摘要:/* 题目: 求单调递增数组中,数值与下标相等的任意数字。 */ /* 思路: 二分法。 */ #include<iostream> #include<cstring> #include<vector> #include<algorithm> #include<map> using namespac
阅读全文
posted @
2019-12-23 21:45
笨宝宝
阅读(137)
推荐(0)