1.柠檬水找零:
1.思路一:
柠檬水找零
class Solution {
public :
bool lemonadeChange ( vector< int > & bills) {
int file= 0 ;
int ten = 0 ;
for ( auto num: bills)
{
if ( num == 5 ) file++ ;
else if ( num == 10 )
{
if ( file > 0 )
file-- , ten++ ;
else
return false ;
}
else
{
if ( ten>= 1 && file>= 1 )
ten-- , file-- ;
else if ( file>= 3 )
file-= 3 ;
else
return false ;
}
}
return true ;
}
} ;
GIF题目解析
2. 将数组和减半的最小操作数:
1.思路一:
将数组和减半的最小操作数
class Solution {
public :
int halveArray ( vector< int > & nums) {
long long sum = 0 ;
for ( auto num : nums)
{
sum += num;
}
long double half = ( ( long double ) sum) / 2 ;
int count = 0 ;
priority_queue< double > qu ( nums. begin ( ) , nums. end ( ) ) ;
while ( half> 0 )
{
double tmp = qu. top ( ) ;
qu. pop ( ) ;
tmp /= 2 ;
half -= tmp;
count++ ;
qu. push ( tmp) ;
}
return count;
}
} ;
GIF题目解析
3.最大数:
最大数
1.思路一:
class Solution {
public :
string largestNumber ( vector< int > & nums) {
vector< string> strs;
for ( int num: nums)
{
strs. push_back ( to_string ( num) ) ;
}
sort ( strs. begin ( ) , strs. end ( ) ,
[ ] ( const string s1 , const string s2)
{
return s1+ s2 > s2+ s1;
}
) ;
string ret;
for ( auto str: strs)
{
ret+= str;
}
if ( ret[ 0 ] == '0' ) return "0" ;
return ret;
}
} ;
GIF题目解析
4.摆动序列:
摆动序列
1.思路一:
class Solution {
public :
int wiggleMaxLength ( vector< int > & nums) {
int ret = 0 ;
int left = 0 ;
int right = 0 ;
for ( int i = 0 ; i < nums. size ( ) - 1 ; i++ )
{
right = nums[ i+ 1 ] - nums[ i] ;
if ( right == 0 ) continue ;
if ( left* right <= 0 ) ret++ ;
left = right;
}
return ret+ 1 ;
}
} ;
GIF题目解析
5.最长递增子序列
最长递增子序列
1.思路一:dp方法
class Solution {
public :
int lengthOfLIS ( vector< int > & nums) {
int n = nums. size ( ) ;
vector< int > dp ( n, 1 ) ;
int ret = 0 ;
for ( int i= 1 ; i< n; i++ )
{
for ( int j= 0 ; j< i; j++ )
{
if ( nums[ j] < nums[ i] )
{
dp[ i] = max ( dp[ j] + 1 , dp[ i] ) ;
}
}
ret = max ( dp[ i] , ret) ;
}
return ret;
}
} ;
GIF题目解析
2.思路二:在dp基础上进行的贪心优化:
class Solution {
public :
int lengthOfLIS ( vector< int > & nums) {
vector< int > dp;
dp. push_back ( nums[ 0 ] ) ;
int n = nums. size ( ) ;
for ( int cur= 1 ; cur< n ; cur++ )
{
if ( nums[ cur] > dp. back ( ) )
{
dp. push_back ( nums[ cur] ) ;
}
else
{
int left = 0 , right = dp. size ( ) - 1 ;
while ( left < right)
{
int mid = left + ( right - left) / 2 ;
if ( dp[ mid] < nums[ cur] ) left = mid + 1 ;
else right = mid;
}
dp[ left] = nums[ cur] ;
}
}
return dp. size ( ) ;
}
} ;
GIF题目解析
6.递增的三元子序列
递增的三元子序列
1.思路一:
class Solution {
public :
bool increasingTriplet ( vector< int > & nums) {
int one = nums[ 0 ] ;
int two = INT_MAX;
for ( int cur = 1 ; cur < nums. size ( ) ; cur++ )
{
if ( nums[ cur] > two) return true ;
else if ( nums[ cur] < two)
{
if ( nums[ cur] <= one) one = nums[ cur] ;
else two = nums[ cur] ;
}
}
return false ;
}
} ;
GIF题目解析
7.最长连续递增序列
最长连续递增序列
1.思路一:
class Solution {
public :
int findLengthOfLCIS ( vector< int > & nums) {
int i= 0 ;
int ret = 0 ;
while ( i < nums. size ( ) )
{
int j = 0 ;
for ( j = i; j< nums. size ( ) - 1 ; j++ )
{
if ( nums[ j] >= nums[ j+ 1 ] ) break ;
}
ret = max ( ret , j- i+ 1 ) ;
i = j+ 1 ;
}
return ret;
}
} ;
GIF题目解析: