1046. 最后一块石头的重量
1046. 最后一块石头的重量
- 用STL的堆暴力的解法
class Solution {
public:
int lastStoneWeight(vector<int>& stones) {
int len=stones.size();
if( len<=0 )
{
return 0;
}
if( 1==len )
{
return stones[0];
}
while( len>=2 )
{
make_heap(stones.begin(),stones.end());
sort_heap(stones.begin(),stones.end());
int a=stones[len-1];
int b=stones[len-2];
stones.pop_back();
stones.pop_back();
if( a!=b )
{
stones.push_back( a-b );
--len;
}
else
{
len-=2;
}
}
if( 1==len )
{
return stones[0];
}
else
{
return 0;
}
}
};