栈的弹出、压入序列
bool IsPopOrder ( vector< int > pushV, vector< int > popV) {
if ( pushV. size ( ) == 0 || popV. size ( ) == 0 )
return false ;
stack< int > st;
int popIndex = 0 ;
for ( int i= 0 ; i< pushV. size ( ) ; i++ ) {
st. push ( pushV[ i] ) ;
while ( ! st. empty ( ) && st. top ( ) == popV[ popIndex] ) {
st. pop ( ) ;
popIndex++ ;
}
}
return st. empty ( ) ;
}
逆波兰表达式求
class Solution {
public :
int evalRPN ( vector< string> & tokens) {
stack< int > s;
int n1, n2;
for ( auto token: tokens) {
if ( token== "+" || token== "-" || token== "*" || token== "/" ) {
n2 = s. top ( ) ;
s. pop ( ) ;
n1 = s. top ( ) ;
s. pop ( ) ;
char c = token[ 0 ] ;
switch ( c) {
case '+' :
s. push ( n1+ n2) ;
break ;
case '-' :
s. push ( n1- n2) ;
break ;
case '*' :
s. push ( n1* n2) ;
break ;
case '/' :
s. push ( n1/ n2) ;
break ;
}
}
else
s. push ( atoi ( token. c_str ( ) ) ) ;
}
return s. top ( ) ;
}
} ;
数组中的第K个最大元素
int findKthLargest ( vector< int > & nums, int k) {
sort ( nums. begin ( ) , nums. end ( ) , ismax) ;
return nums[ k- 1 ] ;
}
static bool ismax ( const int & a, const int & b)
{
return a> b;
}
int findKthLargest ( vector< int > & nums, int k) {
vector< int > minstack ( k) ;
for ( int i= 0 ; i< k; i++ )
{
minstack[ i] = nums[ i] ;
}
make_heap ( minstack. begin ( ) , minstack. end ( ) , greater< int > ( ) ) ;
for ( int i= k; i< nums. size ( ) ; i++ )
{
if ( nums[ i] > minstack[ 0 ] )
{
minstack. push_back ( nums[ i] ) ;
push_heap ( minstack. begin ( ) , minstack. end ( ) , greater< int > ( ) ) ;
}
if ( minstack. size ( ) > k)
{
pop_heap ( minstack. begin ( ) , minstack. end ( ) , greater< int > ( ) ) ;
minstack. pop_back ( ) ;
}
}
return minstack[ 0 ] ;
}
int findKthLargest ( vector< int > & nums, int k) {
priority_queue< int > p ( nums. begin ( ) , nums. end ( ) ) ;
for ( int i= 0 ; i < k- 1 ; ++ i)
p. pop ( ) ;
return p. top ( ) ;
}
二叉树的层次遍历 II
vector< vector< int >> levelOrderBottom ( TreeNode* root) {
vector< vector< int >> a;
if ( ! root) return a;
queue< TreeNode* > q;
q. push ( root) ;
while ( ! q. empty ( ) ) {
vector< int > b;
int s = q. size ( ) ;
for ( int i = 0 ; i < s; ++ i) {
TreeNode* t = q. front ( ) ;
b. push_back ( t- > val) ;
q. pop ( ) ;
if ( t- > left) q. push ( t- > left) ;
if ( t- > right) q. push ( t- > right) ;
}
a. push_back ( b) ;
}
reverse ( a. begin ( ) , a. end ( ) ) ;
return a;
}
二叉树的层平均值
vector< double > averageOfLevels ( TreeNode* root) {
vector< double > res;
if ( ! root)
return res;
queue< TreeNode* > q;
q. push ( root) ;
while ( ! q. empty ( ) ) {
double sum = 0 ;
int n = q. size ( ) ;
for ( int i = 0 ; i < n; i++ ) {
root = q. front ( ) ;
sum + = root- > val;
q. pop ( ) ;
if ( root- > left) q. push ( root- > left) ;
if ( root- > right) q. push ( root- > right) ;
}
res. push_back ( sum/ n) ;
}
return res;
}
在每个树行中找最大值
vector< int > largestValues ( TreeNode* root) {
if ( ! root) return { } ;
vector< int > res;
queue< TreeNode* > q;
q. push ( root) ;
while ( ! q. empty ( ) ) {
int max_num= INT_MIN;
int size = q. size ( ) ;
while ( size-- ) {
root = q. front ( ) ;
q. pop ( ) ;
max_num= max ( max_num, root- > val) ;
if ( root- > left) q. push ( root- > left) ;
if ( root- > right) q. push ( root- > right) ;
}
res. push_back ( max_num) ;
}
return res;
}
N叉树的层序遍历
class Solution {
public :
vector< vector< int >> levelOrder ( Node* root) {
vector< int > layer;
vector< vector< int >> res;
if ( root== NULL ) return res;
queue< Node* > q;
Node* temp;
q. push ( root) ;
while ( ! q. empty ( ) ) {
int num= q. size ( ) ;
for ( int i= 0 ; i< num; i++ ) {
temp= q. front ( ) ;
q. pop ( ) ;
layer. push_back ( temp- > val) ;
if ( temp- > children. size ( ) != 0 ) {
for ( int i= 0 ; i< temp- > children. size ( ) ; i++ ) {
q. push ( temp- > children[ i] ) ;
}
}
}
res. push_back ( layer) ;
layer. clear ( ) ;
}
return res;
}
} ;
前K个高频元素
class Solution {
public :
vector< int > topKFrequent ( vector< int > & nums, int k) {
unordered_map< int , int > m;
priority_queue< pair< int , int >> q;
vector< int > res;
for ( auto a : nums) ++ m[ a] ;
for ( auto it : m) q. push ( { it. second, it. first} ) ;
for ( int i = 0 ; i < k; ++ i) {
res. push_back ( q. top ( ) . second) ; q. pop ( ) ;
}
return res;
}
} ;
前K个高频单词
class Solution {
public :
vector< string> topKFrequent ( vector< string> & words, int k) {
map< string, int > countmap;
for ( auto & e : words)
countmap[ e] ++ ;
multimap< int , string, greater< int >> sortmap;
for ( const auto & kv: countmap)
{
sortmap. insert ( make_pair ( kv. second, kv. first) ) ;
}
vector< string> v;
multimap< int , string> :: iterator it = sortmap. begin ( ) ;
while ( it != sortmap. end ( ) && k-- )
{
v. push_back ( it- > second) ;
++ it;
}
return v;
}
} ;