2009年4月27日
String... array...
而最好的效率應該就是linear time(至少在類似interview problems中)
至於buffer也是能省就省,避免allocate unnecessary space
所以值得注意兩點:
1. Avoid redundant shift due to character removal
2. Use ASCII array (by default definition) instead of character comparison
此外也可以用swap頭尾兩端的方法來reverse a string
題外話:dynamic programming可看作是recursion with caching
避免重覆計算一樣的東西
Algorithm design should consider both (1) base case and (2) recursive case
By the way, recursion can be inefficient while filling the call stack by explicitly returning desired answers. see more from Tail recursion.
... Read more
2009年4月25日
When a programmer needs some art
此文告訴我們"輕輕鬆鬆當(偽)美術"的一些方法
但在我看來,除了改變一些基本遊戲想法,要讓遊戲畫面OK(至少吸引我)也不是太容易的
創意為本,工時還是難省
附上以我用滑鼠所畫的圖做結論:
GUI裡只有按鈕是自己畫的(That is what I am
... Read more
2009年1月12日
vector, deque, and list
1. A deque offers constant-time insert() and erase() operations at the front of the container, whereas a vector does not -- hence the note in the Standard about using a deque if you need to insert or erase at both ends of the sequence.
2. A deque uses memory in a more operating system-friendly way, particularly on systems without virtual memory. For example, a 10-megabyte vector uses a single 10-megabyte block of memory, which is usually less efficient in practice than a 10-megabyte deque that can fit in a series of smaller blocks of memory.
3. A deque is easier to use, and inherently more efficient for growth, than a vector. The only operations supplied by vector that deque doesn't have are capacity() and reserve() -- and that's because deque doesn't need them! For vector, calling reserve() before a large number of push_back()s can eliminate reallocating ever-larger versions of the same buffer every time it finds out that the current one isn't big enough after all. A deque has no such problem, and having a deque::reserve() before a large number of push_back()s would not eliminate any allocations (or any other work) because none of the allocations are redundant; the deque has to allocate the same number of extra pages whether it does it all at once or as elements are actually appended.
Also from: http://www.velocityreviews.com/forums/t280389-vector-list-and-deque.htmlWhen you need random access to the elements of the collection, and you will be doing insertions and deletions only at the back of the container. That is the time you should prefer vector.
Finally, total explanation at: http://blog.csdn.net/qer_liu/archive/2006/05/07/711642.aspx
... Read more
