Time limit: 1 seconds space limit: 32768K heat index: 139492 algorithmic Knowledge video explanation
Title Description
We can use the small rectangle of 2*1 to cover the larger rectangle horizontally or vertically. What is the total number of ways to cover a large rectangle of 2*n with a small rectangle of n 2*1 without overlapping.
Idea: A typical Fibonacci sequence
#include <bits/stdc++.h>
using namespace std;
Class Solution {public
:
int rectcover (int number) {
if (number <= 2) return number;
Return Rectcover (number-1) +rectcover (number-2);
}
;
Time limit: 1 seconds space limit: 32768K heat index: 204418 algorithmic Knowledge video explanation of the topic
Enter two binary trees, a, B, to determine whether a is a sub-structure of a. (PS: We agree that an empty tree is not a sub-structure of any tree)
#include <bits/stdc++.h>
using namespace std;
/*
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;/* empty tree is not a sub-structure of any tree *
/if (! PROOT2) return false;/* empty tree is not a sub-structure of any tree *
/return (DFS (PROOT1,PROOT2) | | Hassubtree (proot1->left,proot2) | | Hassubtree (Proot1->right,proot2));
}
BOOL Dfs (treenode* R1, treenode* R2)
{if
(!R2) return true;/* If the nodes of the B two fork tree have already been matched, then the qualifying *
/if (!R1) return false;
if (r1->val! = R2->val) return false;
Return (DFS (r1->left,r2->left) &&dfs (r1->right,r2->right));
}
;
Time limit: 1 seconds space limit: 32768K heat index: 121779 algorithm Knowledge Video EXPLANATION The topic describes the operation of a given two-fork tree, transforming it into a mirror of the source binary tree. Input Description:
Image definition of binary tree: source binary tree
8
/ \ 6 x
/\ /\
5 7 9
Mirror binary tree
8
/ \ 6
/\ /\
9 7 5
/*
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) return;
treenode* p;
p=proot->left;
Proot->left = proot->right;
Proot->right = p;
Mirror (proot->left);
Mirror (proot->right);
}
};
Time limit: 1 seconds space limit: 32768K Heat index: 90361 points of Knowledge: array algorithm knowledge video explanation
Title DescriptionAll numbers in an array of length n are within the range of 0 to n-1. Some of the numbers in the array are duplicates, but it is not known that several numbers are duplicates. I don't know how many times each number repeats. Please find any duplicate numbers in the array. For example, if you enter an array of length 7 {2,3,1,0,2,5,3}, the corresponding output is the first repeating number 2.
Class Solution {public
:
//Parameters:
// numbers: An array of integers
// Length: The length of array numbers
// duplication: (Output) The duplicated number in the array number
//Return Valu E: True if the input is valid, and there be some duplications in the array number
// otherwise false
BOOL Duplicate (int numbers[], int length, int* duplication) {
map<int,int> MP;
if (length<=1| | Numbers==null) return false;
int cnt=0;
for (int i=0;i<length;i++)
{
mp[numbers[i]]++;
if (mp[numbers[i]]>1)
{
duplication[cnt++]=numbers[i];
return true;
}
}
return false;
}
};
Time limit: 1 seconds space limit: 32768K heat index: 124096 Point of Knowledge: string algorithm knowledge video explanation
Title DescriptionFinds the first occurrence of a string (1<= string length <=10000, all letters) and returns its position
Class Solution {public
:
int Firstnotrepeatingchar (string str) {
if (str.size () ==0) return-1;
int hash[255]={0};
int len = Str.length ();
for (int i=0;i<len;i++)
hash[str[i]-' A ']++;
for (int i=0;i<len;i++)
{
if (hash[str[i]-' A ']==1)
return i;
}
return 0;
}
};