回文排列
class Solution
{
public :
bool canPermutePalindrome ( string s)
{
int count[ 256 ] = { 0 } ;
for ( size_t i = 0 ; i < s. size ( ) ; ++ i)
++ count[ s[ i] ] ;
int flag = 0 ;
for ( size_t i = 0 ; i < 256 ; ++ i)
if ( count[ i] % 2 == 1 )
++ flag;
if ( flag > 1 )
return false ;
return true ;
}
} ;
URL化
class Solution
{
public :
string replaceSpaces ( string S, int length)
{
int front = length - 1 ;
int back = front;
for ( int i = 0 ; i < length; ++ i)
{
if ( S[ i] == ' ' )
{
back += 2 ;
}
}
S. resize ( back + 1 ) ;
while ( front != back)
{
if ( S[ front] != ' ' )
{
S[ back-- ] = S[ front] ;
}
else
{
S[ back-- ] = '0' ;
S[ back-- ] = '2' ;
S[ back-- ] = '%' ;
}
-- front;
}
return S;
}
} ;
配对交换
class Solution {
public :
int exchangeBits ( int num)
{
int num1 = 0b01010101010101010101010101010101 ;
int num2 = 0b10101010101010101010101010101010 ;
num1 &= num;
num2 &= num;
return ( num1 << 1 ) + ( num2 >> 1 ) ;
}
} ;
递归乘法
class Solution
{
public :
int multiply ( int A, int B)
{
if ( B == 0 )
return 0 ;
if ( B == 1 )
return A;
return A + multiply ( A, B - 1 ) ;
}
} ;
阶乘尾数
class Solution
{
public :
int trailingZeroes ( int n)
{
int count = 0 ;
while ( n >= 5 )
{
n /= 5 ;
count += n;
}
return count;
}
} ;
二进制链表转整数
class Solution
{
public :
int getDecimalValue ( ListNode* head)
{
int num = 0 ;
while ( head != NULL )
{
num = ( num << 1 ) + head-> val;
head = head-> next;
}
return num;
}
} ;
从链表中删去总和值为零的连续节点
class Solution {
public :
ListNode* removeZeroSumSublists ( ListNode* head)
{
ListNode* newhead = new ListNode;
newhead-> next = head;
ListNode* front = newhead;
while ( front)
{
int sum = 0 ;
ListNode* back = front-> next;
while ( back)
{
sum += back-> val;
if ( sum == 0 )
{
ListNode* del = front-> next;
back = back-> next;
while ( del != back)
{
ListNode* tmp = del;
del = del-> next;
}
front-> next = back;
}
else
{
back = back-> next;
}
}
front = front-> next;
}
head = newhead-> next;
delete newhead;
return head;
}
} ;
括号的最大嵌套深度
class Solution {
public :
int maxDepth ( string s)
{
int count = 0 ;
int depth = 0 ;
for ( char c : s)
{
if ( c == '(' )
{
++ count;
if ( count > depth)
{
++ depth;
}
}
else if ( c == ')' )
{
-- count;
}
}
return depth;
}
} ;
整理字符串
class Solution {
public :
string makeGood ( string s)
{
size_t i = 0 ;
while ( s. size ( ) > 0 && i < s. size ( ) - 1 )
{
if ( abs ( s[ i] - s[ i+ 1 ] ) == 32 )
{
s. erase ( i, 2 ) ;
if ( i > 0 )
{
-- i;
}
continue ;
}
++ i;
}
return s;
}
} ;
奇偶树
class Solution {
public :
bool isEvenOddTree ( TreeNode* root)
{
queue< TreeNode* > q;
q. push ( root) ;
if ( ( q. front ( ) -> val) % 2 == 0 )
{
return false ;
}
int level = 0 ;
int num = 1 ;
while ( ! q. empty ( ) )
{
vector< int > v;
while ( num-- )
{
if ( q. front ( ) -> left)
{
q. push ( q. front ( ) -> left) ;
v. push_back ( q. back ( ) -> val) ;
}
if ( q. front ( ) -> right)
{
q. push ( q. front ( ) -> right) ;
v. push_back ( q. back ( ) -> val) ;
}
q. pop ( ) ;
}
++ level;
num = v. size ( ) ;
if ( ! v. empty ( ) )
{
if ( level % 2 )
{
int prev = v[ 0 ] ;
if ( prev % 2 == 1 )
{
return false ;
}
for ( size_t i = 1 ; i < v. size ( ) ; ++ i)
{
if ( ( v[ i] % 2 != 0 ) || prev <= v[ i] )
{
return false ;
}
prev = v[ i] ;
}
}
else
{
int prev = v[ 0 ] ;
if ( prev % 2 == 0 )
{
return false ;
}
for ( size_t i = 1 ; i < v. size ( ) ; ++ i)
{
if ( ( v[ i] % 2 != 1 ) || prev >= v[ i] )
{
return false ;
}
prev = v[ i] ;
}
}
}
}
return true ;
}
} ;
将句子排序
class Solution {
public :
string sortSentence ( string s)
{
vector< string> vs;
vs. resize ( 9 ) ;
for ( int i = s. size ( ) - 1 ; i >= 0 ; -- i)
{
if ( s[ i] >= '0' && s[ i] <= '9' )
{
int j = i - 1 ;
while ( j >= 0 && s[ j] != ' ' )
{
-- j;
}
vs[ s[ i] - '0' - 1 ] += string ( s. begin ( ) + j + 1 , s. begin ( ) + i) ;
i = j;
}
}
for ( size_t i = 1 ; i < 9 ; ++ i)
{
if ( vs[ i] . size ( ) > 0 )
{
vs[ 0 ] += ( ' ' + vs[ i] ) ;
}
}
return vs[ 0 ] ;
}
} ;
最长和谐子序列
class Solution {
public :
int findLHS ( vector< int > & nums)
{
map< int , int > m;
for ( int e : nums)
{
m[ e] ++ ;
}
auto it = m. begin ( ) ;
auto prev = it;
++ it;
int max = 0 ;
while ( it != m. end ( ) )
{
if ( ( it-> first - prev-> first == 1 ) && ( prev-> second + it-> second > max) )
{
max = prev-> second + it-> second;
}
prev = it;
++ it;
}
return max;
}
} ;