- 博客(166)
- 资源 (1)
- 收藏
- 关注
转载 树中两个结点的最低公共祖先
struct TreeNode{ int val; TreeNode* left; TreeNode* right; TreeNode* m_vChildren;};TreeNode* GetLastCommonParent(TreeNode* pRoot, TreeNode* pNode1, TreeNode* pNode2){ if (pRoot == nullptr |...
2018-10-06 22:17:05
232
转载 把字符串转换成整数
请你写一个函数StrToInt,实现把字符串转换成整数这个功能,不能使用atoi或者其他类似的库函数。好多输入测试。。。int StrToInt(const char* str){ long long num = 0; g_nStatus = kInvalid; if (str != nullptr&&*str != '\0') { bool minus ...
2018-10-06 21:26:56
219
原创 构建乘积数组
题目:给定一个数组A[0, 1, …, n-1],请构建一个数组B[0, 1, …, n-1],其中B中的元素B[i] =A[0]×A[1]×… ×A[i-1]×A[i+1]×…×A[n-1]。不能使用除法。 void multiply(vector<int> &array1, vector<int> &array2){ int length1...
2018-10-06 20:26:41
245
原创 不用加减乘除做加法
写一个函数,求两个整数之和,要求在函数体内不得使用+、-、×、÷四则运算符号。 int Add(int num1, int num2){ int sum; int carry; do { sum = (num1^num2); carry = (num1&num2) << 1; num1 = sum; num2 = carry; } whil...
2018-10-06 19:34:38
199
转载 求1+2+…+n
题目:求1+2+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。//用构造函数求解class Temp{public: Temp() { ++N; Sum += N; } static void Reset() { N = 0; Sum = 0; } static unsigned...
2018-10-06 18:38:25
1254
翻译 股票的最大利润
题目:假设把某股票的价格按照时间先后顺序存储在数组中,请问买卖交易该股票可能获得的利润是多少?例如一只股票在某些时间节点的价格为{9, 11, 8, 5,7, 12, 16, 14}。如果我们能在价格为5的时候买入并在价格为16时卖出,则能收获最大的利润11。 int MaxDiff(const int* numbers,unsigned int length){ if (numbe...
2018-10-06 17:27:46
178
原创 圆圈中最后剩下的数字
题目:0, 1, …, n-1这n个数字排成一个圆圈,从数字0开始每次从这个圆圈里删除第m个数字。求出这个圆圈里剩下的最后一个数字。方法:用环形链表模拟圆圈int lastRemaining(int n, int m){ if (n < 1 || m < 1) return -1; unsigned int i = 0; list<int> numb...
2018-10-06 17:15:54
153
转载 扑克牌中的顺子
题目:从扑克牌中随机抽5张牌,判断是不是一个顺子,即这5张牌是不是连续的。2~10为数字本身,A为1,J为11,Q为12,K为13,而大、小王可以看成任意数字。把0看成大小王int compare(const void* arg1, const void* arg2){ return *(int*)arg1 - *(int*)arg2;}bool isContinuous(int...
2018-10-06 16:26:03
263
翻译 队列的最大值
定义一个队列并实现函数max得到队列最大值,要求函数max,push_back和pop_front的时间复杂度都是O(1)template<typename T>class QueueWithMax{public: QueueWithMax() :currentIndex(0) {} void push_back(T number) { while (!maxnu...
2018-10-06 11:50:17
404
转载 滑动窗口的最大值
题目:给定一个数组和滑动窗口的大小,请找出所有滑动窗口里的最大值。例如,如果输入数组{2, 3, 4, 2, 6, 2, 5, 1}及滑动窗口的大小3,那么一共存在6个滑动窗口,它们的最大值分别为{4, 4, 6, 6, 6, 5},三种方法:暴力法,最大堆法,双向队列法//简单方法,时间复杂度O(nW)int getMax(const int A[], int size){ in...
2018-10-06 11:11:02
179
转载 数组中数值和下标相等的元素
题目:假设一个单调递增的数组里的每个元素都是整数并且是唯一的。请编程实现一个函数找出数组中任意一个数值等于其下标的元素。例如,在数组{-3, -1,1, 3, 5}中,数字3和它的下标相等。二分法:int getNumberSameAsIndex(const int* numbers, int length){ if (numbers == nullptr || length <...
2018-10-05 17:36:58
371
翻译 0~n-1中缺失的数字
题目:一个长度为n-1的递增排序数组中的所有数字都是唯一的,并且每个数字都在范围0到n-1之内。在范围0到n-1的n个数字中有且只有一个数字不在该数组中,请找出这个数字。int getMissingNumber(const int* numbers, int length){ if (numbers == nullptr || length <= 0) return -1;...
2018-10-05 17:21:24
145
转载 数字在排序数组中出现的次数
题目:统计一个数字在排序数组中出现的次数。例如输入排序数组{1, 2, 3, 3,3, 3, 4, 5}和数字3,由于3在这个数组中出现了4次,因此输出4。int GetNumberOfK(int* data, int length, int k){ int number = 0; if (data != nullptr || length > 0) { int first...
2018-10-05 17:06:35
113
翻译 两个单链表的第一个公共结点
题目:输入两个链表,找出它们的第一个公共结点。ListNode* FindFirstCommonNode(ListNode* pHead1, ListNode* pHead2){ unsigned int nLength1 = getListLength(pHead1); unsigned int nLength2 = getListLength(pHead2); int nLen...
2018-10-05 15:55:28
175
转载 数组中的逆序对
题目:在数组中的两个数字如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数。基于归并排序//基于归并排序int InversePair(int *data, int length){ if (data == nullptr || length <= 0) return 0; int *copy = new int[l...
2018-10-05 15:14:48
150
转载 字符流中第一个只出现一次的字符
题目:请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是'g'。当从该字符流中读出前六个字符"google"时,第一个只出现一次的字符是'l'。//哈希表class CharStatistics{public: CharStatistics() :index(0) { for (int i = 0;i &...
2018-10-05 13:33:37
188
翻译 第一个只出现一次的字符
题目:在字符串中找出第一个只出现一次的字符。如输入"abaccdeff",则输出'b'。//哈希表 char FirstNotRepeatingChar(char *pString){ if(pString==nullptr) return '\0'; const int hashSize = 256; unsigned int hashTable[hashSize]; fo...
2018-10-05 12:48:50
137
原创 丑数
题目:我们把只包含因子2、3和5的数称作丑数(Ugly Number)。求按从小到大的顺序的第1500个丑数。例如6、8都是丑数,但14不是,因为它包含因子7。习惯上我们把1当做第一个丑数。两种方法:暴力求解和空间换时间//直接暴力求解int getUglyNumber(int index){ if (index <= 0) return 0; int number =...
2018-10-05 11:55:13
156
原创 最长不重复字符的子字符串
题目:请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字符串的长度。假设字符串中只包含从'a'到'z'的字符。动态规划//动态规划int longestSubstingWithoutDuplication(const std::string &str){ int curLength = 0; int maxLength = 0; int* positio...
2018-10-05 11:07:48
285
原创 礼物的最大价值
题目:在一个m×n的棋盘的每一格都放有一个礼物,每个礼物都有一定的价值(价值大于0)。你可以从棋盘的左上角开始拿格子里的礼物,并每次向左或者向下移动一格直到到达棋盘的右下角。给定一个棋盘及其上面的礼物,请计算你最多能拿到多少价值的礼物?//动态规划,优化前int getMaxValue(const int*values, int rows, int cols){ if (values ...
2018-10-05 10:10:28
116
原创 把数字翻译成字符串
题目:给定一个数字,我们按照如下规则把它翻译为字符串:0翻译成"a",1翻译成"b",……,11翻译成"l",……,25翻译成"z"。一个数字可能有多个翻译。例如12258有5种不同的翻译,它们分别是"bccfi"、"bwfi"、"bczi"、"mcfi"和"mzi"。请编程实现一个函数用来计算一个数字有多少种不同的翻译方法。
2018-10-04 22:34:42
188
1
原创 从1到n整数中1出现的次数
题目:输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数。例如输入12,从1到12这些整数中包含1 的数字有1,10,11和12,1一共出现了5次。#include<iostream>using namespace std;int countOfIntegers(int digits);int digitOfInteger(int index, int di...
2018-09-19 13:21:50
87
转载 1~n中整数中1出现的次数
#include<iostream>using namespace std;int count(int n){ if (n < 1) return 0; int base = 1; int count = 0; int round = n; while (round) { int weight = round % 10; round /=...
2018-09-19 11:14:51
96
转载 连续子数组的最大和
题目:输入一个整型数组,数组里有正数也有负数。数组中一个或连续的多个整数组成一个子数组。求所有子数组的和的最大值。要求时间复杂度为O(n)。#include <functional>using namespace std;//定义一个全局变量标记输入是否无效bool g_InvalidInput = false;int FindGreatestSumOfSubArra...
2018-09-19 10:05:37
161
转载 数据流中的中位数
题目:如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值。如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值。#include<iostream>#include<algorithm>#include<vector>#include <functional>...
2018-09-19 09:34:50
197
原创 最小的k个数字
题目:输入n个整数,找出其中最小的k个数。例如输入4、5、1、6、2、7、3、8这8个数字,则最小的4个数字是1、2、3、4。方法一:根据数组特点找出时间复杂度为O(n)的算法#include<iostream>#include<algorithm>using namespace std;int RandomOfRange(int start, int e...
2018-09-18 17:27:29
433
原创 数组中出现次数超过一半的数字
方法一:基于Partition函数的时间复杂度为O(n)的算法#include<iostream>#include<algorithm>using namespace std;bool CheckInvalidArray(int* numbers, int length);bool CheckMoreThanHalf(int* numbers, int le...
2018-09-18 15:01:55
110
原创 Set Matrix Zeroes
先扫描第一行第一列,如果有0,则将各自的flag设置为true- 然后扫描除去第一行第一列的整个数组,如果有0,则将对应的第一行和第一列的数字赋0- 再次遍历除去第一行第一列的整个数组,如果对应的第一行和第一列的数字有一个为0,则将当前值赋0- 最后根据第一行第一列的flag来更新第一行第一列class Solution{public: void setZeroes(vector...
2018-09-13 00:46:06
85
原创 字符串的排列
题目:输入一个字符串,打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a、b、c所能排列出来的所有字符串abc、acb、bac、bca、cab和cba,用递归。#include <cstdio>void Permutation(char* pStr, char* pBegin);void Permutation(char* pStr){//判断输入是否...
2018-09-12 22:32:30
75
原创 二叉搜索树与双向链表
题目:输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。#include<iostream>using namespace std;struct BinaryTreeNode{ int val; BinaryTreeNode* left; BinaryTreeNode* right;};void...
2018-09-12 13:04:15
94
原创 复杂链表的复制
题目:请实现函数ComplexListNode* Clone(ComplexListNode* pHead),复制一个复杂链表。在复杂链表中,每个结点除了有一个m_pNext指针指向下一个结点外,还有一个m_pSibling 指向链表中的任意结点或者nullptr。#include<iostream>using namespace std;//链表结构struct Com...
2018-09-12 10:38:26
85
原创 leetcode:Gray Code
公式法1:n ⊕ (n/2) class Solution{public: vector<int> grayCode(int n) { vector<int> result; const size_t size = 1 << n; result.reserve(size); for (size_t i = 9;i < siz...
2018-09-12 00:45:51
128
原创 Leetcode:Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top有 f(n) = f(n - 1) + f(n - 2)方法一:迭代...
2018-09-11 23:55:59
107
原创 求二叉树中和为某个值的路径
#include <iostream>#include<vector>using namespace std;struct BinaryTreeNode{ int val; BinaryTreeNode* left; BinaryTreeNode* right;};void FindThePath(BinaryTreeNode* pRoot, i...
2018-09-11 22:05:00
130
原创 二叉搜索树的后序遍历序列
题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则返回true,否则返回false。假设输入的数组的任意两个数字都互不相同#include <iostream>using namespace std;//二叉搜索树特征是:根结点大于左子树结点,小于右子树结点,可以利用这个特点进行判断bool VerifySequenceOfBST(int se...
2018-09-11 21:07:15
84
原创 Plus One加一
方法一:class Solution{public: vector<int>plusOne(vector<int> &num) { int n = num.size();//若数位为9,则置0;否则加一返回该数 for (int i = n - 1;i >= 0;--i) { if (num[i] == 9) num[...
2018-09-11 00:37:13
137
原创 Rotate Image
You are given an n × n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up: Could you do this in-place?思路 1,时间复杂度 O(n^2),空间复杂度 O(1)将数组旋转90度,先沿着副对角线翻转,再沿着水平中线翻转c...
2018-09-11 00:10:29
94
原创 之子形打印二叉树
#include<iostream>#include<stack>using namespace std;struct BinaryTreeNode{ int m_dbValue; BinaryTreeNode* m_pLeft; BinaryTreeNode* m_pRight;};//创建...
2018-09-10 22:22:56
340
原创 分行从上到下打印二叉树
#include<iostream>#include<deque>using namespace std;struct BinaryTreeNode{ int m_dbValue; BinaryTreeNode* m_pLeft; BinaryTreeNode* m_pRight;};//创建...
2018-09-10 21:56:59
262
原创 从上到下打印二叉树
#include<iostream>#include<deque>using namespace std;struct BinaryTreeNode{ double m_dbValue; BinaryTreeNode* m_pLeft; BinaryTreeNode* m_pRight;};/...
2018-09-10 21:29:02
120
后台开发的一些源代码
2019-04-03
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人