设计问题
- shuffle an array
- 最小栈
shuffle an array
直接copy了别人的代码,用了库函数random_shuffle(iterator1,iterator2)
代码:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21class Solution { public: vector<int> m_nums; Solution(vector<int> nums) : m_nums(nums){ } /** Resets the array to its original configuration and return it. */ vector<int> reset() { return m_nums; } /** Returns a random shuffling of the array. */ vector<int> shuffle() { vector<int> temp(m_nums); random_shuffle(temp.begin(),temp.end()); return temp; } };
最小栈
也是copy的代码,需要好好理解。
来一个,如果符合比目前最小的更小,就入min。
出栈的时候,如果不是目前最小,就一般出,否则min也出。
AC代码:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30class MinStack { public: /** initialize your data structure here. */ MinStack() { } void push(int x) { s.push(x); if(min.empty()||min.top()>=x) min.push(x); } void pop() { if(min.top()==s.top()) min.pop(); s.pop(); } int top() { return s.top(); } int getMin() { return min.top(); } private: stack<int> s; stack<int> min; };
最后
以上就是潇洒果汁最近收集整理的关于leetcode初级算法 设计问题的全部内容,更多相关leetcode初级算法内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复