如果你觉得不错可以 ⭐Star 和 Fork ❤
存储最小值 + 不断更新最小值:
class MinStack {
private:
stack<pair<int,int>> minstack;
public:
/** initialize your data structure here. */
MinStack() {
}
void push(int x) {
if(minstack.empty()){
pair<int,int> temp(x,x);
minstack.push(temp);
}
else{
int cur = min(minstack.top().first, x);
pair<int,int> temp(cur,x);
minstack.push(temp);
}
}
void pop() {
minstack.pop();
}
int top() {
return minstack.top().second;
}
int getMin() {
return minstack.top().first;
}
};
/**
* Your MinStack object will be instantiated and called as such:
* MinStack* obj = new MinStack();
* obj->push(x);
* obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->getMin();
*/
使用辅助栈存储最小值:
class MinStack {
private:
stack<int> minstack;
stack<int> numstack;
public:
/** initialize your data structure here. */
MinStack() {
}
void push(int x) {
numstack.push(x);
if(minstack.empty())
minstack.push(x);
else{
int temp = min(minstack.top(), x);
minstack.push(temp);
}
}
void pop() {
numstack.pop();
minstack.pop();
}
int top() {
return numstack.top();
}
int getMin() {
return minstack.top();
}
};
/**
* Your MinStack object will be instantiated and called as such:
* MinStack* obj = new MinStack();
* obj->push(x);
* obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->getMin();
*/
双指针 + 数学推导:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode * posA = headA;
ListNode * posB = headB;
while(posA != posB){
posA = posA == NULL ? headA : posA->next;
posB = posB == NULL ? headB : posB->next;
}
return posA;
}
};
哈希计数:
class Solution {
public:
int majorityElement(vector<int>& nums) {
int len = nums.size();
map<int, int> m;
for(int i=0; i<len; i++){
m[ nums[i] ]++;
if(m[ nums[i] ] > len/2)
return nums[i];
}
return -1;
}
};
排序取中间:
class Solution {
public:
int majorityElement(vector<int>& nums) {
sort(nums.begin(), nums.end());
return nums[ nums.size()/2 ];
}
};
Moore 摩尔投票法:
class Solution {
public:
int majorityElement(vector<int>& nums) {
int count = 0, ans;
for(auto n : nums){
if(count == 0)
ans = n;
if(ans == n)
count++;
else
count--;
}
return ans;
}
};
动态规划:
class Solution {
public:
int rob(vector<int>& nums) {
int len = nums.size();
if(len == 0)
return 0;
if(len == 1)
return nums[0];
int dp[len];
dp[0] = nums[0];
dp[1] = max(nums[0], nums[1]);
for(int i=2; i<len; i++){
dp[i] = max(dp[i-1], dp[i-2]+nums[i]);
}
return dp[len-1];
}
};
深度优先搜索 DFS:
class Solution {
public:
int dic[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
int numIslands(vector<vector<char>>& grid) {
int ans = 0;
int v_len = grid.size();
if(v_len == 0)
return 0;
int h_len = grid[0].size();
for(int i=0; i<v_len; i++){
for(int j=0; j<h_len; j++){
if(grid[i][j] == '1'){
ans++;
grid[i][j] = 'v';
int pos[2] = {i, j};
dfs(pos, grid);
}
}
}
return ans;
}
void dfs(int pos[], vector<vector<char>>& grid){
for(int i=0; i<4; i++){
int now[2] = {pos[0]+dic[i][0], pos[1]+dic[i][1]};
if(now[0] >= 0 && now[0] < grid.size() &&
now[1] >= 0 && now[1] < grid[0].size()){
if(grid[ now[0] ][ now[1] ] == '1'){
grid[ now[0] ][ now[1] ] = 'v';
dfs(now, grid);
}
}
}
return;
}
};