README:This article is used to share some templates and methods that I have summarized in the past six months。
模板
# include <iostream>
# include <string>
# include <vector>
# include <list>
# include <algorithm>
# include <deque>
# include <set>
# include <map>
# include <queue>
# include <math.h>
# include <unordered_set>
# include <unordered_map>
using namespace std;
# define _for ( i, a, b) for ( int i= ( a) ; i< ( b) ; ++ i)
# define _rep ( i, a, b) for ( int i= ( a) ; i<= ( b) ; ++ i)
template < typename T >
ostream& operator << ( ostream & os, const vector< T> & v)
{
for ( int i = 0 ; i < v. size ( ) ; i++ )
os << v[ i] << " " ;
return os;
}
template < typename T >
ostream& operator << ( ostream& os, const set< T> & v)
{
for ( typename set < T> :: iterator it = v. begin ( ) ; it != v. end ( ) ; it++ )
os << * it << " " ;
return os;
}
struct TreeNode {
int val;
TreeNode * left;
TreeNode * right;
TreeNode ( ) : val ( 0 ) , left ( nullptr ) , right ( nullptr ) { }
TreeNode ( int x) : val ( x) , left ( nullptr ) , right ( nullptr ) { }
TreeNode ( int x, TreeNode * left, TreeNode * right) : val ( x) , left ( left) , right ( right) { }
} ;
struct ListNode {
int val;
ListNode * next;
ListNode ( ) : val ( 0 ) , next ( nullptr ) { }
ListNode ( int x) : val ( x) , next ( nullptr ) { }
ListNode ( int x, ListNode * next) : val ( x) , next ( next) { }
} ;
int main ( ) {
system ( "pause" ) ;
}
中序遍历二叉树
void midsearch ( TreeNode* root , vector< int > & vec)
{
if ( root== NULL )
{
return ;
}
midsearch ( root-> left, vec) ;
vec. push_back ( root-> val) ;
midsearch ( root-> right, vec) ;
}
层级遍历二叉树
void Leveltraverse ( TreeNode* root, vector< int > & v)
{
TreeNode* p;
if ( ! root)
return ;
queue< TreeNode* > q;
q. push ( root) ;
while ( ! q. empty ( ) )
{
p = q. front ( ) ;
q. pop ( ) ;
v. push_back ( p-> val) ;
if ( p-> left)
{
q. push ( p-> left) ;
}
if ( p-> right)
{
q. push ( p-> right) ;
}
}
return ;
}
回溯
class Solution {
public :
bool backtrack ( int p, int sub, vector< int > & v, vector< int > & matchsticks)
{
if ( p >= matchsticks. size ( ) )
return true ;
for ( int i = 0 ; i < 4 ; i++ )
{
if ( v[ i] + matchsticks[ p] > sub)
continue ;
v[ i] += matchsticks[ p] ;
if ( backtrack ( p+ 1 , sub, v, matchsticks) )
{
return true ;
}
v[ i] -= matchsticks[ p] ;
}
return false ;
}
static bool pred ( int a, int b)
{
return a > b;
}
bool makesquare ( vector< int > & matchsticks) {
int len = matchsticks. size ( ) ;
int all = 0 ;
for ( int i = 0 ; i < len; i++ )
{
all += matchsticks[ i] ;
}
if ( all % 4 != 0 )
{
return false ;
}
sort ( matchsticks. begin ( ) , matchsticks. end ( ) , pred) ;
int sub = all / 4 ;
vector< int > v ( 4 , 0 ) ;
int p = 0 ;
return backtrack ( p, sub, v, matchsticks) ;
}
} ;
并查集
void init ( int n)
{
fa[ n] = n;
}
int find ( int i)
{
if ( fa[ i] == i)
return i;
fa[ i] = find ( fa[ i] ) ;
return fa[ i] ;
}
void unionn ( int x, int y)
{
int fa1 = find ( x) ;
int fa2 = find ( y) ;
fa[ fa1] = fa2;
}
基于BFS的汇入两洋问题
class Solution {
public :
bool ifon ( int x, int y, int m, int n)
{
if ( x < 0 || x > m- 1 || y < 0 || y > n- 1 )
return false ;
else
return true ;
}
vector< vector< int >> pacificAtlantic ( vector< vector< int >> & h) {
int m = h. size ( ) ;
int n = h[ 0 ] . size ( ) ;
int a1[ 250 ] [ 250 ] ;
int a2[ 250 ] [ 250 ] ;
int dirx[ 4 ] = { - 1 , 1 , 0 , 0 } ;
int diry[ 4 ] = { 0 , 0 , - 1 , 1 } ;
queue< int > qx1;
queue< int > qy1;
queue< int > qx2;
queue< int > qy2;
for ( int i = 0 ; i < m; i++ )
{
for ( int j = 0 ; j < n; j++ )
{
a1[ i] [ j] = 0 ;
a2[ i] [ j] = 0 ;
if ( i == 0 || j == 0 )
{
a1[ i] [ j] = 1 ;
qx1. push ( i) ;
qy1. push ( j) ;
}
if ( i == m - 1 || j == n - 1 )
{
a2[ i] [ j] = 1 ;
qx2. push ( i) ;
qy2. push ( j) ;
}
}
}
while ( ! qx1. empty ( ) )
{ for ( int k = 0 ; k < 4 ; k++ )
{
if ( ! ifon ( qx1. front ( ) + dirx[ k] , qy1. front ( ) + diry[ k] , m, n) || a1[ qx1. front ( ) + dirx[ k] ] [ qy1. front ( ) + diry[ k] ] == 1 )
continue ;
else
{
if ( h[ qx1. front ( ) + dirx[ k] ] [ qy1. front ( ) + diry[ k] ] >= h[ qx1. front ( ) ] [ qy1. front ( ) ] )
{
qx1. push ( qx1. front ( ) + dirx[ k] ) ;
qy1. push ( qy1. front ( ) + diry[ k] ) ;
a1[ qx1. front ( ) + dirx[ k] ] [ qy1. front ( ) + diry[ k] ] = 1 ;
}
}
}
qx1. pop ( ) ;
qy1. pop ( ) ;
}
while ( ! qx2. empty ( ) )
{
for ( int k = 0 ; k < 4 ; k++ )
{
if ( ! ifon ( qx2. front ( ) + dirx[ k] , qy2. front ( ) + diry[ k] , m, n) || a2[ qx2. front ( ) + dirx[ k] ] [ qy2. front ( ) + diry[ k] ] == 1 )
continue ;
else
{
if ( h[ qx2. front ( ) + dirx[ k] ] [ qy2. front ( ) + diry[ k] ] >= h[ qx2. front ( ) ] [ qy2. front ( ) ] )
{
qx2. push ( qx2. front ( ) + dirx[ k] ) ;
qy2. push ( qy2. front ( ) + diry[ k] ) ;
a2[ qx2. front ( ) + dirx[ k] ] [ qy2. front ( ) + diry[ k] ] = 1 ;
}
}
}
qx2. pop ( ) ;
qy2. pop ( ) ;
}
vector< vector< int >> vec;
vector< int > v;
for ( int i = 0 ; i < m; i++ )
{
for ( int j = 0 ; j < n; j++ )
{
if ( a1[ i] [ j] == 1 && a2[ i] [ j] == 1 )
{
v. push_back ( i) ;
v. push_back ( j) ;
vec. push_back ( v) ;
v. clear ( ) ;
}
}
}
return vec;
}
} ;
分治例题leet241
class Solution {
public :
vector< int > diffWaysToCompute ( string str) {
int len = str. size ( ) ;
vector< int > ans;
for ( int i = 0 ; i < len; i++ )
{
if ( str[ i] == '+' || str[ i] == '*' || str[ i] == '-' )
{
vector< int > ans1 = diffWaysToCompute ( str. substr ( 0 , i) ) ;
vector< int > ans2 = diffWaysToCompute ( str. substr ( i + 1 ) ) ;
for ( int val1 : ans1)
{
for ( int val2 : ans2)
{
if ( str[ i] == '-' )
ans. push_back ( val1 - val2) ;
if ( str[ i] == '+' )
ans. push_back ( val1 + val2) ;
if ( str[ i] == '*' )
ans. push_back ( val1 * val2) ;
}
}
}
}
if ( ans. empty ( ) )
ans. push_back ( stoi ( str) ) ;
return ans;
}
} ;
建立平衡二叉树
TreeNode* BuildValTree ( vector< int > & v, int l, int r)
{
if ( l > r)
return NULL ;
int m = ( l + r) / 2 ;
TreeNode* root = new TreeNode ( v[ m] ) ;
root-> left = BuildValTree ( v, l, m - 1 ) ;
root-> right = BuildValTree ( v, m + 1 , r) ;
return root;
}
SPF广度优先搜索迷宫问题
# include <iostream>
# include <string>
# include <vector>
# include <queue>
# include <cstdlib>
using namespace std;
class point
{
public :
int x;
int y;
int step;
} ;
void create_map ( int arr[ ] [ 100 ] , int temparr[ ] [ 100 ] , int x_num, int y_num)
{
for ( int i = 0 ; i < 100 ; i++ )
{
for ( int j = 0 ; j < 100 ; j++ )
{
arr[ i] [ j] = 0 ;
temparr[ i] [ j] = 0 ;
}
}
for ( int i = 1 ; i <= x_num; i++ )
{
for ( int j = 1 ; j <= y_num; j++ )
{
cin>> arr[ i] [ j] ;
}
}
}
int main ( ) {
int arr[ 100 ] [ 100 ] , x_num, y_num, star_x, star_y, px, py;
int temparr[ 100 ] [ 100 ] ;
int move_x[ 4 ] = { 0 , 0 , - 1 , 1 } ;
int move_y[ 4 ] = { 1 , - 1 , 0 , 0 } ;
queue< point> que;
point po;
cin >> x_num >> y_num;
create_map ( arr, temparr, x_num, y_num) ;
cin >> star_x >> star_y;
cin >> px >> py;
po. x = star_x;
po. y = star_y;
temparr[ star_x] [ star_y] = 2 ;
po. step = 0 ;
que. push ( po) ;
int flag = 0 ;
int re = 0 ;
while ( que. empty ( ) == 0 )
{
flag = 0 ;
int x = que. front ( ) . x, y = que. front ( ) . y;
for ( int i = 0 ; i < 4 ; i++ )
{
if ( arr[ x + move_x[ i] ] [ y + move_y[ i] ] == 1 && temparr[ x + move_x[ i] ] [ y + move_y[ i] ] == 0 )
{
point temppo;
temppo. x = x + move_x[ i] ;
temppo. y = y + move_y[ i] ;
temppo. step = que. front ( ) . step+ 1 ;
if ( temppo. x == px && temppo. y == py)
{
re = temppo. step;
break ;
}
que. push ( temppo) ;
temparr[ x + move_x[ i] ] [ y + move_y[ i] ] = 2 ;
flag = 1 ;
}
}
if ( re != 0 )
break ;
que. pop ( ) ;
}
cout << re << endl;
system ( "pause" ) ;
}
Point
struct Point
{
int x, y;
Point ( int x = 0 , int y= 0 ) : x ( x) , y ( y) { }
Point& operator = ( Point& p) { x = p. x; y = p. y; return * this ; }
} ;
typedef Point Vector;
Vector operator + ( const Vector& A, const Vector& B)
{
return Vector ( A. x + B. x, A. y + B. y) ;
}
Vector operator - ( const Vector& A, const Vector& B)
{
return Vector ( A. x - B. x, A. y - B. y) ;
}
Vector operator - ( const Vector& A, int p)
{
return Vector ( A. x* p, A. y* p) ;
}
bool operator == ( const Vector& a, const Vector& b)
{
return a. x == b. x && a. y == b. y;
}
bool operator < ( const Point& p1, const Point& p2)
{
return p1. x < p2. x || ( p1. x == p2. x && p1. y < p2. y) ;
}
istream& operator >> ( istream& is, Point& p)
{
return is >> p. x >> p. y;
}
next数组求法
# include <iostream>
# include <string>
# include <vector>
using namespace std;
void getnext ( int next[ ] , string str)
{
int j = 0 ;
next[ 0 ] = 0 ;
for ( int i = 1 ; i < str. size ( ) ; i++ )
{
while ( j > 0 && str[ i] != str[ j] )
j = next[ j - 1 ] ;
if ( str[ i] == str[ j] )
j++ ;
next[ i] = j;
}
}
int main ( ) {
string str;
cin >> str;
int * arr = new int [ str. size ( ) ] ;
getnext ( arr, str) ;
for ( int i = 0 ; i < str. size ( ) ; i++ )
{
cout << arr[ i] << " " ;
}
system ( "pause" ) ;
}
cout_vector_set
template < typename T >
ostream& operator << ( ostream & os, const vector< T> & v)
{
for ( int i = 0 ; i < v. size ( ) ; i++ )
os << v[ i] << " " ;
return os;
}
template < typename T >
ostream& operator << ( ostream& os, const set< T> & v)
{
for ( typename set < T> :: iterator it = v. begin ( ) ; it != v. end ( ) ; it++ )
os << * it << " " ;
return os;
}