20.有效的括号 https://leetcode.cn/problems/valid-parentheses/
bool isValid(char * s){
int len = 0;
int num = 0;
int i = 0;
char *array = NULL;
len = strlen(s);
array = (char *)malloc(len + 1);
if (NULL == array) return false;
memset(array, 0, len + 1);
for (i = 0; i < len; i++)
{
switch (s[i])
{
case '(':
case '{':
case '[':
array[num++] = s[i];
break;
case ')':
if (num <= 0 || '(' != array[num - 1])
{
if (NULL != array){free(array); array = NULL;}
return false;
}
num--;
break;
case '}':
if (num <= 0 || '{' != array[num - 1]){
if (NULL != array){free(array); array = NULL;}
return false;
}
num--;
break;
case ']':
if (num <= 0 || '[' != array[num - 1]){
if (NULL != array){free(array); array = NULL;}
return false;
}
num--;
break;
default:
break;
}
}
END:
if (NULL != array){free(array); array = NULL;}
if (0 == num)
return true;
return false;
}
1047.删除字符串中的所有相邻重复项 https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/
char * removeDuplicates(char * s){
int len = 0;
char *array = 0;
int i = 0;
int num = 0;
len = strlen(s);
array = (char *)malloc(len + 1);
if (NULL == array) return NULL;
memset(array, 0, len + 1);
//printf("111\n");
array[num++] = s[0];
for (i = 1; i < len; i++)
{
//printf("start i = %d\n", i);
if (num <= 0 || array[num - 1] != s[i])
array[num++] = s[i];
else if (num >= 1 && array[num - 1] == s[i])
num--;
//printf("end i = %d\n", i);
}
//printf("222\n");
memset(s, 0, len);
strncpy(s, array, num);
if (NULL != array){free(array); array = NULL;}
//printf("333\n");
return s;
}
150.逆波兰表达式求值 https://leetcode.cn/problems/evaluate-reverse-polish-notation/
#ifdef DEBUG
#define debug(fmt, args...) do{printf("[%s+%d] "fmt"\n", __FUNCTION__, __LINE__, ##args);}while(0)
#else
#define debug(fmt, args...)
#endif
int evalRPN(char **tokens, int tokensSize){
int i = 0;
//int tmp_num = 0;
long int value = 0;
int *array = NULL;
int num = 0;
long int array1 = 0;
long int array2 = 0;
array = (int *)malloc(sizeof(long int) * tokensSize);
if (NULL == array) return 0;
memset(array, 0, sizeof(long int) * tokensSize);
for (i = 0; i < tokensSize; i++)
{
// is num
if (0 == strcmp(tokens[i], "+"))
{
array2 = array[--num];
array1 = array[--num];
value = array1 + array2;
debug("%ld + %ld = %ld", array1, array2, value);
array[num++] = value;
}
else if (0 == strcmp(tokens[i], "-"))
{
array2 = array[--num];
array1 = array[--num];
value = array1 - array2;
debug("%ld - %ld = %ld", array1, array2, value);
array[num++] = value;
}
else if (0 == strcmp(tokens[i], "*"))
{
array2 = array[--num];
array1 = array[--num];
value = array1 * array2;
debug("%ld * %ld = %ld", array1, array2, value);
array[num++] = value;
}
else if (0 == strcmp(tokens[i], "/"))
{
array2 = array[--num];
array1 = array[--num];
value = array1 / array2;
debug("%ld / %ld = %ld", array1, array2, value);
array[num++] = value;
}
// is 运算符
else
{
value = atoi(tokens[i]);
array[num++] = value;
}
}
value = array[--num];
return value;
}