11.盛最多水的容器
题目
代码
class Solution {
public :
int maxArea ( vector< int > & height) {
int left = 0 ;
int right = height. size ( ) - 1 ;
int result = ( right - left) * min ( height[ right] , height[ left] ) ;
while ( right> left) {
if ( height[ left] < height[ right] ) {
left++ ;
}
else {
right-- ;
}
result = max ( result, ( right - left) * min ( height[ right] , height[ left] ) ) ;
}
return result;
}
} ;
15.三数之和
题目
代码
class Solution {
public :
vector< vector< int >> threeSum ( vector< int > & nums) {
int n = nums. size ( ) ;
sort ( nums. begin ( ) , nums. end ( ) ) ;
vector< vector< int >> ans;
for ( int first = 0 ; first < n; ++ first) {
if ( first > 0 && nums[ first] == nums[ first - 1 ] ) {
continue ;
}
int third = n - 1 ;
int target = - nums[ first] ;
for ( int second = first + 1 ; second < n; ++ second) {
if ( second > first + 1 && nums[ second] == nums[ second - 1 ] ) {
continue ;
}
while ( second < third && nums[ second] + nums[ third] > target) {
-- third;
}
if ( second == third) {
break ;
}
if ( nums[ second] + nums[ third] == target) {
. push_back ( { nums[ first] , nums[ second] , nums[ third] } ) ;
}
}
}
return ans;
}
} ;
31. 下一个排列
题目
代码
class Solution {
public :
void nextPermutation ( vector< int > & nums) {
int length = nums. size ( ) ;
int k = length;
for ( int i= length- 2 ; i>= 0 ; -- i) {
if ( nums[ i] < nums[ i+ 1 ] ) {
k = i;
break ;
}
}
if ( k== length) {
sort ( nums. begin ( ) , nums. end ( ) ) ;
} else {
for ( int i= length- 1 ; i> k; -- i) {
if ( nums[ i] > nums[ k] ) {
int temp = nums[ i] ;
nums[ i] = nums[ k] ;
nums[ k] = temp;
break ;
}
}
sort ( nums. begin ( ) + k+ 1 , nums. end ( ) ) ;
}
}
} ;
33. 搜索旋转排序数组
题目
代码
class Solution {
public :
int search ( vector< int > & nums, int target) {
for ( int i= 0 ; i< nums. size ( ) ; ++ i) {
if ( nums[ i] == target) {
return i;
}
}
return - 1 ;
}
} ;
34. 在排序数组中查找元素的第一个和最后一个位置
题目
代码
class Solution {
public :
vector< int > searchRange ( vector< int > & nums, int target) {
vector< int > result;
int length = nums. size ( ) ;
if ( length== 0 ) {
result. push_back ( - 1 ) ;
result. push_back ( - 1 ) ;
return result;
}
int count = 0 ;
for ( int i= 0 ; i< length; ++ i) {
count++ ;
if ( nums[ i] == target) {
result. push_back ( i) ;
break ;
}
if ( count== length|| length== 0 ) {
result. push_back ( - 1 ) ;
result. push_back ( - 1 ) ;
return result;
}
}
for ( int i= length- 1 ; i>= 0 ; -- i) {
if ( nums[ i] == target) {
result. push_back ( i) ;
break ;
}
}
return result;
}
} ;
74. 搜索二维矩阵
题目
代码
class Solution {
public :
bool searchMatrix ( vector< vector< int >> & matrix, int target) {
int rows = matrix. size ( ) - 1 ;
int cols = matrix[ 0 ] . size ( ) - 1 ;
int index = rows;
while ( index>= 0 ) {
if ( matrix[ index] [ 0 ] <= target) {
for ( int i= 0 ; i<= cols; ++ i) {
if ( matrix[ index] [ i] == target) {
return true ;
}
}
return false ;
} else {
index-- ;
}
}
return false ;
}
} ;
82. 删除排序链表中的重复元素 II
题目
代码
class Solution {
public :
ListNode* deleteDuplicates ( ListNode* head) {
if ( ! head || ! head-> next)
return head;
ListNode* dummy = new ListNode ( - 1 ) ;
dummy-> next = head;
ListNode* pre = dummy;
ListNode* cur = head;
while ( cur&& cur-> next) {
if ( cur-> val== cur-> next-> val) {
while ( cur-> next && cur-> val == cur-> next-> val) {
cur = cur-> next;
}
pre-> next = cur-> next;
cur = cur-> next;
} else {
pre = cur;
cur = cur-> next;
}
}
return dummy-> next;
}
} ;
153. 寻找旋转排序数组中的最小值(二分查找)
题目
代码
class Solution {
public :
int findMin ( vector< int > & nums) {
int left = 0 ;
int right = nums. size ( ) - 1 ;
while ( left< right) {
int middle = ( right + left) >> 1 ;
if ( nums[ middle] < nums[ right] ) {
right = middle;
} else {
left = middle + 1 ;
}
}
return nums[ right] ;
}
} ;
162. 寻找峰值
题目
代码
class Solution {
public :
int findPeakElement ( vector< int > & nums) {
int left = 0 ;
int right = nums. size ( ) - 1 ;
while ( left< right) {
int middle = ( right - left) / 2 + left;
if ( nums[ middle] < nums[ middle+ 1 ] ) {
left = middle + 1 ;
} else {
right = middle;
}
}
return left;
}
} ;
844. 比较含退格的字符串
题目
代码
class Solution {
public :
bool backspaceCompare ( string s, string t) {
vector< char > r_s;
vector< char > t_s;
r_s = back ( s) ;
t_s = back ( t) ;
if ( t_s. size ( ) == 0 && r_s. size ( ) == 0 ) return true ;
if ( r_s. size ( ) != t_s. size ( ) ) return false ;
for ( int i= 0 ; i< r_s. size ( ) ; ++ i) {
if ( r_s[ i] != t_s[ i] ) {
return false ;
}
}
return true ;
}
vector< char > back ( string& s) {
vector< char > result;
int length = s. size ( ) ;
for ( int i= 0 ; i< length; ++ i) {
if ( s[ i] == '#' && result. size ( ) == 0 ) {
continue ;
}
if ( s[ i] == '#' && result. size ( ) != 0 ) {
result. pop_back ( ) ;
continue ;
}
result. push_back ( s[ i] ) ;
}
return result;
}
} ;
986. 区间列表的交集
题目
代码
class Solution {
public :
vector< vector< int >> intervalIntersection ( vector< vector< int >> & A, vector< vector< int >> & B) {
int cnt = 0 ;
vector< vector< int >> vec;
vector< int > tmp ( 2 ) ;
for ( int i= 0 ; i< A. size ( ) && cnt< B. size ( ) ; )
{
if ( A[ i] [ 0 ] > B[ cnt] [ 1 ] )
{
cnt++ ;
}
else if ( A[ i] [ 1 ] < B[ cnt] [ 0 ] )
{
i++ ;
}
else if ( A[ i] [ 1 ] < B[ cnt] [ 1 ] )
{
tmp[ 0 ] = max ( A[ i] [ 0 ] , B[ cnt] [ 0 ] ) ;
tmp[ 1 ] = A[ i] [ 1 ] ;
vec. push_back ( tmp) ;
i++ ;
}
else
{
tmp[ 0 ] = max ( A[ i] [ 0 ] , B[ cnt] [ 0 ] ) ;
tmp[ 1 ] = B[ cnt] [ 1 ] ;
vec. push_back ( tmp) ;
cnt++ ;
}
}
return vec;
}
} ;