题目:
316. 去除重复字母
关键词:
单调栈,哈希表。
思路:
入栈条件:未入栈,且出现次数为1;
统计出现次数--->清零入栈标志位;
--->遍历每个字符,若未入栈,且比栈顶字符小,弹出栈顶字符;
--->清栈顶入栈标志位;
--->若入栈,置入栈标志位;否则出现次数计数减1
0316.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#define MY_DEBUG printf
#define MY_DEBUG
//#define MLOGD(x, fmt...) MY_DEBUG("[%s_%d]"x, __func__, __LINE__, ##fmt)
#define MLOGD
#define MLOGI(x, fmt...) MY_DEBUG("[%s_%d]"x, __func__, __LINE__, ##fmt)
#define MLOGE(x, fmt...) MY_DEBUG("[%s_%d]"x, __func__, __LINE__, ##fmt)
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
typedef char StackValType;
typedef struct {
StackValType *buf;
int top;
size_t size;
} MyStackType;
static MyStackType g_stack = {
.buf = NULL,
.top = 0,
.size = 0
};
int StackPush(StackValType val)
{
if (g_stack.top >= g_stack.size) {
MLOGE("out of range\n");
return -1;
}
MLOGD("---------------------push %d\n", val);
g_stack.buf[g_stack.top++] = val;
return 0;
}
StackValType StackPop(void)
{
if (g_stack.top <= 0) {
MLOGE("empty stack\n");
return -1;
}
return g_stack.buf[--g_stack.top];
}
StackValType StackPeek(void)
{
if (g_stack.top == 0) {
MLOGE("empty stack\n");
return -1;
}
return g_stack.buf[g_stack.top - 1];
}
int StackInit(size_t size)
{
if (size == 0) {
MLOGE("invalid size\n");
return -1;
}
g_stack.buf = (StackValType *)malloc(size * sizeof(StackValType));
if (g_stack.buf == NULL) {
MLOGE("malloc error\n");
return -1;
}
g_stack.size = size;
g_stack.top = 0;
return 0;
}
void StackDeInit(void)
{
if (g_stack.buf != NULL) {
free(g_stack.buf);
g_stack.buf = NULL;
}
g_stack.size = 0;
g_stack.top = -1;
}
char * removeDuplicateLetters(char * s)
{
if (s == NULL) {
MLOGE("s is null\n");
return NULL;
}
size_t srcLen = strlen(s);
if (srcLen == 0) {
MLOGE("src len is 0\n");
return "";
}
int ret = StackInit(srcLen + 1);
if (ret < 0) {
MLOGE("stack init error\n");
return NULL;
}
int i;
int offset;
size_t map[256] = {-1};
for (i = 0; i < srcLen; i++) {
map[s[i]] += 1;
}
bool letters[256] = {0};
for (i = 0; i < srcLen; i++) {
while ((letters[s[i] - 'a'] == false) && (g_stack.top >= 1) && (s[i] < StackPeek()) && (map[StackPeek()] > 1)) {
map[StackPeek()] -= 1;
char out = StackPop();
letters[out - 'a'] = false;
MLOGI("i=%d, %c out, peek is %c\n", i, out, StackPeek());
}
if (letters[s[i] - 'a'] == false) {
StackPush(s[i]);
letters[s[i] - 'a'] = true;
MLOGI("i=%d, %c in\n", i, s[i]);
} else {
map[s[i]] -= 1;
}
}
offset = g_stack.top;
MLOGI("offset=%d\n", offset);
g_stack.buf[offset] = '\0';
return g_stack.buf;
}
CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
SET(TARGET_NAME lc-out)
PROJECT(${TARGET_NAME})
SET(CMAKE_CXX_COMPILER "g++.exe")
SET(CMAKE_C_COMPILER "gcc.exe")
SET(USER_PUBLIC_DIR "D:/leetcode/user_public")
SET(CMAKE_C_FLAGS "-g")
SET(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS}")
FILE(GLOB_RECURSE SRC_LIST ${CMAKE_CURRENT_LIST_DIR}/*.cpp ${CMAKE_CURRENT_LIST_DIR}/*.c)
FILE(GLOB_RECURSE REMOVE_CMAKE ${CMAKE_CURRENT_LIST_DIR}/cmake-build-debug/* ${CMAKE_CURRENT_LIST_DIR}/build/*)
LIST(REMOVE_ITEM SRC_LIST ${REMOVE_CMAKE})
# MESSAGE("${CMAKE_CURRENT_LIST_DIR}")
# MESSAGE("SRC_LIST is:" ${SRC_LIST})
INCLUDE_DIRECTORIES(
${USER_PUBLIC_DIR}/include
)
LINK_DIRECTORIES(
${USER_PUBLIC_DIR}/lib
)
SET(USR_C_FLAGS0 "-Wall -Werror -Wno-char-subscripts ")
SET(CMAKE_C_FLAGS "-g ${USR_C_FLAGS0}")
ADD_EXECUTABLE(${TARGET_NAME} ${SRC_LIST})
TARGET_LINK_LIBRARIES(${TARGET_NAME} gtestd gmockd)
网友评论