11Container With Most Water36.5%Medium
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container and n is at least 2.
此题看的答案class Solution {
public:
int maxArea(vector<int>& height) {
int n=height.size();
int areamax=0;
int i=0,j=n-1;
while(i<j)
{
int hmin=min(height[i],height[j]);
areamax=max(areamax,(j-i)*hmin);
while(height[i]<=hmin&&i<j) {
++i;
}
while(height[j]<=hmin&&i<j) {
j--;
}
}
return areamax;
}
};
class Solution {
public:
int maxArea(vector<int>& height) {
int n=height.size();
int areamax=0;
int i=0,j=n-1;
while(i<j)
{
int hmin=min(height[i],height[j]);
areamax=max(areamax,(j-i)*hmin);
while(height[i]<=hmin&&i<j) {
++i;
}
while(height[j]<=hmin&&i<j) {
j--;
}
}
return areamax;
}
};
18 4Sum26.7%Medium
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note: The solution set must not contain duplicate quadruplets.
For example, given array S = [1, 0, -1, 0, -2, 2], and target = 0. A solution set is: [ [-1, 0, 0, 1], [-2, -1, 1, 2], [-2, 0, 0, 2] ]思路1:编程未通过,借鉴3Sum
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
//这个题只需要调用3sum加和函数即可
//注意string和vector的区别,vector没有+重载
int n=nums.size();
vector<vector<int>> res;
std::sort(nums.begin(),nums.end());
for(int i=0;i<n;i++)
{
int quardTarget=target-nums[i];
threeSum(res,nums,quardTarget,i,n-1);
while(i+1<n&&nums[i]==nums[i+1]) i++;
}
return res;
}
//3Sum加和函数
void threeSum(vector<vector<int>> res,vector<int>& nums,int target,int i,int j){
int n=nums.size();
for(int k=i+1;k<n;k++)
{
int tempTarget=target-nums[k];
int front=k+1,back=j;
while(front<back)
{
int sum=nums[front]+nums[back];
if(sum<tempTarget) front++;
else if(sum>tempTarget) back--;
else if(sum==tempTarget)
{
vector<int> triplets(4,0);
triplets[0]=nums[i];
triplets[1]=nums[k];
triplets[2]=nums[front];
triplets[3]=nums[back];
res.push_back(triplets);
while(front<back&&nums[front]==triplets[2]) front++;
while(front<back&&nums[back]==triplets[3]) back--;
}
}
while(k+1<n&&nums[k]==nums[k+1]) k++;
}
return ;
}
};
思路2:使用哈希表,暂放
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note: The solution set must not contain duplicate quadruplets.
For example, given array S = [1, 0, -1, 0, -2, 2], and target = 0. A solution set is: [ [-1, 0, 0, 1], [-2, -1, 1, 2], [-2, 0, 0, 2] ]
31 Next Permutation28.7%Medium
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place, do not allocate extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3
→ 1,3,2
3,2,1
→ 1,2,3
1,1,5
→ 1,5,
605. Can Place Flowers 实际上是贪心算法
Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for water and both would die.
Given a flowerbed (represented as an array containing 0 and 1, where 0 means empty and 1 means not empty), and a number n, return if n new flowers can be planted in it without violating the no-adjacent-flowers rule.
Example 1:
Input: flowerbed = [1,0,0,0,1], n = 1
Output: True
Example 2:
Input: flowerbed = [1,0,0,0,1], n = 2
Output: False
Note:
- The input array won't violate no-adjacent-flowers rule.
- The input array size is in the range of [1, 20000].
- n is a non-negative integer which won't exceed the input array size.
注意这个题目的分析:虽然可能有多种种植方式(两种),从第一个满足情况就种植的方式会是最大的种植数目
class Solution {
public:
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
int count=0;
int size=flowerbed.size();
int i=0;
while(i<size)
{
if(flowerbed[i]==0&&(i==0||flowerbed[i-1]==0)&&(i==size-1||flowerbed[i+1]==0))//注意双等号
{
count++;
flowerbed[i]=1;//这里要改变成1
}
if(count>=n) return true;
i++;
}
return false;
}
};
48. Rotate Image
Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for water and both would die.
Given a flowerbed (represented as an array containing 0 and 1, where 0 means empty and 1 means not empty), and a number n, return if n new flowers can be planted in it without violating the no-adjacent-flowers rule.
Example 1:
Input: flowerbed = [1,0,0,0,1], n = 1 Output: True
Example 2:
Input: flowerbed = [1,0,0,0,1], n = 2 Output: False
Note:
- The input array won't violate no-adjacent-flowers rule.
- The input array size is in the range of [1, 20000].
- n is a non-negative integer which won't exceed the input array size.
注意这个题目的分析:虽然可能有多种种植方式(两种),从第一个满足情况就种植的方式会是最大的种植数目
class Solution {
public:
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
int count=0;
int size=flowerbed.size();
int i=0;
while(i<size)
{
if(flowerbed[i]==0&&(i==0||flowerbed[i-1]==0)&&(i==size-1||flowerbed[i+1]==0))//注意双等号
{
count++;
flowerbed[i]=1;//这里要改变成1
}
if(count>=n) return true;
i++;
}
return false;
}
};
48. Rotate Image
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Note:
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
Example 1:
Given input matrix = [ [1,2,3], [4,5,6], [7,8,9] ], rotate the input matrix in-place such that it becomes: [ [7,4,1], [8,5,2], [9,6,3] ]
Example 2:
Given input matrix = [ [ 5, 1, 9,11], [ 2, 4, 8,10], [13, 3, 6, 7], [15,14,12,16] ], rotate the input matrix in-place such that it becomes: [ [15,13, 2, 5], [14, 3, 4, 1], [12, 6, 8, 9], [16, 7,10,11] ]
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int n=matrix.size();
if(n==0) return ;
int m=matrix[0].size();
if(n!=m) return ;
int lx=0,ly=0;
int rx=n-1,ry=n-1;//一定是方阵
while(lx<rx)//相等时,只剩一个元素不需要操作
{
doCircle(matrix,lx++,ly++,rx--,ry--);
}
}
void doCircle(vector<vector<int>>& matrix,int lx,int ly,int rx,int ry){
int times=rx-lx;//需要调整的次数
for(int i=0;i<times;i++)
{
int tmp=matrix[lx][ly+i];
matrix[lx][ly+i]=matrix[rx-i][ly];
matrix[rx-i][ly]=matrix[rx][ry-i];
matrix[rx][ry-i]=matrix[lx+i][ry];
matrix[lx+i][ry]=tmp;
}
}
};
581. Shortest Unsorted Continuous Subarray 最短排序
对于一个无序数组A,请设计一个算法,求出需要排序的最短子数组的长度。
给定一个整数数组A及它的大小n,请返回最短子数组的长度。
class Solution {
public:
int findUnsortedSubarray(vector<int>& nums) {
int n=nums.size();
if(n<=1) return 0;
int minl=nums[n-1];
int minleft=-1;
for(int i=n-2;i>=0;i--)
{
if(nums[i]>minl)
minleft=i;
else
minl=min(nums[i],minl);
}
if(minleft==-1) return 0;
int maxl=nums[0];
int maxright=-1;
for(int i=1;i<n;i++)
{
if(nums[i]<maxl)
maxright=i;
else
maxl=max(nums[i],maxl);
}
return maxright-minleft+1;
}
};