目录
1.leetcode第20题
2. leetcode第42题--接雨水
3. leetcode 第84题--柱状图中最大的矩形
4. leetcode第496题--下一个更大元素I
5. leetcode第439题--每日温度
bool isValid(char * s){
char stack[strlen(s)+1];
memset(stack,0,strlen(s)+1);
int i = 0;
int top = 0;
for(i=0; i < strlen(s);i++)
{
if(s[i] == ' ')
{
continue;
}
/* 左半边 */
if(s[i] == '[' || s[i] == '(' || s[i] == '{')
{
top++;
stack[top] = s[i];
continue;
}
/* 右半边 */
if(stack[top] == '[' && s[i] == ']')
{
top--;
continue;
}
if(stack[top] == '{' && s[i] == '}')
{
top--;
continue;
}
if(stack[top] == '(' && s[i] == ')')
{
top--;
continue;
}
return false;
}
if(top > 0)
return false;
return true;
}
int getMin( int a, int b)
{
if(a >= b)
return b;
else
return a;
}
int trap(int* height, int heightSize){
int* stack = (int*)malloc(sizeof(int) * heightSize);
memset(stack, -1, sizeof(int) * heightSize);
int index = -1;
int res = 0;
for(int i = 0; i < heightSize; i++) {
//1、 如果栈顶元素小于当前元素(栈为严格单调减,如果相等说明一定是紧邻的,两者间没有凹陷),说明中间可能有凹陷回溯求和
while(index >= 0 && height[stack[index]] < height[i]) {
int bH = height[stack[index]];
index--; //3-1 退栈,一定有一个凹陷
if(index < 0) { // 3-1-1 已经退完了 没有凹陷了
break;
} else { // 3-1-2 如果有凹陷,求取凹陷
if(bH == height[stack[index]]) { //如果回退后的栈顶与凹陷基值还相等,那继续玩后退直达找到最近的高点
continue;
}
int h = getMin(height[i], height[stack[index]])-bH; //取凹陷两边高点的最小值并减去凹陷的高度
printf("h = %d\n",h);
int width = i - stack[index] - 1;//取凹陷两边高点的宽度
res += h * width;
}
}
stack[++index] = i; //2、 如果栈顶元素大于当前元素,则说明形状还在形成,压栈
}
return res;
}
int largestRectangleArea(int* heights, int heightsSize){
int top = -1;
int area, i;
int maxarea = 0;
int *stack = (int *)malloc(sizeof(int) * (heightsSize + 2));
int *buff = (int *)malloc(sizeof(int) * (heightsSize + 2));
// 在前面加哨兵
buff[0] = 0;
for (int i = 1; i <= heightsSize; i++) {
buff[i] = heights[i - 1];
}
// 在最后加哨兵
buff[heightsSize + 1] = 0;
stack[++top] = 0;
for (i = 1; i < heightsSize + 2; i++) {
while (top > 0 && buff[i] < buff[stack[top]]) {
area = (i - stack[top - 1] - 1) * buff[stack[top]];
maxarea = maxarea > area ? maxarea : area;
top--;
}
stack[++top] = i;
}
return maxarea;
}
typedef struct Node
{
int key;
int value;
struct Node *next;
}HashNode;
int hash(int key,int numSize)
{
return key & (numSize - 1);
}
void add(HashNode *HashTable,int key,int value,int numSize)
{
HashNode *head = &HashTable[hash(key,numSize)];
HashNode *q = malloc(sizeof(HashNode));
q->key = key;
q->value = value;
q->next = head->next;
head->next = q;
}
int getKey(HashNode * hashtable, int key, int numsize)
{
HashNode * head = &hashtable[hash(key, numsize)];
HashNode * tail = head->next;
while(tail){
if(tail->key == key) return tail->value;
tail = tail->next;
}
return -1;
}
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* nextGreaterElement(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize){
HashNode * hashtable = malloc(sizeof(HashNode) * nums2Size);
memset(hashtable, 0, sizeof(HashNode) * nums2Size);
int *ans = malloc(sizeof(int) * nums1Size);
int *stack = malloc(sizeof(int) *nums2Size);
int i = 0;
int top = -1;
for(i = 0; i < nums2Size; i++)
{
while(top >= 0 && stack[top] < nums2[i])
{
add(hashtable, stack[top--], nums2[i], nums2Size);
}
stack[++top] = nums2[i];
}
while(top >= 0)
{
add(hashtable,stack[top--],-1,nums2Size);
}
for(i = 0; i < nums1Size;i++)
{
ans[i] = getKey(hashtable,nums1[i],nums2Size);
}
*returnSize = nums1Size;
return ans;
}
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* dailyTemperatures(int* T, int TSize, int* returnSize){
*returnSize = TSize;
int top = -1; //栈顶下标
int stack[TSize];//创建TSize大小的数据 模拟栈 存储值为T的下标
int *result = (int *)malloc(sizeof(int)*TSize);
memset(result,0,sizeof(int)*TSize);
for (int i = 0; i=0)
{
int index = stack[top];
int data = T[index];
if (t>data)
{
top--;
result[index] = i - index;
}
else
{
break;
}
}
top++;
stack[top] = i;
}
while (top>=0)
{
result[stack[top]] = 0;
top--;
}
return result;
}