第一种方法,暴力法:

    1. typedef struct {
    2. int *stack;
    3. int current_size;
    4. } MinStack;
    5. /** initialize your data structure here. */
    6. MinStack* minStackCreate() {
    7. MinStack *ms=(MinStack*)malloc(sizeof(MinStack));
    8. ms->stack=(int*)malloc(20000*sizeof(int));
    9. ms->current_size=0;
    10. return ms;
    11. }
    12. void minStackPush(MinStack* obj, int x) {
    13. obj->stack[obj->current_size]=x;
    14. obj->current_size++;
    15. }
    16. void minStackPop(MinStack* obj) {
    17. obj->current_size--;
    18. }
    19. int minStackTop(MinStack* obj) {
    20. if (obj->current_size==0) return 0;
    21. else
    22. return obj->stack[obj->current_size-1];
    23. }
    24. int minStackMin(MinStack* obj) {
    25. if (obj->current_size==0) return 0;
    26. int temp=obj->stack[0];
    27. for (int i=0;i<obj->current_size;++i) {
    28. if (obj->stack[i]<temp)
    29. temp=obj->stack[i];
    30. }
    31. return temp;
    32. }
    33. void minStackFree(MinStack* obj) {
    34. free(obj->stack);
    35. free(obj);
    36. }
    37. /**
    38. * Your MinStack struct will be instantiated and called as such:
    39. * MinStack* obj = minStackCreate();
    40. * minStackPush(obj, x);
    41. * minStackPop(obj);
    42. * int param_3 = minStackTop(obj);
    43. * int param_4 = minStackMin(obj);
    44. * minStackFree(obj);
    45. */

    leedcode通过:

    1. 执行用时:96 ms, 在所有 C 提交中击败了11.20% 的用户
    2. 内存消耗:12.8 MB, 在所有 C 提交中击败了61.38% 的用户

    第二种方法,使用两个栈

    1. typedef struct {
    2. int *stack;
    3. int *min_stack;
    4. int current_size;
    5. } MinStack;
    6. /** initialize your data structure here. */
    7. MinStack* minStackCreate() {
    8. MinStack *ms=(MinStack*)malloc(sizeof(MinStack));
    9. ms->stack = (int*)malloc(8000*sizeof(int));
    10. ms->min_stack = (int*)malloc(8000*sizeof(int));
    11. ms->current_size = 0;
    12. return ms;
    13. }
    14. void minStackPush(MinStack* obj, int x) {
    15. obj->stack[obj->current_size]=x;
    16. if (obj->current_size==0) {
    17. obj->min_stack[obj->current_size]=x;
    18. }
    19. else {
    20. if (x < obj->min_stack[obj->current_size-1]) {
    21. obj->min_stack[obj->current_size]=x;
    22. }
    23. else {
    24. obj->min_stack[obj->current_size]=obj->min_stack[obj->current_size-1];
    25. }
    26. }
    27. obj->current_size++;
    28. }
    29. void minStackPop(MinStack* obj) {
    30. if (obj->current_size!=0)
    31. obj->current_size--;
    32. }
    33. int minStackTop(MinStack* obj) {
    34. if (obj->current_size!=0)
    35. return obj->stack[obj->current_size-1];
    36. return 0;
    37. }
    38. int minStackMin(MinStack* obj) {
    39. if (obj->current_size!=0)
    40. return obj->min_stack[obj->current_size-1];
    41. return 0;
    42. }
    43. void minStackFree(MinStack* obj) {
    44. free(obj->stack);
    45. free(obj->min_stack);
    46. free(obj);
    47. }
    48. /**
    49. * Your MinStack struct will be instantiated and called as such:
    50. * MinStack* obj = minStackCreate();
    51. * minStackPush(obj, x);
    52. * minStackPop(obj);
    53. * int param_3 = minStackTop(obj);
    54. * int param_4 = minStackMin(obj);
    55. * minStackFree(obj);
    56. */

    leedcode通过:

    1. 执行用时:24 ms, 在所有 C 提交中击败了90.57% 的用户
    2. 内存消耗:12.7 MB, 在所有 C 提交中击败了66.92% 的用户