Ch3-2: implement min() for a stack with O(1)

来源:互联网 发布:freebsd10 安装软件 编辑:程序博客网 时间:2024/06/06 21:03


Here will show the thinking flow from bad to good.

idea 1:


注:正如 http://www.learncpp.com/cpp-tutorial/69-dynamic-memory-allocation-with-new-and-delete/所述:

new[] 在这里是“use the array form of new and delete (often called new[] and delete[]):

而“buf是用来access这个node型的array的”, 所以“Note that array access is done the same way with dynamically allocated arrays as with normal arrays. While this might look slightly funny, given that pnArray is explicitly declared as a pointer, remember that arrays are really just pointers in C++ anyway.

output:
Executing the program....$demo 0 1920 + 20 - 2018 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 0 manhattan-100 -100-100 -100

Whole solution with ans1 and ans2:
ans use 2 stack instead of 1 so save more space in average.

Here is the final result:

Executing the program....$demo 20 9090 20 30 40 59 58 57 56 55 54 53 52 51 50 manhattan  //s120 30 40 50 manhattan  // s2:here shows that s2 only stack the current min value-100 -100-100 -100


0 0
原创粉丝点击