剑指 offer 算法题
1. 二维数组中的查找
在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
class Solution {
public:
bool Find(int target, vector<vector<int> > array) {
int row = array.size();
if (row == 0) return false;
int col = array[0].size();
if (col == 0) return false;
int i = 0,j = col - 1;
while (i < row && array[i][j] < target) i++;
for (; i < row; i++){
for (; j >= 0; j--){
if (array[i][j] == target) return true;
else if (array[i][j] < target) break;
}
}
return false;
}
};
2. 替换空格
请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
class Solution {
public:
void replaceSpace(char *str,int length) {
char *buf = new char[length];
for (int i = 0; i < length; i++) buf[i] = str[i];
int j = 0;
for (int i = 0; i < length; i++){
if (buf[i] == ' '){
str[j++] = '%';
str[j++] = '2';
str[j++] = '0';
}else str[j++] = buf[i];
}
}
};
3. 从尾到头打印链表
输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) :
* val(x), next(NULL) {
* }
* };
*/
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
vector<int> res;
if(head == NULL) return res;
res = printListFromTailToHead(head->next);
res.push_back(head->val);
return res;
}
};
4. 重建二叉树
输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* fun(vector<int> pre,vector<int> vin,int x,int y,int x1,int y1){
int len = y - x + 1;
if (len <= 0) return NULL;
int i = 0;
while (vin[i + x1] != pre[x]) i++;
TreeNode *res = new TreeNode(pre[x]);
res->left = fun(pre, vin, x + 1, x + i, x1, x1 + i - 1);
res->right = fun(pre, vin, x + i + 1, y, x1 + i + 1, y1);
return res;
}
TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) {
int len = pre.size();
return fun(pre, vin, 0, len - 1, 0, len - 1);
}
};
5. 用两个栈实现队列
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
class Solution
{
public:
void push(int node) {
stack1.push(node);
}
int pop() {
int res;
while(!stack1.empty()) {stack2.push(stack1.top());stack1.pop();}
res = stack2.top();stack2.pop();
while(!stack2.empty()) {stack1.push(stack2.top());stack2.pop();}
return res;
}
private:
stack<int> stack1;
stack<int> stack2;
};
6. 旋转数组的最小数字
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
class Solution {
public:
int minNumberInRotateArray(vector<int> rotateArray) {
int l = rotateArray.size();
if (l < 2) return 0;
for(int i = 0; i < l-1; i++){
if (rotateArray[i] > rotateArray[i+1]) return rotateArray[i+1];
}
if (rotateArray[l-1] < rotateArray[0]) return rotateArray[l-1];
return rotateArray[0];
}
};
7. 斐波那契数列
大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。n<=39
class Solution {
public:
int Fibonacci(int n) {
if (n < 2) return n;
int a = 0, b = 1;
int c;
for (int i = 2; i <= n; i++){
c = a + b;
a = b;
b = c;
}
return c;
}
};
8. 跳台阶
一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)。
class Solution {
public:
int jumpFloor(int number) {
if(number == 1) return 1;
if(number == 2) return 2;
int a = 1, b = 2, c;
for (int i = 3; i <= number; i++){
c = a + b;
a = b;
b = c;
}
return c;
}
};
9. 变态跳台阶
一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
class Solution {
public:
int jumpFloorII(int number) {
return pow(2,number - 1);
}
};
10. 矩形覆盖
我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?
class Solution {
public:
int rectCover(int number) {
if (number <= 2) return number;
int a = 1, b = 2, c;
for (int i = 3; i <= number; i++){
c = a + b;
a = b;
b = c;
}
return c;
}
};
11. 二进制中1的个数
输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。
class Solution {
public:
int NumberOf1(int n) {
int count=0;
unsigned int flag=1;
while(flag){
if (n & flag){
count++;
}
flag=flag<<1;
}
return count;
}
};
12. 数值的整数次方
给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
class Solution {
public:
double Power(double base, int exponent) {
long long p = abs( (long long) exponent);
double r = 1.0;
while (p) {
if (p & 1)
r *= base;
base *= base;
p >>= 1;
}
return ( exponent > 0 ) ? r : 1/r;
}
};
13. 调整数组顺序使奇数位于偶数前面
输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。
class Solution {
public:
void reOrderArray(vector<int> &array){
for (int i = 0; i < array.size();i++){
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]);
}
}
}
}
};
14. 链表中倒数第k个结点
输入一个链表,输出该链表中倒数第k个结点。
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindKthToTail(ListNode* p, unsigned int k) {
//if(!p) return nullptr;
auto p1=p;
for(int i=0;i!=k;++i)
if(!p1)return nullptr;
else
p1=p1->next;
while(p1){
p1=p1->next;
p=p->next;
}
return p;
}
};
15. 反转链表
输入一个链表,反转链表后,输出新链表的表头。
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
ListNode *head = NULL, *buf;
buf = pHead->next;
while (pHead){
pHead->next = head;
head = pHead;
pHead = buf;
buf = pHead->next;
}
return head;
}
};
16. 合并两个排序的链表
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* Merge(ListNode* pHead1, ListNode* pHead2){
ListNode *head;
ListNode *buf;
if (!pHead1) return pHead2;
if (!pHead2) return pHead1;
if (pHead1->val <= pHead2->val) head = pHead1;
else{
head = pHead2;
pHead2 = pHead1;
pHead1 = head;
}
while (pHead1->next){
while (pHead1->next && pHead1->next->val <= pHead2->val) pHead1 = pHead1->next;
if (!pHead1->next) {
pHead1->next = pHead2;
return head;
}
buf = pHead1->next;
pHead1->next = pHead2;
pHead2 = buf;
pHead1 = pHead1->next;
}
pHead1->next = pHead2;
return head;
}
};
17. 树的子结构
输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2)
{
if(!pRoot1)
return false;
if(!pRoot2)
return false;
return ( dfs(pRoot1,pRoot2)) || HasSubtree(pRoot1->left, pRoot2) || HasSubtree(pRoot1->right, pRoot2);
}
private:
bool dfs(TreeNode * r1, TreeNode * r2){
if(!r2)
return true;
if(!r1)
return false;
if(r1->val != r2->val)
return false;
return dfs(r1->left, r2->left) && dfs(r1->right, r2->right);
}
};
18. 二叉树的镜像
操作给定的二叉树,将其变换为源二叉树的镜像。
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
void Mirror(TreeNode *pRoot) {
if (pRoot == NULL) return;
TreeNode *buf = pRoot->left;
pRoot->left = pRoot->right;
pRoot->right = buf;
Mirror(pRoot->left);
Mirror(pRoot->right);
}
};
19. 顺时针打印矩阵
输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
class Solution {
public:
vector<int> printMatrix(vector<vector<int> > matrix) {
vector<int> res;
int row = matrix.size();
int col = matrix[0].size();
int r = row;
int c = col;
int i = 0;
while (1){
for (int j = i; j < col - i; j++) res.push_back(matrix[i][j]);
r--; if (r == 0) break;
for (int j = i + 1; j < row - i; j++) res.push_back(matrix[j][col - i - 1]);
c--; if (c == 0) break;
for (int j = col - i - 2; j >= i; j--) res.push_back(matrix[row - i - 1][j]);
r--; if (r == 0) break;
for (int j = row - i - 2; j > i; j--) res.push_back(matrix[j][i]);
c--; if (c == 0) break;
i++;
}
return res;
}
};
20. 包含min函数的栈
定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。
class Solution {
public:
stack<int> stack1,stack2;
void push(int value) {
stack1.push(value);
if(stack2.empty())
stack2.push(value);
else if(value<=stack2.top()){ //经典,反正栈是后入先出,有比你小的,你出不出没影响
stack2.push(value);
}
}
void pop() {
if(stack1.top()==stack2.top())
stack2.pop();
stack1.pop();
}
int top() {
return stack1.top();
}
int min() {
return stack2.top();
}
};
21. 栈的压入、弹出序列
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列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) {
int len = pushV.size();
if (len != popV.size()) return false;
stack<int> stk;
int k = 0;
stk.push(pushV[k++]);
for (int i = 0; i < len; i++){
if (!stk.empty() && stk.top() == popV[i]){
stk.pop();
continue;
}
while(k < len && pushV[k] != popV[i]) stk.push(pushV[k++]);
if(k == len) return false;
k++;
}
return true;
}
};
22. 从上往下打印二叉树
从上往下打印出二叉树的每个节点,同层节点从左至右打印。
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
vector<int> PrintFromTopToBottom(TreeNode* root) {
vector<int> res;
queue<TreeNode*> q;
TreeNode* buf;
if (!root) return res;
q.push(root);
while (!q.empty()){
buf = q.front();
res.push_back(buf->val);
if (buf->left) q.push(buf->left);
if (buf->right) q.push(buf->right);
q.pop();
}
return res;
}
};
23. 二叉搜索树的后序遍历序列
输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。
class Solution {
public:
bool VerifySquenceOfBST(vector<int> sequence) {
int len = sequence.size();
if (len == 0) return false;
return fun(sequence, 0, len - 1);
}
private:
bool fun(vector<int> sequence, int x, int y){
if (x >= y) return true;
int i = x;
while(i < y && sequence[i] < sequence[y]) i++;
if (i == y) return fun(sequence, x, y-1);
int j = i;
while(j < y)
if (sequence[j++] < sequence[y]) return false;
return fun(sequence, x, i - 1) && fun(sequence, i, y - 1);
}
};
24. 二叉树中和为某一值的路径
输入一颗二叉树的跟节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。(注意: 在返回值的list中,数组长度大的数组靠前)
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
vector<vector<int> > buffer;
vector<int> tmp;
vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {
if(root==NULL)
return buffer;
tmp.push_back(root->val);
if(expectNumber-root->val==0 && root->left==NULL && root->right==NULL)
buffer.push_back(tmp);
FindPath(root->left,expectNumber-root->val);
FindPath(root->right,expectNumber-root->val);
if(tmp.size()!=0)
tmp.pop_back();
return buffer;
}
};
25. 复杂链表的复制
输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)
/*
struct RandomListNode {
int label;
struct RandomListNode *next, *random;
RandomListNode(int x) :
label(x), next(NULL), random(NULL) {
}
};
*/
class Solution {
public:
/*
1、复制每个节点,如:复制节点A得到A1,将A1插入节点A后面
2、遍历链表,A1->random = A->random->next;
3、将链表拆分成原链表和复制后的链表
*/
RandomListNode* Clone(RandomListNode* pHead)
{
if(!pHead) return NULL;
RandomListNode *currNode = pHead;
while(currNode){
RandomListNode *node = new RandomListNode(currNode->label);
node->next = currNode->next;
currNode->next = node;
currNode = node->next;
}
currNode = pHead;
while(currNode){
RandomListNode *node = currNode->next;
if(currNode->random){
node->random = currNode->random->next;
}
currNode = node->next;
}
//拆分
RandomListNode *pCloneHead = pHead->next;
RandomListNode *tmp;
currNode = pHead;
while(currNode->next){
tmp = currNode->next;
currNode->next =tmp->next;
currNode = tmp;
}
return pCloneHead;
}
};
26. 二叉搜索树与双向链表
输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
TreeNode* Convert(TreeNode* pRootOfTree)
{
if(!pRootOfTree) return NULL;
if(!pRootOfTree->left && !pRootOfTree->right){
leftLast = pRootOfTree;
return pRootOfTree;
}
TreeNode *left = Convert(pRootOfTree->left);
if(left){
leftLast->right = pRootOfTree;
pRootOfTree->left = leftLast;
}
leftLast = pRootOfTree;
TreeNode *right = Convert(pRootOfTree->right);
if(right){
right->left = pRootOfTree;
pRootOfTree->right = right;
}
return left != NULL ? left:pRootOfTree;
}
private:
TreeNode* leftLast = NULL;
};
27. 字符串的排列
输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
class Solution {
public:
vector<string> Permutation(string str) {
vector<string> result;
int len = str.length();
if(!len) return result;
Permutations(result, str, 0, len);
return result;
}
void Permutations(vector<string>&result, string str,int index, int len){
if (index == len){
result.push_back(str);
return;
}
for (int i = index; i < len; ++i) {
if (i!=index && str[i]== str[index]) continue;
//这里交换很巧妙,相当于挨个往后推的!!!!!!!!
swap(str[i],str[index]);
Permutations(result, str, index+1, len);
}
}
};
28. 数组中出现次数超过一半的数字
数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为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 num = numbers[0], count = 1;
for (int i = 1; i < n; i++) {
if (numbers[i] == num) count++;
else count--;
if (count == 0) {
num = numbers[i];
count = 1;
}
}
count = 0;
for (int i = 0; i < n; i++) {
if (numbers[i] == num) count++;
}
if (count * 2 > n) return num;
return 0;
}
};
29. 最小的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> result;
if (input.size() == 0 || k == 0 || k>input.size()) {
return result;
}
for (int i = input.size() / 2 - 1; i >= 0; i--) { //初始化堆
heapAdjust(input, i, k);
}
int i = k;
while (i<input.size()) {
if (input[0]>input[i]) {
input[0] = input[i];
heapAdjust(input, 0, k);
}
i++;
}
for (int i = 0; i<k; i++)
result.push_back(input[i]);
return result;
}
private:
void heapAdjust(vector<int> &arr, int s, int len) {
int temp, j;
temp = arr[s];
for (j = 2 * (s + 1); j - 1 < len; j *= 2) {
if (j < len && arr[j - 1] < arr[j]) ++j;
if (temp >= arr[j - 1]) break;
arr[s] = arr[j - 1];
s = j - 1;
}
arr[s] = temp;
}
void heapSort(vector<int> &arr, int len) {
int i;
for (i = len / 2 - 1; i >= 0; i--) heapAdjust(arr, i, len);
for (i = len - 1; i > 0; i--) {
swap(arr[i], arr[0]);
heapAdjust(arr, 0, i);
}
}
};
30. 连续子数组的最大和
HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学。今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决。但是,如果向量中包含负数,是否应该包含某个负数,并期望旁边的正数会弥补它呢?例如:{6,-3,-2,7,-15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止)。给一个数组,返回它的最大连续子序列的和,你会不会被他忽悠住?(子向量的长度至少是1)
class Solution {
public:
int FindGreatestSumOfSubArray(vector<int> array) {
int res = array[0];
for (int i = 1; i < array.size(); i++) {
array[i] = max(array[i],array[i] + array[i-1]);
res = max(res, array[i]);
}
return res;
}
};
31. 整数中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 {
public:
int NumberOf1Between1AndN_Solution(int n)
{
int tem = n;
int i = 1;
int res = 0;
while (tem){
int y = tem % 10;
tem /= 10;
res += tem * i;
if (y == 1){
res += n % i + 1;
}else if (y > 1){
res += i;
}
i *= 10;
}
return res;
}
};
32. 把数组排成最小的数
输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。
class Solution {
public:
string PrintMinNumber(vector<int> numbers) {
int len = numbers.size();
if(len == 0) return "";
sort(numbers.begin(), numbers.end(), cmp);
string res;
for(int i = 0; i < len; i++){
res += to_string(numbers[i]);
}
return res;
}
static bool cmp(int a, int b){
string A = to_string(a) + to_string(b);
string B = to_string(b) + to_string(a);
return A < B;
}
};
33. 丑数
把只包含质因子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> res(index);
res[0] = 1;
int t2 = 0, t3 = 0, t5 = 0, i;
for (i = 1; i < index; ++i)
{
res[i] = min(res[t2] * 2, min(res[t3] * 3, res[t5] * 5));
if (res[i] == res[t2] * 2)t2++;
if (res[i] == res[t3] * 3)t3++;
if (res[i] == res[t5] * 5)t5++;
}
return res[index - 1];
}
};
34. 第一个只出现一次的字符
在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写)。
class Solution {
public:
int FirstNotRepeatingChar(string str) {
if(str.size()==0)
return -1;
char ch[256]={0};
for(int i=0;i<str.size();i++)
ch[str[i]]++;
for(int i=0;i<str.size();i++)
if(ch[str[i]]==1)
return i;
return 0;
}
};
35. 数组中的逆序对
在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的结果输出。 即输出P%1000000007
class Solution {
public:
int InversePairs(vector<int> data) {
int length = data.size();
if (length <= 0)
return 0;
vector<int> copy;
for (int i = 0; i<length; i++)
copy.push_back(data[i]);
long long count = InversePairsCore(data, copy, 0, length - 1);
return count % 1000000007;
}
long long InversePairsCore(vector<int> &data, vector<int> ©, int start, int end)
{
if (start == end)
{
copy[start] = data[start];
return 0;
}
int length = (end - start) / 2;
//这里 copy 和 data 的位置换了
long long left = InversePairsCore(copy, data, start, start + length);
long long right = InversePairsCore(copy, data, start + length + 1, end);
int i = start + length;
int j = end;
int indexcopy = end;
long long count = 0;
while (i >= start&&j >= start + length + 1)
{
if (data[i]>data[j])
{
copy[indexcopy--] = data[i--];
count = count + j - start - length;
}
else
{
copy[indexcopy--] = data[j--];
}
}
for (; i >= start; i--)
copy[indexcopy--] = data[i];
for (; j >= start + length + 1; j--)
copy[indexcopy--] = data[j];
return left + right + count;
}
};
36. 两个链表的第一个公共结点
输入两个链表,找出它们的第一个公共结点。
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindFirstCommonNode(ListNode* pHead1, ListNode* pHead2) {
ListNode *p1 = pHead1;
ListNode *p2 = pHead2;
int len1 = 0, len2 = 0, diff = 0;
while (p1 != NULL) {
p1 = p1->next;
len1++;
}
while (p2 != NULL) {
p2 = p2->next;
len2++;
}
if (len1>len2) {
diff = len1 - len2;
p1 = pHead1;
p2 = pHead2;
}
else {
diff = len2 - len1;
p1 = pHead2;
p2 = pHead1;
}
for (int i = 0; i<diff; i++) {
p1 = p1->next;
}
while (p1 != NULL && p2 != NULL) {
if (p1 == p2)
break;
p1 = p1->next;
p2 = p2->next;
}
return p1;
}
};
37. 数字在排序数组中出现的次数
统计一个数字在排序数组中出现的次数。
class Solution {
public:
int GetNumberOfK(vector<int> data ,int k) {
return biSearch(data, k+0.5) - biSearch(data, k-0.5) ;
}
private:
int biSearch(const vector<int> & data, double num){
int s = 0, e = data.size()-1;
while(s <= e){
int mid = (e - s)/2 + s;
if(data[mid] < num)
s = mid + 1;
else if(data[mid] > num)
e = mid - 1;
}
return s;
}
};
38. 二叉树的深度
输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
int TreeDepth(TreeNode* pRoot){
if(!pRoot) return 0 ;
return max(1+TreeDepth(pRoot->left), 1+TreeDepth(pRoot->right));
}
};
39. 平衡二叉树
输入一棵二叉树,判断该二叉树是否是平衡二叉树。
class Solution {
public:
int getDepth(TreeNode* root){
if(root==NULL)
return 0;
int left=getDepth(root->left);
int right=getDepth(root->right);
if(abs(left-right)>1){
isBalanced=false;
}
return right>left ? right+1:left+1;
}
bool IsBalanced_Solution(TreeNode* pRoot) {
getDepth(pRoot);
return isBalanced;
}
private:
bool isBalanced = true;
};
40. 数组中只出现一次的数字
一个整型数组里除了两个数字之外,其他的数字都出现了偶数次。请写程序找出这两个只出现一次的数字。
class Solution {
public:
void FindNumsAppearOnce(vector<int> data,int* num1,int* num2) {
if(data.size() < 2)
return;
int resultExclusiveOR = 0;
for(int i = 0; i < data.size(); i++){
resultExclusiveOR ^= data[i];
}
unsigned int indexOf1 = FindFirstBitIs1(resultExclusiveOR);
*num1 = *num2 = 0;
for(int j = 0; j < data.size(); j++){
if(IsBit1(data[j], indexOf1))
*num1 ^= data[j];
else
*num2 ^= data[j];
}
}
unsigned int FindFirstBitIs1(int num){
int indexBit = 0;
while(((num & 1) == 0) && (indexBit < 8*sizeof(int))){
num = num >> 1;
indexBit++;
}
return indexBit;
}
bool IsBit1(int num, unsigned int indexBit){
num = num >> indexBit;
return (num&1);
}
};
41. 和为S的连续正数序列
小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100。但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数)。没多久,他就得到另一组连续正数和为100的序列:18,19,20,21,22。现在把问题交给你,你能不能也很快的找出所有和为S的连续正数序列? Good Luck!
class Solution {
public:
vector<vector<int> > FindContinuousSequence(int sum) {
int l = 1,r = 1,sumx = 1;
vector<vector<int> > ans;
while(l <= r){
r ++;
sumx += r;
while(sumx > sum){
sumx -= l;
l ++;
}
if(sumx == sum && l != r){
vector<int> tmp;
for(int i = l;i <= r;i ++) tmp.push_back(i);
ans.push_back(tmp);
}
}
return ans;
}
};
42. 和为S的两个数字
输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。
class Solution {
public:
vector<int> FindNumbersWithSum(vector<int> array,int sum) {
vector<int> result;
int length = array.size();
if (length <= 1)
return result;
int i = 0;
int j = length - 1;
int Sum;
while (i < j)
{
Sum = array[i] + array[j];
if (Sum > sum)
j--;
else if (Sum < sum)
i++;
else
{
result.push_back(array[i]);
result.push_back(array[j]);
break;
}
}
return result;
}
};
43. 左旋转字符串
汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果,即“XYZdefabc”。是不是很简单?OK,搞定它!
class Solution {
public:
string LeftRotateString(string str, int n) {
if(n<0) return NULL;
if(n==0) return str;
string strTemp="";
strTemp=str.substr(0,n);
str.erase(0,n);
str+=strTemp;
return str;
}
};
44. 翻转单词顺序列
牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上。同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思。例如,“student. a am I”。后来才意识到,这家伙原来把句子单词的顺序翻转了,正确的句子应该是“I am a student.”。Cat对一一的翻转这些单词顺序可不在行,你能帮助他么?
class Solution {
public:
string ReverseSentence(string str) {
string res = "", tmp = "";
for(unsigned int i = 0; i < str.size(); ++i){
if(str[i] == ' ') res = " " + tmp + res, tmp = "";
else tmp += str[i];
}
if(tmp.size()) res = tmp + res;
return res;
}
};
45. 扑克牌顺子
LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)…他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子, 如果牌能组成顺子就输出true,否则就输出false。为了方便起见,你可以认为大小王是0。
class Solution {
public:
bool IsContinuous( vector<int> numbers ) {
if(numbers.size() != 5) return false;
int min = 14;
int max = -1;
int flag = 0;
for(int i = 0; i < numbers.size(); i++) {
int number = numbers[i];
if(number < 0 || number > 13) return false;
if(number == 0) continue;
if(((flag >> number) & 1) == 1) return false;
flag |= (1 << number);
if(number > max) max = number;
if(number < min) min = number;
if(max - min >= 5) return false;
}
return true;
}
};
46. 孩子们的游戏(圆圈中最后剩下的数)
让小朋友们围成一个大圈。然后,他随机指定一个数m,让编号为0的小朋友开始报数。每次喊到m-1的那个小朋友要出列唱首歌,然后可以在礼品箱中任意的挑选礼物,并且不再回到圈中,从他的下一个小朋友开始,继续0…m-1报数….这样下去….直到剩下最后一个小朋友,可以不用表演,并且拿到牛客名贵的“名侦探柯南”典藏版(名额有限哦!!^_^)。请你试着想下,哪个小朋友会得到这份礼品呢?(注:小朋友的编号是从0到n-1)
class Solution {
public:
int LastRemaining_Solution(unsigned int n, unsigned int m)
{
if(n==0)
return -1;
if(n==1)
return 0;
else
return (LastRemaining_Solution(n-1,m)+m)%n;
}
};
47. 求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) {
bool a[n][n+1];
return sizeof(a)>>1;
}
int Sum_Solution1(int n) {
int sum = n;
boolean ans = (n>0)&&((sum+=Sum_Solution(n-1))>0);
return sum;
}
};
48. 不用加减乘除做加法
写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。
class Solution {
public:
int Add(int num1, int num2)
{
int n1,n2;
n1=(num1&num2)<<1;
n2=num1^num2;
while(n1&n2)
{
num1=n1;num2=n2;
n1=(num1&num2)<<1;
n2=num1^num2;
}
return n1|n2;
}
};
49. 把字符串转换成整数
将一个字符串转换成一个整数(实现Integer.valueOf(string)的功能,但是string不符合数字要求时返回0),要求不能使用字符串转换整数的库函数。 数值为0或者字符串不是一个合法的数值则返回0。
class Solution {
public:
int StrToInt(string str) {
int n = str.size(), s = 1;
long long res = 0;
if(!n) return 0;
if(str[0] == '-') s = -1;
for(int i = (str[0] == '-' || str[0] == '+') ? 1 : 0; i < n; ++i){
if(!('0' <= str[i] && str[i] <= '9')) return 0;
res = (res << 1) + (res << 3) + (str[i] & 0xf);//res=res*10+str[i]-'0';
}
return res * s;
}
};
50. 数组中重复的数字
在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。
int find_dup( int numbers[], int length) {
for ( int i= 0 ; i<length; i++) {
int index = numbers[i];
if (index >= length) {
index -= length;
}
if (numbers[index] >= length) {
return index;
}
numbers[index] = numbers[index] + length;
}
return - 1 ;
}
51. 构建乘积数组
给定一个数组 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 length = A.size();
vector<int> B(length);
if(length != 0 ){
B[0] = 1;
//计算下三角连乘
for(int i = 1; i < length; i++){
B[i] = B[i-1] * A[i-1];
}
int temp = 1;
//计算上三角
for(int j = length-2; j >= 0; j--){
temp *= A[j+1];
B[j] *= temp;
}
}
return B;
}
};
52. 正则表达式匹配
请实现一个函数用来匹配包括’.’和’‘的正则表达式。模式中的字符’.’表示任意一个字符,而’‘表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串”aaa”与模式”a.a”和”ab*ac*a”匹配,但是与”aa.a”和”ab*a”均不匹配
class Solution {
public:
bool match(char* str, char* pattern){
if (pattern[0] == 0 && str[0] == 0)
return true;
if (pattern[0] != 0 && pattern[1] == '*')
if (match(str, pattern + 2))
return true;
if ((pattern[0] == '.' && str[0]) || str[0] == pattern[0]){
if (match(str + 1, pattern + 1))
return true;
if (pattern[1] == '*' && match(str + 1, pattern))
return true;
}
return false;
}
};
53. 表示数值的字符串
请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串”+100”,”5e2”,”-123”,”3.1416”和”-1E-16”都表示数值。 但是”12e”,”1a3.14”,”1.2.3”,”+-5”和”12e+4.3”都不是。
class Solution {
public:
bool isNumeric(char* str) {
// 标记符号、小数点、e是否出现过
bool sign = false, decimal = false, hasE = false;
for (int i = 0; i < strlen(str); i++) {
if (str[i] == 'e' || str[i] == 'E') {
if (i == strlen(str)-1) return false; // e后面一定要接数字
if (hasE) return false; // 不能同时存在两个e
hasE = true;
} else if (str[i] == '+' || str[i] == '-') {
// 第二次出现+-符号,则必须紧接在e之后
if (sign && str[i-1] != 'e' && str[i-1] != 'E') return false;
// 第一次出现+-符号,且不是在字符串开头,则也必须紧接在e之后
if (!sign && i > 0 && str[i-1] != 'e' && str[i-1] != 'E') return false;
sign = true;
} else if (str[i] == '.') {
// e后面不能接小数点,小数点不能出现两次
if (hasE || decimal) return false;
decimal = true;
} else if (str[i] < '0' || str[i] > '9') // 不合法字符
return false;
}
return true;
}
};
54. 字符流中第一个不重复的字符
请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符”go”时,第一个只出现一次的字符是”g”。当从该字符流中读出前六个字符“google”时,第一个只出现一次的字符是”l”。 如果当前字符流没有存在出现一次的字符,返回#字符。
class Solution
{
public:
//Insert one char from stringstream
void Insert(char ch)
{
++cnt[ch - '\0'];
if(cnt[ch - '\0'] == 1)
data.push(ch);
}
//return the first appearence once char in current stringstream
char FirstAppearingOnce()
{
while(!data.empty() && cnt[data.front()] >= 2) data.pop();
if(data.empty()) return '#';
return data.front();
}
private:
queue<char> data;
unsigned cnt[128];
};
55. 链表中环的入口结点
给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
public:
ListNode* EntryNodeOfLoop(ListNode* pHead){
if(pHead == NULL || pHead->next == NULL)
return NULL;
ListNode *p1 = pHead;
ListNode *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;
}
if(p1 == p2)
return p1;
}
}
return NULL;
}
};
56. 删除链表中重复的结点
在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
public:
ListNode* deleteDuplication(ListNode* pHead){
if (pHead == NULL) return pHead;
ListNode *pre = NULL;
ListNode *p = pHead;
ListNode *q = NULL;
while (p != NULL){
if (p->next != NULL && p->next->val == p->val){
q = p->next;
while (q != NULL && q->next != NULL && q->next->val == p->val)
q = q->next;
if (p == pHead)
pHead = q->next;
else
pre->next = q->next;
p = q->next;
}
else
{
pre = p;
p = p->next;
}
}
return pHead;
}
};
57. 二叉树的下一个结点
给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。
/*
struct TreeLinkNode {
int val;
struct TreeLinkNode *left;
struct TreeLinkNode *right;
struct TreeLinkNode *next;
TreeLinkNode(int x) :val(x), left(NULL), right(NULL), next(NULL) {
}
};
*/
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){ //没右子树,则找第一个当前节点是父节点左孩子的节点
if(pNode->next->left==pNode) return pNode->next;
pNode = pNode->next;
}
return NULL; //退到了根节点仍没找到,则返回null
}
};
58. 对称的二叉树
请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
*/
class Solution {
public:
bool isSymmetrical(TreeNode* pRoot){
return isSymmetrical(pRoot,pRoot);
}
bool isSymmetrical(TreeNode* pRoot1,TreeNode* pRoot2){
if(pRoot1==nullptr&&pRoot2==nullptr)
return true;
if(pRoot1==nullptr||pRoot2==nullptr)
return false;
if(pRoot1->val!=pRoot2->val)
return false;
return isSymmetrical(pRoot1->left,pRoot2->right)&& isSymmetrical(pRoot1->right,pRoot2->left);
}
};
59. 按之字形顺序打印二叉树
请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
*/
class Solution {
public:
vector<vector<int> > Print(TreeNode* pRoot) {
vector<vector<int> > res;
if (pRoot == nullptr) return res;
stack<TreeNode*> stackL;
stack<TreeNode*> stackR;
TreeNode* popNode;
vector<int> tmp;
tmp.push_back(pRoot->val);
res.push_back(tmp);
tmp.clear();
stackL.push(pRoot);
while (!stackL.empty() || !stackR.empty()) {
while (!stackL.empty()) {
popNode = stackL.top();
stackL.pop();
if (popNode->right) {
stackR.push(popNode->right);
tmp.push_back(popNode->right->val);
}
if (popNode->left) {
stackR.push(popNode->left);
tmp.push_back(popNode->left->val);
}
}
if (!tmp.empty()) {
res.push_back(tmp);
tmp.clear();
}
while (!stackR.empty()) {
popNode = stackR.top();
stackR.pop();
if (popNode->left) {
stackL.push(popNode->left);
tmp.push_back(popNode->left->val);
}
if (popNode->right) {
stackL.push(popNode->right);
tmp.push_back(popNode->right->val);
}
}
if (!tmp.empty()) {
res.push_back(tmp);
tmp.clear();
}
}
return res;
}
};
60. 把二叉树打印成多行
从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。
class Solution {
public:
vector<vector<int> > Print(TreeNode* pRoot) {
vector<vector<int> > vec;
if (pRoot == NULL) return vec;
queue<TreeNode*> q;
q.push(pRoot);
while (!q.empty())
{
int lo = 0, hi = q.size();
vector<int> c;
while (lo++ < hi)
{
TreeNode *t = q.front();
q.pop();
c.push_back(t->val);
if (t->left) q.push(t->left);
if (t->right) q.push(t->right);
}
vec.push_back(c);
}
return vec;
}
};
61. 序列化二叉树
请实现两个函数,分别用来序列化和反序列化二叉树
class Solution {
public:
vector<int> buf;
void dfs1(TreeNode *root) {
if (!root) buf.push_back(0xFFFFFFFF);
else {
buf.push_back(root->val);
dfs1(root->left);
dfs1(root->right);
}
}
TreeNode* dfs2(int* &p) {
if (*p == 0xFFFFFFFF) {
p++;
return NULL;
}
TreeNode* res = new TreeNode(*p);
p++;
res->left = dfs2(p);
res->right = dfs2(p);
return res;
}
char* Serialize(TreeNode *root) {
buf.clear();
dfs1(root);
int bufSize = buf.size();
int *res = new int[bufSize];
for (int i = 0; i<bufSize; i++) res[i] = buf[i];
return (char*)res;
}
TreeNode* Deserialize(char *str) {
int *p = (int*)str;
return dfs2(p);
}
};
62. 二叉搜索树的第k个结点
给定一棵二叉搜索树,请找出其中的第k小的结点。例如, (5,3,7,2,4,6,8) 中,按结点数值大小顺序第三小结点的值为4。
class Solution {
public:
void inorder(TreeNode* root, TreeNode* &ans) {
if (root) {
inorder(root->left, ans);
count--;
if (!count) ans = root;
inorder(root->right, ans);
}
}
TreeNode* KthNode(TreeNode* pRoot, int k)
{
if (!pRoot || k < 1) return nullptr;
TreeNode* ans = NULL;
count = k;
inorder(pRoot, ans);
return ans;
}
private:
int count;
};
63. 数据流中的中位数
如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值。如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值。我们使用Insert()方法读取数据流,使用GetMedian()方法获取当前读取数据的中位数。
class Solution {
priority_queue<int, vector<int>, less<int> > p;
priority_queue<int, vector<int>, greater<int> > q;
public:
void Insert(int num) {
if (p.empty() || num <= p.top()) p.push(num);
else q.push(num);
if (p.size() == q.size() + 2) q.push(p.top()), p.pop();
if (p.size() + 1 == q.size()) p.push(q.top()), q.pop();
}
double GetMedian() {
return p.size() == q.size() ? (p.top() + q.top()) / 2.0 : p.top();
}
};
64. 滑动窗口的最大值
给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。例如,如果输入数组{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> res;
deque<int> s;
for (unsigned int i = 0; i<num.size(); ++i) {
while (s.size() && num[s.back()] <= num[i])//从后面依次弹出队列中比当前num值小的元素,同时也能保证队列首元素为当前窗口最大值下标
s.pop_back();
while (s.size() && i - s.front() + 1 > size)//当当前窗口移出队首元素所在的位置,即队首元素坐标对应的num不在窗口中,需要弹出
s.pop_front();
s.push_back(i);//把每次滑动的num下标加入队列
if (size && i + 1 >= size)//当滑动窗口首地址 i 大于等于size时才开始写入窗口最大值
res.push_back(num[s.front()]);
}
return res;
}
};
65. 矩阵中的路径
请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则之后不能再次进入这个格子。 例如 a b c e s f c s a d e e 这样的3 X 4 矩阵中包含一条字符串”bcced”的路径,但是矩阵中不包含”abcb”路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入该格子。
class Solution {
private:
bool isPath(char *matrix, vector<char> flags, char* str, int x, int y, int rows, int cols){
if (x<0 || x >= rows || y<0 || y >= cols) //越界的点
return false;
if (matrix[x*cols + y] == *str && flags[x*cols + y] == 0){
flags[x*cols + y] = 1;
if (*(str + 1) == 0) // 字符串结尾了(最后一个满足的)
return true;
bool condition = isPath(matrix, flags, (str + 1), x, y - 1, rows, cols) ||
isPath(matrix, flags, (str + 1), x - 1, y, rows, cols) ||
isPath(matrix, flags, (str + 1), x, y + 1, rows, cols) ||
isPath(matrix, flags, (str + 1), x + 1, y, rows, cols);
if (condition == false)
flags[x*cols + y] = 0;
return condition;
}
else
return false;
}
public:
bool hasPath(char* matrix, int rows, int cols, char* str){
vector<char> flags(rows*cols, 0);
bool condition = false;
for (int i = 0; i<rows; i++)
for (int j = 0; j<cols; j++){
condition = (condition || isPath(matrix, flags, str, i, j, rows, cols));
}
return condition;
}
};
66. 机器人的运动范围
地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7 = 18。但是,它不能进入方格(35,38),因为3+5+3+8 = 19。请问该机器人能够达到多少个格子?
class Solution {
public:
int movingCount(int threshold, int rows, int cols){
bool *flag = new bool[rows * cols];
for (int i = 0; i < rows * cols; i++)
flag[i] = false;
int count = moving(threshold, rows, cols, 0, 0, flag);
return count;
}
int moving(int threshold, int rows, int cols, int i, int j, bool* flag){
int count = 0;
if (i >= 0 && i < rows && j >= 0 && j < cols && getsum(i) + getsum(j) <= threshold && flag[i * cols + j] == false){
flag[i * cols + j] = true;
count = 1 + moving(threshold, rows, cols, i + 1, j, flag)
+ moving(threshold, rows, cols, i - 1, j, flag)
+ moving(threshold, rows, cols, i, j - 1, flag)
+ moving(threshold, rows, cols, i, j + 1, flag);
}
return count;
}
int getsum(int num){
int sum = 0;
while (num){
sum += num % 10;
num /= 10;
}
return sum;
}
};