71. Simplify Path
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/"
, => "/home"
path = "/a/./b/../../c/"
, => "/c"
- Did you consider the case where path =
"/../"
?
In this case, you should return"/"
. - Another corner case is the path might contain multiple slashes
'/'
together, such as"/home//foo/"
.
In this case, you should ignore redundant slashes and return"/home/foo"
.
Solution: 使用 "/"
作为分隔符将两个 "/"
之间的内容分割出来,分别进行判断,先使用vector记录方便进入上一个目录,最后放入string中。
Code:
class Solution {
public:
string simplifyPath(string path) {
vector<string> simpath;;
int lastsize = -1;
int i = 0;
while(path[i]){
//根据分隔符分隔出一个文件夹名称
int t = 0;
for(int j=i+1; path[j] && path[j]!='/'; j++) t++;
if(t==0){
i = i+1;
continue;
}
string name = path.substr(i+1,t);
if(name==".."){
//回到上一层
if(!simpath.empty()) simpath.pop_back();
}else if(name!="."){
simpath.push_back(name);
}
i = i+t+1;
}
string ans = "";
for(int i=0; i<simpath.size(); i++){
ans += "/"+simpath[i];
}
if(ans=="") ans = "/";
return ans;
}
};
32. Longest Valid Parentheses
Given a string containing just the characters '('
and ')'
, find the length of the longest valid (well-formed) parentheses substring.
For "(()"
, the longest valid parentheses substring is "()"
, which has length = 2.
Another example is ")()())"
, where the longest valid parentheses substring is "()()"
, which has length = 4.
Solution: 使用两个stack,一个用来记录剩下的未匹配的char,另一个用来记录到这个char之前有多长的合法parentheses,假如能成功匹配这个char,那么当前长度需要加上前面匹配到的长度。
Code:
class Solution {
public:
int longestValidParentheses(string s) {
unordered_map<char,char> m;
m[')'] = '(';
stack<char> container;
stack<int> ans;
int curans = 0;
for(int i=0; s[i]; i++){
if(container.empty()){
container.push(s[i]);
ans.push(curans);
curans = 0;
}else{
if(m[s[i]]==container.top()){
container.pop();
curans += ans.top() + 2;
ans.pop();
}else{
container.push(s[i]);
ans.push(curans);
curans = 0;
}
}
}
while(!ans.empty()){
if(ans.top()>curans) curans = ans.top();
ans.pop();
}
return curans;
}
};
84. Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.
Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]
.
The largest rectangle is shown in the shaded area, which has area = 10
unit.
For example,
Given heights = [2,1,5,6,2,3]
,
return 10
.
Solution: 最直观的做法是对一个柱向左右两边扩展直至找到比它矮的柱停止,然后记录下每个柱的最大面积,时间复杂度是O(n^2)超时。因此这里利用stack来解,具体解法参考:http://www.cnblogs.com/ganganloveu/p/4148303.html
1、如果已知height数组是升序的,应该怎么做?
比如1,2,5,7,8
那么就是(1*5) vs. (2*4) vs. (5*3) vs. (7*2) vs. (8*1)
也就是max(height[i]*(size-i))
2、使用栈的目的就是构造这样的升序序列,按照以上方法求解。
Code:
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
if(heights.size()==0) return 0;
stack<int> h;
h.push(heights[0]);
int max = 0;
for(int i=1; i<heights.size(); i++){
int t = 0;
//中间被pop掉的几个,因为也是有序的,因此可以用同样的方法直接计算最大面积
while(!h.empty() && h.top()>heights[i]){
t++;
if(h.top()*t>max) max = h.top()*t;
h.pop();
}
while(t>=0){
h.push(heights[i]);
t--;
}
}
int i = 0;
while(!h.empty()){
i++;
if(h.top()*i>max) max = h.top()*i;
h.pop();
}
return max;
}
};