二维数组中的查找
-
题目描述
在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
-
题解
class Solution {
public:
bool Find(int target, vector<vector<int> > array) {
int rowCount = array.size(), colCount = array[0].size();
for(int i = rowCount - 1, j = 0; i >= 0 && j < colCount;) {
if(array[i][j] == target) return true;
else if(target < array[i][j]) i--;
else j++;
}
return false;
}
};
替换空格
-
题目描述
请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
-
题解
class Solution {
public:
void replaceSpace(char *str,int length) {
int n = length, sn = 0;
for(int i = 0; str[i] != '\0'; i++) {
if(str[i] == ' ') sn++;
}
int nn = n + 2 * sn;
while(n >= 0 && nn > n) {
if(str[n] == ' ') {
str[nn--] = '0';
str[nn--] = '2';
str[nn--] = '%';
}
else {
str[nn--] = str[n];
}
n--;
}
}
};
从尾到头打印链表
-
题目描述
输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
-
题解
class Solution {
public:
vector<int> v;
vector<int> printListFromTailToHead(ListNode* head) {
if(head != NULL) {
printListFromTailToHead(head->next);
v.push_back(head->val);
}
return v;
}
};
重建二叉树
-
题目描述
输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。
-
题解
class Solution {
public:
TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) {
if(pre.size() == 0 || vin.size() == 0) return NULL;
TreeNode* n = new TreeNode(pre[0]);
int i = 0;
while(vin[i] != pre[0]) i++;
vector<int> p1(pre.begin()+1, pre.begin()+i+1);
vector<int> p2(pre.begin()+i+1, pre.end());
vector<int> v1(vin.begin(), vin.begin()+i);
vector<int> v2(vin.begin()+i+1, vin.end());
n->left = reConstructBinaryTree(p1, v1);
n->right = reConstructBinaryTree(p2, v2);
return n;
}
};
用两个栈实现队列
-
题目描述
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
-
题解
class Solution
{
public:
void push(int node) {
stack1.push(node);
}
int pop() {
if(stack2.size() == 0) {
while(!stack1.empty()) {
stack2.push(stack1.top());
stack1.pop();
}
}
int ans = stack2.top();
stack2.pop();
return ans;
}
private:
stack<int> stack1;
stack<int> stack2;
};
旋转数组的最小数字
-
题目描述
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
-
题解
class Solution {
public:
int minNumberInRotateArray(vector<int> rotateArray) {
int low = 0, high = rotateArray.size() - 1;
while(low < high) {
int mid = low + (high - low) / 2;
if(rotateArray[mid] > rotateArray[high]) low = mid + 1;
else if(rotateArray[mid] == rotateArray[high]) high--;
else if(rotateArray[mid] < rotateArray[high]) high = mid;
}
return rotateArray[low];
}
};
变态跳台阶
-
题目描述
一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
-
题解
class Solution {
public:
int jumpFloorII(int number) {
if(number <= 0) return -1;
else if(number == 1) return 1;
else return 2*jumpFloorII(number-1);
}
};
矩形覆盖
-
题目描述
我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?
-
题解
class Solution {
public:
int rectCover(int number) {
if(number == 0) return 0;
int a = 1, b = 1;
for(int i = 2; i <= number; i++) {
b = a + b;
a = b - a;
}
return b;
}
};
二进制中1的个数
-
题目描述
输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。
-
题解
class Solution {
public:
int NumberOf1(int n) {
int num = 0;
while(n) {
n = n & (n-1);
num++;
}
return num;
}
};
数值的整数次方
-
题目描述
给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
-
题解
class Solution {
public:
double Power(double base, int exponent) {
int e = exponent < 0 ? -exponent : exponent;
double ans = 1;
while(e) {
if(e&1) ans *= base;
e >>= 1;
base *= base;
}
return exponent < 0 ? 1 / ans : ans;
}
};
调整数组顺序使奇数位于偶数前面
-
题目描述
输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。
-
题解
class Solution {
public:
void reOrderArray(vector<int> &array) {
for(int i = 0; i < array.size(); i++) { //O(n2) 也可以开一个数组空间换时间
for(int j = array.size() - 1; j > i; j--) {
if(array[j] % 2 == 1 && array[j-1] % 2 == 0) {
swap(array[j], array[j-1]);
}
}
}
}
};
链表中倒数第k个结点
-
题目描述
输入一个链表,输出该链表中倒数第k个结点。
-
题解
class Solution {
public:
ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
if(pListHead == NULL || k == 0) return NULL;
ListNode *n1 = pListHead, *n2 = pListHead;
for(int i = 0; i < k-1; i++) {
if(n1->next != NULL)n1 = n1->next;
else return NULL;
}
while(n1->next != NULL) {
n1 = n1->next;
n2 = n2->next;
}
return n2;
}
};
反转链表
-
题目描述
输入一个链表,反转链表后,输出新链表的表头。
-
题解
//解法1
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
ListNode *pre = NULL, *next = NULL;
while(pHead != NULL) {
next = pHead->next;
pHead->next = pre;
pre = pHead;
pHead = next;
}
return pre;
}
};
//解法2
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
if(pHead == NULL || pHead->next == NULL) return pHead;
ListNode* last = ReverseList(pHead->next);
pHead->next->next = pHead;
pHead->next = NULL;
return last;
}
};
合并两个排序的链表
-
题目描述
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
-
题解
class Solution {
public:
ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
{
if(pHead1 == NULL) return pHead2;
if(pHead2 == NULL) return pHead1;
if(pHead1->val < pHead2->val) {
pHead1->next = Merge(pHead1->next, pHead2);
return pHead1;
}
else {
pHead2->next = Merge(pHead1, pHead2->next);
return pHead2;
}
}
};
树的子结构
-
题目描述
输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
-
题解
class Solution {
public:
bool isSubtree(TreeNode* a, TreeNode* b) { //判断两棵树是否相同
if(b == NULL) return true;
if(a == NULL) return false;
if(a->val != b->val) return false;
else return isSubtree(a->left, b->left) && isSubtree(a->right, b->right);
}
bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2) //判断A树中是否含有B树
{
if(pRoot1 == NULL || pRoot2 == NULL) return false;
return isSubtree(pRoot1, pRoot2) || HasSubtree(pRoot1->left, pRoot2) || HasSubtree(pRoot1->right, pRoot2);
}
};
二叉树的镜像
-
题目描述
操作给定的二叉树,将其变换为源二叉树的镜像。
-
输入描述:
二叉树的镜像定义:源二叉树
8
/ \
6 10
/ \ / \
5 7 9 11
镜像二叉树
8
/ \
10 6
/ \ / \
11 9 7 5
-
题解
class Solution {
public:
void Mirror(TreeNode *pRoot) {
if(pRoot){
swap(pRoot->left, pRoot->right);
Mirror(pRoot->left);
Mirror(pRoot->right);
}
}
};
包含min函数的栈
-
题目描述
定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。
-
题解
class Solution {
public:
stack<int> s1, s2;
void push(int value) {
s1.push(value);
if(s2.empty()) {
s2.push(value);
}
else if(value <= s2.top()) {
s2.push(value);
}
}
void pop() {
if(s1.top() == s2.top()) s2.pop();
s1.pop();//这里的顺序要特别注意一下
}
int top() {
return s1.top();
}
int min() {
return s2.top();
}
};
栈的压入、弹出序列
-
题目描述
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)
-
题解
class Solution {
public:
bool IsPopOrder(vector<int> pushV,vector<int> popV) {
if(pushV.size() == 0 || popV.size() == 0) return false;
stack<int> s;
for(int i = 0, j = 0; i < pushV.size(); i++) {
s.push(pushV[i]);
while(!s.empty() && s.top() == popV[j]) {
s.pop();
j++;
}
}
return s.empty();
}
};
二叉搜索树的后序遍历序列
-
题目描述
输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。
-
题解
class Solution {
public:
bool judge(vector<int> a, int l, int r) {
if(l >= r) return true;
int i = r - 1;
while(i >= l && a[i] > a[r]) i--;
for(int j = i; j >= l; j--) if(a[j] > a[r]) return false;
return judge(a, l, i - 1) && judge(a, i, r - 1);
}
bool VerifySquenceOfBST(vector<int> sequence) {
if(sequence.size() == 0) return false;
return judge(sequence, 0, sequence.size()-1);
}
};
二叉树中和为某一值的路径
-
题目描述
输入一颗二叉树的跟节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。(注意: 在返回值的list中,数组长度大的数组靠前)
-
题解
class Solution {
public:
vector<vector<int> > all;
vector<int> v;
vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {
if(root == NULL) return all;
v.push_back(root->val);
expectNumber -= root->val;
if(expectNumber == 0 && root->left == NULL && root->right == NULL) all.push_back(v);
FindPath(root->left, expectNumber);
FindPath(root->right, expectNumber);
v.pop_back();
return all;
}
};
复杂链表的复制
-
题目描述
输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)
-
题解
/*
*解题思路:
*1、遍历链表,复制每个结点,如复制结点A得到A1,将结点A1插到结点A后面;
*2、重新遍历链表,复制老结点的随机指针给新结点,如A1.random = A.random.next;
*3、拆分链表,将链表拆分为原链表和复制后的链表
*/
class Solution {
public:
RandomListNode* Clone(RandomListNode* pHead)
{
if(pHead == NULL) return pHead;
RandomListNode* now = pHead;
while(now != NULL) {
RandomListNode* clone = new RandomListNode(now->label);
RandomListNode* next = now->next;
now->next = clone;
clone->next = next;
now = next;
}
now = pHead;
while(now != NULL) {
now->next->random = now->random == NULL ? NULL : now->random->next;
now = now->next->next;
}
now = pHead;
RandomListNode* clone = pHead->next;
while(now != NULL) {
RandomListNode* cloneNode = now->next;
now->next = cloneNode->next;
cloneNode->next = cloneNode->next == NULL ? NULL : cloneNode->next->next;
now = now->next;
}
return clone;
}
};
二叉搜索树与双向链表
-
题目描述
输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。
-
题解
中序遍历即可。只需要记录一个pre指针即可。
class Solution {
public:
TreeNode* Convert(TreeNode* pRootOfTree)
{
if(pRootOfTree == nullptr) return nullptr;
TreeNode* pre = nullptr;
convertHelper(pRootOfTree, pre);
TreeNode* res = pRootOfTree;
while(res ->left) res = res ->left;
return res;
}
void convertHelper(TreeNode* cur, TreeNode*& pre)
{
if(cur == nullptr) return;
convertHelper(cur ->left, pre);
cur ->left = pre;
if(pre) pre ->right = cur;
pre = cur;
convertHelper(cur ->right, pre);
}
};
字符串的排列
-
题目描述
输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
-
题解
class Solution {
public:
set<string> st;
vector<string> ans;
vector<string> Permutation(string str) {
if(str != "") dfs(str, 0, str.length());
for(auto it = st.begin(); it != st.end(); it++) {
ans.push_back(*it);
}
return ans;
}
void dfs(string s, int cur, int n) {
if(cur == n) {
st.insert(s);
return ;
}
for(int i = cur; i < n; i++) {
swap(s[cur], s[i]);
dfs(s, cur+1, n);
swap(s[cur], s[i]);
}
}
};
数组中出现次数超过一半的数字
-
题目描述
数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。
-
题解
class Solution {
public:
int MoreThanHalfNum_Solution(vector<int> numbers) {
int n = numbers.size();
if(n == 0) return 0;
int number = numbers[0], count = 1;
for(int i = 1; i < n; i++) {
if(numbers[i] == number) count++;
else count--;
if(count < 0) {
number = numbers[i];
count = 1;
}
}
count = 0;
for(int i = 0; i < n; i++) {
if(numbers[i] == number) count++;
}
if(count * 2 > n) return number;
return 0;
}
};
最小的K个数
-
题目描述
输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。
-
题解
class Solution {
public:
vector<int> GetLeastNumbers_Solution(vector<int> input, int k) {
vector<int> ans;
if(k > input.size()) return ans;
priority_queue<int, vector<int>, greater<int> > q;
for(int i = 0; i < input.size(); i++) {
q.push(input[i]);
}
while(k--) {
ans.push_back(q.top());
q.pop();
}
return ans;
}
};
连续子数组的最大和
-
题目描述
HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学。今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决。但是,如果向量中包含负数,是否应该包含某个负数,并期望旁边的正数会弥补它呢?例如:{6,-3,-2,7,-15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止)。给一个数组,返回它的最大连续子序列的和,你会不会被他忽悠住?(子向量的长度至少是1)
-
题解
class Solution {
public:
int FindGreatestSumOfSubArray(vector<int> array) {
int sum = array[0], maxSum = array[0];
for(int i = 1; i < array.size(); i++) {
sum = max(array[i]+sum, array[i]);
maxSum = max(maxSum, sum);
}
return maxSum;
}
};
整数中1出现的次数(从1到n整数中1出现的次数)
-
题目描述
求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1、10、11、12、13因此共出现6次,但是对于后面问题他就没辙了。ACMer希望你们帮帮他,并把问题更加普遍化,可以很快的求出任意非负整数区间中1出现的次数(从1 到 n 中1出现的次数)。
-
题解
class Solution {
//主要思路:设定整数点(如1、10、100等等)作为位置点i(对应n的各位、十位、百位等等),分别对每个数位上有多少包含1的点进行分析
//根据设定的整数位置,对n进行分割,分为两部分,高位n/i,低位n%i
//当i表示百位,且百位对应的数>=2,如n=31456,i=100,则a=314,b=56,此时百位为1的次数有a/10+1=32(最高两位0~31),每一次都包含100个连续的点,即共有(a%10+1)*100个点的百位为1
//当i表示百位,且百位对应的数为1,如n=31156,i=100,则a=311,b=56,此时百位对应的就是1,则共有a%10(最高两位0-30)次是包含100个连续点,当最高两位为31(即a=311),本次只对应局部点00~56,共b+1次,所有点加起来共有(a%10*100)+(b+1),这些点百位对应为1
//当i表示百位,且百位对应的数为0,如n=31056,i=100,则a=310,b=56,此时百位为1的次数有a/10=31(最高两位0~30)
//综合以上三种情况,当百位对应0或>=2时,有(a+8)/10次包含所有100个点,还有当百位为1(a%10==1),需要增加局部点b+1
//之所以补8,是因为当百位为0,则a/10==(a+8)/10,当百位>=2,补8会产生进位位,效果等同于(a/10+1)
public:
int NumberOf1Between1AndN_Solution(int n)
{
int count = 0;
for(int i = 1; i <= n; i *= 10) {
int a = n/i, b = n%i;
count += (a+8)/10*i + (a%10==1)*(b+1);
}
return count;
}
};
把数组排成最小的数
-
题目描述
输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。
-
题解
class Solution {
public:
static int cmp(int a, int b) { //注意这里用到排序要加static
string s1 = to_string(a) + to_string(b), s2 = to_string(b) + to_string(a);
return s1 < s2;
}
string PrintMinNumber(vector<int> numbers) {
sort(numbers.begin(), numbers.end(), cmp);
string ans;
for(int i = 0; i < numbers.size(); i++) {
ans += to_string(numbers[i]);
}
return ans;
}
};
丑数
-
题目描述
把只包含质因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含质因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。
-
题解
class Solution {
public:
int GetUglyNumber_Solution(int index) {
if(index < 7) return index;
vector<int> v(index);
v[0] = 1;
int x2 = 0, x3 = 0, x5 = 0;
for(int i = 1; i < index; i++) {
v[i] = min(v[x2]*2, min(v[x3]*3, v[x5]*5));
if(v[x2]*2 == v[i]) x2++;
if(v[x3]*3 == v[i]) x3++;
if(v[x5]*5 == v[i]) x5++;
}
return v[index-1];
}
};
数组中的逆序对
-
题目描述
在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的结果输出。 即输出P%1000000007
-
题解
public class Solution {
int cnt;
public int InversePairs(int[] array) {
cnt = 0;
if (array != null)
mergeSortUp2Down(array, 0, array.length - 1);
return cnt;
}
/*
* 归并排序(从上往下)
*/
public void mergeSortUp2Down(int[] a, int start, int end) {
if (start >= end)
return;
int mid = (start + end) >> 1;
mergeSortUp2Down(a, start, mid);
mergeSortUp2Down(a, mid + 1, end);
merge(a, start, mid, end);
}
/*
* 将一个数组中的两个相邻有序区间合并成一个
*/
public void merge(int[] a, int start, int mid, int end) {
int[] tmp = new int[end - start + 1];
int i = start, j = mid + 1, k = 0;
while (i <= mid && j <= end) {
if (a[i] <= a[j])
tmp[k++] = a[i++];
else {
tmp[k++] = a[j++];
cnt += mid - i + 1; // core code, calculate InversePairs............
}
}
while (i <= mid)
tmp[k++] = a[i++];
while (j <= end)
tmp[k++] = a[j++];
for (k = 0; k < tmp.length; k++)
a[start + k] = tmp[k];
}
}
两个链表的第一个公共结点
-
题目描述
输入两个链表,找出它们的第一个公共结点。
-
题解
/*
找出2个链表的长度,然后让长的先走两个链表的长度差,然后再一起走
(因为2个链表用公共的尾部)
*/
class Solution {
public:
ListNode* FindFirstCommonNode( ListNode *pHead1, ListNode *pHead2) {
int len1 = findListLenth(pHead1);
int len2 = findListLenth(pHead2);
if(len1 > len2){
pHead1 = walkStep(pHead1,len1 - len2);
}else{
pHead2 = walkStep(pHead2,len2 - len1);
}
while(pHead1 != NULL){
if(pHead1 == pHead2) return pHead1;
pHead1 = pHead1->next;
pHead2 = pHead2->next;
}
return NULL;
}
int findListLenth(ListNode *pHead1){
if(pHead1 == NULL) return 0;
int sum = 1;
while(pHead1 = pHead1->next) sum++;
return sum;
}
ListNode* walkStep(ListNode *pHead1, int step){
while(step--){
pHead1 = pHead1->next;
}
return pHead1;
}
};
数字在排序数组中出现的次数
-
题目描述
统计一个数字在排序数组中出现的次数。
-
题解
class Solution {
public:
int GetNumberOfK(vector<int> data ,int k) {
return search(data, k+0.5) - search(data, k-0.5);
}
int search(vector<int> v, double k) {
int l = 0, r = v.size()-1;
while(l <= r) {
int m = l + (r-l)/2;
if(k < v[m]) r = m-1;
else l = m+1;
}
return l;
}
};
二叉树的深度
-
题目描述
输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
-
题解
class Solution {
public:
int TreeDepth(TreeNode* pRoot)
{
if(pRoot == NULL) return 0;
return max(TreeDepth(pRoot->left), TreeDepth(pRoot->right)) + 1;
}
};
平衡二叉树
-
题目描述
。输入一棵二叉树,判断该二叉树是否是平衡二叉树
-
题解
//自底向上
class Solution {
public:
bool IsBalanced_Solution(TreeNode* pRoot) {
return getDepth(pRoot) != -1;
}
int getDepth(TreeNode* root) {
if(root == NULL) return 0;
int left = getDepth(root->left);
if(left == -1) return -1;
int right = getDepth(root->right);
if(right == -1) return -1;
return abs(right - left) > 1 ? -1 : max(left, right) + 1;
}
};
//自顶向下 有重复 效率较差
public classSolution {
public boolean IsBalanced_Solution(TreeNode root) {
if(root == null) {
return true;
}
return Math.abs(maxDepth(root.left) - maxDepth(root.right)) <= 1 &&
IsBalanced_Solution(root.left) && IsBalanced_Solution(root.right);
}
private int maxDepth(TreeNode root) {
if(root == null) {
return 0;
}
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}
}
数组中只出现一次的数字
-
题目描述
一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。
-
题解
/*考虑过程:
首先我们考虑这个问题的一个简单版本:一个数组里除了一个数字之外,其他的数字都出现了两次。请写程序找出这个只出现一次的数字。
这个题目的突破口在哪里?题目为什么要强调有一个数字出现一次,其他的出现两次?我们想到了异或运算的性质:任何一个数字异或它自己都等于0 。也就是说,如果我们从头到尾依次异或数组中的每一个数字,那么最终的结果刚好是那个只出现一次的数字,因为那些出现两次的数字全部在异或中抵消掉了。
有了上面简单问题的解决方案之后,我们回到原始的问题。如果能够把原数组分为两个子数组。在每个子数组中,包含一个只出现一次的数字,而其它数字都出现两次。如果能够这样拆分原数组,按照前面的办法就是分别求出这两个只出现一次的数字了。
我们还是从头到尾依次异或数组中的每一个数字,那么最终得到的结果就是两个只出现一次的数字的异或结果。因为其它数字都出现了两次,在异或中全部抵消掉了。由于这两个数字肯定不一样,那么这个异或结果肯定不为0 ,也就是说在这个结果数字的二进制表示中至少就有一位为1 。我们在结果数字中找到第一个为1 的位的位置,记为第N 位。现在我们以第N 位是不是1 为标准把原数组中的数字分成两个子数组,第一个子数组中每个数字的第N 位都为1 ,而第二个子数组的每个数字的第N 位都为0 。
现在我们已经把原数组分成了两个子数组,每个子数组都包含一个只出现一次的数字,而其它数字都出现了两次。因此到此为止,所有的问题我们都已经解决。*/
class Solution {
public:
void FindNumsAppearOnce(vector<int> data,int* num1,int *num2) {
int temp = 0;
for(int i = 0; i < data.size(); i++) {
temp ^= data[i];
}
int index = findFirst(temp);
for(int i = 0; i < data.size(); i++) {
if(test(data[i], index)) num1[0] ^= data[i];
else num2[0] ^= data[i];
}
}
int findFirst(int temp) {
int index = 0;
while((temp & 1) == 0 && index < 32) {
temp >>= 1;
index++;
}
return index;
}
int test(int a, int index) {
return (a >> index) & 1 == 1;
}
};
和为S的连续正数序列
-
题目描述
小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100。但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数)。没多久,他就得到另一组连续正数和为100的序列:18,19,20,21,22。现在把问题交给你,你能不能也很快的找出所有和为S的连续正数序列? Good Luck!
-
输出描述:
输出所有和为S的连续正数序列。序列内按照从小至大的顺序,序列间按照开始数字从小到大的顺序
-
题解
class Solution {
public://双指针
vector<vector<int> > FindContinuousSequence(int sum) {
vector<vector<int> > ans;
int l = 1, r = 2;
while(l < r) {
int num = (r+l) * (r-l+1) / 2;
if(num == sum) {
vector<int> v;
for(int i = l; i <= r; i++) {
v.push_back(i);
}
ans.push_back(v);
l++;
}
else if(num < sum) r++;
else l++;
}
return ans;
}
};
和为S的两个数字
-
题目描述
输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。
-
输出描述:
对应每个测试案例,输出两个数,小的先输出。
-
题解
/*
数列满足递增,设两个头尾两个指针i和j,
若ai + aj == sum,就是答案(相差越远乘积越小)
若ai + aj > sum,aj肯定不是答案之一(前面已得出 i 前面的数已是不可能),j -= 1
若ai + aj < sum,ai肯定不是答案之一(前面已得出 j 后面的数已是不可能),i += 1
O(n)
*/
class Solution {
public:
vector<int> FindNumbersWithSum(vector<int> array,int sum) {
vector<int> ans;
int n = array.size();
int l = 0, r = n - 1;
while(l < r) {
if(array[l] + array[r] == sum) {
ans.push_back(array[l]);
ans.push_back(array[r]);
break;
}
else if (array[l] + array[r] < sum) l++;
else r--;
}
return ans;
}
};
左旋转字符串
-
题目描述
汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果,即“XYZdefabc”。是不是很简单?OK,搞定它!
-
题解
class Solution {
public:
string LeftRotateString(string str, int n) {
if(str.length() == 0) return str;
n %= str.length();
str += str.substr(0, n);
str.erase(0,n);
return str;
}
};
翻转单词顺序列
-
题目描述
牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上。同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思。例如,“student. a am I”。后来才意识到,这家伙原来把句子单词的顺序翻转了,正确的句子应该是“I am a student.”。Cat对一一的翻转这些单词顺序可不在行,你能帮助他么?
-
题解
class Solution {
public: //一个个字符处理
string ReverseSentence(string str) {
string ans, tmp;
for(int i = 0; i < str.length(); i++) {
if(str[i] == ' ') {
ans = " " + tmp + ans;
tmp = "";
}
else tmp += str[i];
}
if(tmp.size()) ans = tmp + ans;
return ans;
}
};
扑克牌顺子
-
题目描述
LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿!!“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子.....LL不高兴了,他想了想,决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12,K为13。上面的5张牌就可以变成“1,2,3,4,5”(大小王分别看作2和4),“So Lucky!”。LL决定去买体育彩票啦。 现在,要求你使用这幅牌模拟上面的过程,然后告诉我们LL的运气如何, 如果牌能组成顺子就输出true,否则就输出false。为了方便起见,你可以认为大小王是0。
-
题解
class Solution {
public:
bool IsContinuous( vector<int> numbers ) {
if(numbers.size() == 0) return false;
sort(numbers.begin(), numbers.end());
int zero = 0, sum = 0;
for(int i = 0; i < numbers.size() - 1; i++) {
if(numbers[i] == 0) zero++;
else if(numbers[i+1] == numbers[i]) return false;
else sum += numbers[i+1] - numbers[i] - 1;
}
return sum <= zero;
}
};
孩子们的游戏(圆圈中最后剩下的数)
-
题目描述
每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此。HF作为牛客的资深元老,自然也准备了一些小游戏。其中,有个游戏是这样的:首先,让小朋友们围成一个大圈。然后,他随机指定一个数m,让编号为0的小朋友开始报数。每次喊到m-1的那个小朋友要出列唱首歌,然后可以在礼品箱中任意的挑选礼物,并且不再回到圈中,从他的下一个小朋友开始,继续0...m-1报数....这样下去....直到剩下最后一个小朋友,可以不用表演,并且拿到牛客名贵的“名侦探柯南”典藏版(名额有限哦!!^_^)。请你试着想下,哪个小朋友会得到这份礼品呢?(注:小朋友的编号是从0到n-1)
-
题解
class Solution {
public:
int LastRemaining_Solution(int n, int m)
{
int* a = new int[n];
int i = -1, step = 0, count = 0;
while(count != n) {
i++;
if(i == n) i = 0;
if(a[i] == -1) continue;
step++;
if(step == m) {
a[i] = -1;
count++;
step = 0;
}
}
return i;
}
};
求1+2+3+...+n
-
题目描述
求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
-
题解
class Solution {
public:
int Sum_Solution(int n) {
int ans = n;
ans && (ans += Sum_Solution(n - 1));
return ans;
}
};
不用加减乘除做加法
-
题目描述
写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。
-
题解
/*
首先看十进制是如何做的: 5+7=12,三步走
第一步:相加各位的值,不算进位,得到2。
第二步:计算进位值,得到10. 如果这一步的进位值为0,那么第一步得到的值就是最终结果。
第三步:重复上述两步,只是相加的值变成上述两步的得到的结果2和10,得到12。
同样我们可以用三步走的方式计算二进制值相加: 5-101,7-111 第一步:相加各位的值,不算进位,得到010,二进制每位相加就相当于各位做异或操作,101^111。
第二步:计算进位值,得到1010,相当于各位做与操作得到101,再向左移一位得到1010,(101&111)<<1。
第三步重复上述两步, 各位相加 010^1010=1000,进位值为100=(010&1010)<<1。
继续重复上述两步:1000^100 = 1100,进位值为0,跳出循环,1100为最终结果。
*/
class Solution {
public:
int Add(int num1, int num2)
{
while(num2 != 0) {
int temp = num1 ^ num2;
num2 = (num1 & num2) << 1;
num1 = temp;
}
return num1;
}
};
把字符串转换成整数
-
题目描述
将一个字符串转换成一个整数(实现Integer.valueOf(string)的功能,但是string不符合数字要求时返回0),要求不能使用字符串转换整数的库函数。 数值为0或者字符串不是一个合法的数值则返回0。
-
题解
class Solution {
public:
int StrToInt(string str) {
int ans = 0, flag = 1, n = str.length();
if(str[0] == '-') flag = -1;
for(int i = (str[0] == '-' || str[0] == '+')?1:0; i < n; i++) {
if(str[i] < '0' || str[i] > '9') return 0;
ans = ans*10 + str[i]-'0';
}
return ans*flag;
}
};
数组中重复的数字
-
题目描述
在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。
-
题解
bool duplicate(int numbers[], int length, int* duplication) {
for(int i = 0; i < length; i++) {
if(numbers[i] < length) numbers[i] += length;
else {
duplication[0] = numbers[i] - length;
return true;
}
}
return false;
}
构建乘积数组
-
题目描述
给定一个数组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]。不能使用除法。
-
题解
class Solution {
public:
vector<int> multiply(const vector<int>& A) {
int n = A.size();
vector<int> B(n); //要加个(n)
B[0] = 1;
for(int i = 1; i < n; i++) B[i] = B[i-1] * A[i-1];
int tmp = 1;
for(int i = n-2; i >= 0; i--) {
tmp *= A[i+1];
B[i] *= tmp;
}
return B;
}
};
正则表达式匹配
-
题目描述
请实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但是与"aa.a"和"ab*a"均不匹配
-
题解
class Solution {
public:
bool match(char* str, char* pattern)
{
if(*str == '\0' && *pattern == '\0') return true;
if(*str != '\0' && *pattern == '\0') return false;
if(*(pattern+1) != '*') {
if(*str == *pattern || (*str != '\0' && *pattern == '.')) return match(str+1, pattern+1);
return false;
}
else {
if(*str == *pattern || (*str != '\0' && *pattern == '.')) return match(str, pattern+2) || match(str+1, pattern);
return match(str, pattern+2);
}
}
};
表示数值的字符串
-
题目描述
请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100","5e2","-123","3.1416"和"-1E-16"都表示数值。 但是"12e","1a3.14","1.2.3","+-5"和"12e+4.3"都不是。
-
题解
//https://www.nowcoder.com/questionTerminal/6f8c901d091949a5837e24bb82a731f2?f=discussion
class Solution {
public:
bool isNumeric(char* string)
{
bool sign = false, flag = false, hasE = false;
for(int i = 0; i < strlen(string); i++) {
if(string[i] == 'E' || string[i] == 'e') {
if(i == strlen(string)-1 || hasE) return false;
hasE = true;
}
else if(string[i] == '+' || string[i] == '-') {
if(sign == 0 && i > 0 && string[i-1] != 'E' && string[i-1] != 'e') return false;
if(sign && string[i-1] != 'E' && string[i-1] != 'e') return false;
sign = true;
}
else if(string[i] == '.') {
if(flag || hasE) return false;
flag = true;
}
else if(string[i] < '0' || string[i] > '9') return false;
}
return true;
}
};
字符流中第一个不重复的字符
-
题目描述
请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。
-
输出描述:
如果当前字符流没有存在出现一次的字符,返回#字符。
-
题解
class Solution
{
public:
string s;
int hash[256] = {0};
//Insert one char from stringstream
void Insert(char ch)
{
s += ch;
hash[ch]++;
}
//return the first appearence once char in current stringstream
char FirstAppearingOnce()
{
for(int i = 0; i < s.size(); i++) {
if(hash[s[i]] == 1) return s[i];
}
return '#';
}
};
链表中环的入口结点
-
题目描述
给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。
-
题解
class Solution {
public:
ListNode* EntryNodeOfLoop(ListNode* pHead)
{
if(pHead == NULL || pHead->next == NULL) {
return NULL;
}
ListNode *p1 = pHead, *p2 = pHead;
while(p2 != NULL || p2->next != NULL) {
p1 = p1->next;
p2 = p2->next->next;
if(p1 == p2) {
p2 = pHead;
while(p1 != p2) {
p1 = p1->next;
p2 = p2->next;
}
return p2;
}
}
return NULL;
}
};
删除链表中重复的结点
-
题目描述
在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
-
题解
class Solution {
public:
ListNode* deleteDuplication(ListNode* pHead)
{
if(pHead == NULL || pHead->next == NULL) {
return pHead;
}
if(pHead->val == pHead->next->val) {
ListNode* n = pHead->next;
while(n != NULL && n->val == pHead->val) n = n->next;
return deleteDuplication(n);
}
else {
pHead->next = deleteDuplication(pHead->next);
return pHead;
}
}
};
二叉树的下一个结点
-
题目描述
给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。
-
题解
//https://www.nowcoder.com/questionTerminal/9023a0c988684a53960365b889ceaf5e?f=discussion
class Solution {
public:
TreeLinkNode* GetNext(TreeLinkNode* pNode)
{
if(pNode == NULL) return NULL;
if(pNode->right != NULL) {
pNode = pNode->right;
while(pNode->left != NULL) pNode = pNode->left;
return pNode;
}
while(pNode->next != NULL) {
TreeLinkNode* n = pNode->next;
if(n->left == pNode) return n;
pNode = pNode->next;
}
return NULL;
}
};
对称的二叉树
-
题目描述
请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。
-
题解
class Solution {
public:
bool isSymmetrical(TreeNode* pRoot)
{
if(pRoot == NULL) return true;
return dfs(pRoot->left, pRoot->right);
}
bool dfs(TreeNode* l, TreeNode* r) {
if(l == NULL && r == NULL) return true;
if(l == NULL || r == NULL) return false;
if(l->val != r->val) return false;
return dfs(l->left, r->right) && dfs(l->right, r->left);
}
};
按之字形顺序打印二叉树
题目描述
请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。
题解
public static ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
int layer = 1;
//s1存奇数层节点
Stack<TreeNode> s1 = new Stack<TreeNode>();
s1.push(pRoot);
//s2存偶数层节点
Stack<TreeNode> s2 = new Stack<TreeNode>();
ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
while (!s1.empty() || !s2.empty()) {
if (layer%2 != 0) {
ArrayList<Integer> temp = new ArrayList<Integer>();
while (!s1.empty()) {
TreeNode node = s1.pop();
if(node != null) {
temp.add(node.val);
System.out.print(node.val + " ");
s2.push(node.left);
s2.push(node.right);
}
}
if (!temp.isEmpty()) {
list.add(temp);
layer++;
System.out.println();
}
} else {
ArrayList<Integer> temp = new ArrayList<Integer>();
while (!s2.empty()) {
TreeNode node = s2.pop();
if(node != null) {
temp.add(node.val);
System.out.print(node.val + " ");
s1.push(node.right);
s1.push(node.left);
}
}
if (!temp.isEmpty()) {
list.add(temp);
layer++;
System.out.println();
}
}
}
return list;
}
序列化二叉树
-
题目描述
请实现两个函数,分别用来序列化和反序列化二叉树
-
题解
public class Solution {
String s = "";
String Serialize(TreeNode root) {
if(root == null) {
s += "#,";
return s;
}
s += root.val + ",";
Serialize(root.left);
Serialize(root.right);
return s;
}
int i = -1;
TreeNode Deserialize(String str) {
i++;
String[] ss = str.split(",");
TreeNode node = null;
if(!ss[i].equals("#")) {
node = new TreeNode(Integer.valueOf(ss[i]));
node.left = Deserialize(str);
node.right = Deserialize(str);
}
return node;
}
}
二叉搜索树的第k个结点
-
题目描述
给定一棵二叉搜索树,请找出其中的第k小的结点。例如, (5,3,7,2,4,6,8) 中,按结点数值大小顺序第三小结点的值为4。
-
题解
class Solution {
public:
int num = 0;
TreeNode* KthNode(TreeNode* pRoot, int k)
{
if(pRoot) {
TreeNode* t = KthNode(pRoot->left, k);
if(t) return t;
num++;
if(num == k) return pRoot;
t = KthNode(pRoot->right, k);
if(t) return t;
}
return NULL;
}
};
数据流中的中位数
-
题目描述
如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值。如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值。我们使用Insert()方法读取数据流,使用GetMedian()方法获取当前读取数据的中位数。
-
题解
class Solution {
public:
vector<int> v;
int n;
void Insert(int num)
{
v.push_back(num);
n = v.size();
for(int i = n-1; i > 0 && v[i] < v[i-1]; i--) swap(v[i], v[i-1]);
}
double GetMedian()
{
return (v[n/2] + v[(n-1)/2]) / 2.0;
}
};
滑动窗口的最大值
-
题目描述
给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,6,6,5}; 针对数组{2,3,4,2,6,2,5,1}的滑动窗口有以下6个: {[2,3,4],2,6,2,5,1}, {2,[3,4,2],6,2,5,1}, {2,3,[4,2,6],2,5,1}, {2,3,4,[2,6,2],5,1}, {2,3,4,2,[6,2,5],1}, {2,3,4,2,6,[2,5,1]}。
-
题解
class Solution {
public:
vector<int> maxInWindows(const vector<int>& num, unsigned int size)
{
vector<int> ans;
deque<int> q;
for(int i = 0; i < num.size(); i++) {
while(q.size() && num[q.back()] < num[i]) q.pop_back();
while(q.size() && i-q.front()+1 > size) q.pop_front();
q.push_back(i);
if(size && i+1 >= size) ans.push_back(num[q.front()]);
}
return ans;
}
};