x日练三题,冰冻三尺非一日之寒。
今日题目:1、装更多水的容器;❤2、数组中第k大的元素;3、设置矩阵的0。
今日摘录:
走向远方,
从少年到青年,
从青年到老年,
我们从星星走到了夕阳。
——汪国真 《走向远方》
11. Container With Most Water | Difficulty: 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.
题意:一个容器中有许多条线,找到能盛水最大的两条线。
思路:
1、从最左和最右开始查找,如果碰到能提供更高的高度的线再进行一次判断。
class Solution {
public:
int maxArea(vector<int>& height) {
int left = 0,right = height.size()-1;
int maxArea = 0;
while(left<right)
{
int heightMax = min(height[left],height[right]);
maxArea = max(maxArea,(heightMax*(right-left)));
while(height[left]<=heightMax && left<right) left++;
while(height[right]<=heightMax && left<right) right--;
}
return maxArea;
}
};
结果:28ms
215. Kth Largest Element in an Array | Difficulty: Medium
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.
For example,
Given [3,2,1,5,6,4] and k = 2, return 5.
Note:
You may assume k is always valid, 1 ≤ k ≤ array’s length.
题意:找数组中的第k大元素。
思路:
1、最直观的思路,排序之后取下标,复杂度O(nlogn)
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
sort(nums.begin(),nums.end());
return nums[nums.size()-k];
}
};
结果:26ms
2、利用一个最小堆来实现。
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
priority_queue<int> pq(nums.begin(), nums.end());
for(int i=0;i<k-1;i++)
pq.pop();
return pq.top();
}
};
结果:24ms
3、利用快排的思想,主要要注意边界条件的判断,容易写错。
class Solution {
public:
int quickSort(vector<int>&nums,int l,int r)
{
int pivot = nums[l];
int left = l+1,right = r;
while(left<=right)
{
if(nums[left]<pivot&&nums[right]>pivot) swap(nums[left++],nums[right--]);
if(nums[right]<=pivot) right--;
if(nums[left]>=pivot) left++;
}
swap(nums[l],nums[right]);
return right;
}
int findKthLargest(vector<int>& nums, int k) {
if(nums.size()==0) return 0;
int l = 0,r = nums.size()-1;
while(l<=r)
{
int pos = quickSort(nums,l,r);
if(pos==k-1) return nums[pos];
else if(pos>k-1) r = pos-1;
else l = pos+1;
}
return nums[l];
}
};
结果:204ms
update:2016.9.8
class Solution {
public:
int quickSort(vector<int>&nums,int l,int r)
{
int pivot = nums[l];
int left = l+1,right = r;
while(left<=right)
{
while(left<=right && nums[right]<=pivot) right--;
while(left<=right && nums[left]>=pivot) left++;
if(left<right && nums[left]<pivot&&nums[right]>pivot) swap(nums[left++],nums[right--]);
}
swap(nums[l],nums[right]);
return right;
}
int findKthLargest(vector<int>& nums, int k) {
if(nums.size()==0) return 0;
int l = 0,r = nums.size()-1;
while(l<=r)
{
int pos = quickSort(nums,l,r);
if(pos==k-1) return nums[pos];
else if(pos>k-1) r = pos-1;
else l = pos+1;
}
return nums[l];
}
};
结果:63ms
4、堆排序
class Solution {
public:
void heapAdjust(vector<int>&nums,int s,int n)
{
int tmp = nums[s];
int j;
for(j=2*s+1;j<n;j = 2*j+1)
{
if(j+1<n && nums[j]<nums[j+1]) j++;
if(tmp>nums[j]) break;
nums[s] = nums[j];
s = j;
}
nums[s] = tmp;
return ;
}
int findKthLargest(vector<int>& nums, int k) {
int n = nums.size();
if(n==0) return 0;
for(int i=n/2-1;i>=0;i--)
{
heapAdjust(nums,i,n);
}
for(int i=1;i<=k-1;i++)
{
swap(nums[0],nums[n-i]);
heapAdjust(nums,0,n-i);
}
return nums[0];
}
};
结果:13ms
73. Set Matrix Zeroes | Difficulty: Medium
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
click to show follow up.
Follow up:
Did you use extra space?
A straight forward solution using O(mn) space is probably a bad idea.
A simple improvement uses O(m + n) space, but still not the best solution.
Could you devise a constant space solution?
题意:给定一个矩阵,如果某行或某列有0就将相应行或列全部置0.
思路:
1、首先遍历一次,如果某行有0,就将该行第一个元素置0,某列有0就将该列第一个元素置为0.但这样又有一个问题,就是第0行和第0列都是修改[0][0]位置元素,这样就产生了交叠,所以需要设置另外一个常量来表示第0行或者第0列中的任意一个。不失一般性,可以用col0来代表第0列为0的情况,其他按照上述所说进行修改。这样我们在第二遍遍历的时候,对于非第0行第0列元素,需要判断一次该行首元素和该列首元素中是否有0,对于第0行或第0列元素,则无需处理,因为有0它们才会修改成0,没有0它们就不会修改,而这正是我们的初衷。
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int row = matrix.size(),col = matrix[0].size();
int col0 = 1;
for(int i=0;i<row;i++)
{
if(matrix[i][0]==0) col0 = 0;
for(int j=1;j<col;j++)
{
if(matrix[i][j]==0)
matrix[i][0]=matrix[0][j]=0;
}
}
for(int i=row-1;i>=0;i--)
{
for(int j=col-1;j>0;j--)
{
if(matrix[i][0]==0||matrix[0][j]==0)
matrix[i][j]=0;
}
if(col0==0) matrix[i][0]=0;
}
}
};
结果:68ms