leetcode其余题目

时间:2023-03-09 18:23:05
leetcode其余题目

1.Largest Rectangle in Histogram

http://discuss.leetcode.com/questions/259/largest-rectangle-in-histogram

http://www.cnblogs.com/remlostime/archive/2012/11/25/2787359.html

2.Minimum Window Substring

http://discuss.leetcode.com/questions/97/minimum-window-substring

http://www.cnblogs.com/remlostime/archive/2012/11/16/2774077.html

3.Sort Colors

class Solution {
public:
void sortColors(int A[], int n) {
int p0 = 0, p2 = n;
for (int i = 0; i < p2; ++i) {
if (A[i] == 0) {
swap(A[i], A[p0++]);
}
else if (A[i] == 2) {
swap(A[i--], A[--p2]);
}
}
}
};

4.Text Justification,贪心法

http://www.cnblogs.com/remlostime/archive/2012/11/18/2776335.html

5.Rotate Image

class Solution {
public:
void rotate(vector<vector<int> > &matrix)
{
int n = matrix.size();
for(int i = 0; i < n/2; ++i)
for(int j = i; j < n-1-i; ++j){
int t = matrix[i][j];
matrix[i][j] = matrix[n-1-j][i];
matrix[n-1-j][i] = matrix[n-1-i][n-1-j];
matrix[n-1-i][n-1-j] = matrix[j][n-1-i];
matrix[j][n-1-i] = t;
}
}
};

6.Generate Parentheses

class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> ans;
if (n>0) generator(ans, "", 0, 0, n);
return ans;
} void generator(vector<string> & ans, string s, int l, int r, int n) {
// r/l: appearance of ) (
if (l == n) {
ans.push_back(s.append(n-r, ')'));
return;
}
generator(ans, s+'(', l+1, r, n);
if (l>r) generator(ans, s+")", l, r+1, n);
}
};

7.Container With Most Water

class Solution {
public:
int maxArea(vector<int> &height)
{
int i = 0;
int j = height.size() - 1;
int res = 0;
while(i < j)
{
int temp = min(height[i], height[j]) * (j - i);
if(temp > res)
res = temp;
if(height[i] <= height[j])
i++;
else
j--;
}
return res;
}
};