中级农民
- 积分
- 115
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2019-12-7
- 最后登录
- 1970-1-1
|
06.14.2020 刷题第三天 打卡第三天
1. Sort with two stacks
Given an array, which is initially stored in one stack, sort it with one additional stack (total 2 stacks).
After sorting the original stack should contain the sorted integers and from top to bottom the integers are sorted in ascending order.
Mindset:
Basically it is the kind of selection sort, just use three int variables:
preMax, curMax, count(handle the duplicates) to solve it
2. A to the power of B
Mindset:
Watch out the corner case and not overlooked the odd number of B;
3. Get target in unknown size sorted array
Mindset:
Watch out for the difference between classical binary Search, don't forget to check the rightHand side (equals to null) condition.
4. Use two queues to perform a stack
Mindset:
Offer: just offers into the first queue
Pop: Handle the size limit to 1, then pop it out; do it repeatedly(O(N^2));
Top: Check the size of queue1 and peek it!
also, I can use another way to solve this problem:
generally speaking, that's pretty same, but they use a temp queue as the bridge to swap two queues
Deque<Integer> temp = q1;
q1 = q2;
q2 = temp;
return prev;
Also, it utilized prev and cur two variables to determine the final one element in the first queue and return the prev one.
5.Deque By three stacks
Mindset:
In order to achieve the best time complexity, utilize the buffer as a bridge to contact two stacks, and always check the half size of the stack.
6. Remove repeated element in a sorted array
Mindset:
use three variables: slow/fast/begin(handle the fast and begin in a while loop)
use fast - begin == 1 to detect if duplicated available |
|