Leetcode 杂题
给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
说明:你不能倾斜容器,且 n 的值至少为 2。
图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。
示例:
输入: [1,8,6,2,5,4,8,3,7] 输出: 49
class Solution {
public:int maxArea(vector<int>& a){vector <int> v[20100];int up = 0;int mx = 0, mn = 2e9;int ans = 0;int pos = 0;for (auto u : a) v[u].push_back(++pos), up = max(up, u);for (int i = up; i; --i){if (v[i].size() > 0){for (auto u : v[i]){mx = max(mx, u);mn = min(mn, u);}ans = max(ans, (mx - mn) * i);}}return ans;}
};
给定一个大小为 n 的数组,找到其中的众数。众数是指在数组中出现次数大于 ⌊ n/2 ⌋
的元素。
你可以假设数组是非空的,并且给定的数组总是存在众数。
示例 1:
输入: [3,2,3] 输出: 3
示例 2:
输入: [2,2,1,1,1,2,2] 输出: 2
class Solution {
public:int majorityElement(vector<int>& nums) {int cnt = 0, now = 0;for (auto u : nums){if (cnt == 0) now = u, cnt = 1;else cnt += (u == now ? 1 : -1);}return now;}
};
运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制。它应该支持以下操作: 获取数据 get和 写入数据 put
。
获取数据 get(key)
- 如果密钥 (key) 存在于缓存中,则获取密钥的值(总是正数),否则返回 -1。
写入数据 put(key, value)
- 如果密钥不存在,则写入其数据值。当缓存容量达到上限时,它应该在写入新数据之前删除最近最少使用的数据值,从而为新的数据值留出空间。
进阶:
你是否可以在 O(1) 时间复杂度内完成这两种操作?
#define rep(i, a, b) for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i)
#define MP make_pair
#define fi first
#define se secondclass LRUCache {private:int up;int cnt;list< pair<int, int>> queue; unordered_map< int, list<pair<int, int>>::iterator> mp;public:LRUCache(int capacity){up = capacity;mp.clear();cnt = 0;}int get(int key){int ret = -1;auto p = mp.find(key);if (p != mp.end()){ret = p -> se -> se;queue.erase(p -> se);queue.push_front(MP(key, ret));p -> se = queue.begin();}return ret;}void put(int key, int value){auto p = mp.find(key);if (p != mp.end()){queue.erase(p -> se);}else if (cnt == up){int delkey = queue.back().fi;queue.pop_back();mp.erase(delkey);}else ++cnt;queue.push_front(MP(key, value));mp[key] = queue.begin();}
};
设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。
- push(x) -- 将元素 x 推入栈中。
- pop() -- 删除栈顶的元素。
- top() -- 获取栈顶元素。
- getMin() -- 检索栈中的最小元素。
示例:
MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minStack.getMin(); --> 返回 -3. minStack.pop(); minStack.top(); --> 返回 0. minStack.getMin(); --> 返回 -2.
class MinStack {private:stack <int> s, t;public:/** initialize your data structure here. */MinStack(){}void push(int x){s.push(x);if (t.empty() || x <= t.top()){t.push(x);}}void pop(){int now = s.top();s.pop();if (!t.empty() && t.top() == now){t.pop();}}int top(){return s.top();}int getMin(){return t.top();}
};
转载于:https://www.cnblogs.com/cxhscst2/p/11446794.html
Leetcode 杂题相关推荐
- LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)
LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...
- LeetCode刷题记录14——257. Binary Tree Paths(easy)
LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...
- LeetCode刷题记录13——705. Design HashSet(easy)
LeetCode刷题记录13--705. Design HashSet(easy) 目录 LeetCode刷题记录13--705. Design HashSet(easy) 前言 题目 语言 思路 源 ...
- LeetCode刷题记录12——232. Implement Queue using Stacks(easy)
LeetCode刷题记录12--232. Implement Queue using Stacks(easy) 目录 LeetCode刷题记录12--232. Implement Queue usin ...
- LeetCode刷题记录11——290. Word Pattern(easy)
LeetCode刷题记录11--290. Word Pattern(easy) 目录 LeetCode刷题记录11--290. Word Pattern(easy) 题目 语言 思路 源码 后记 题目 ...
- LeetCode刷题记录10——434. Number of Segments in a String(easy)
LeetCode刷题记录10--434. Number of Segments in a String(easy) 目录 LeetCode刷题记录9--434. Number of Segments ...
- LeetCode刷题记录9——58. Length of Last Word(easy)
LeetCode刷题记录9--58. Length of Last Word(easy) 目录 LeetCode刷题记录9--58. Length of Last Word(easy) 题目 语言 思 ...
- LeetCode刷题记录8——605. Can Place Flowers(easy)
LeetCode刷题记录8--605. Can Place Flowers(easy) 目录 LeetCode刷题记录8--605. Can Place Flowers(easy) 题目 语言 思路 ...
- LeetCode刷题记录7——824. Goat Latin(easy)
LeetCode刷题记录7--824. Goat Latin(easy) 目录 LeetCode刷题记录7--824. Goat Latin(easy) 题目 语言 思路 后记 题目 题目需要将一个输 ...
最新文章
- 单例模式(单一实例)
- 如何在 Linux 中使用 find
- fedora操作系统优缺点_不同类型的操作系统的优缺点
- 分布式文档存储独角兽MongoDB——MongoDB常见命令(2)
- 【Flink】Flink Invalid timestamp -1 Timestamp should always be none-negative or null
- Python2 倒计时,还不快来掌握 Python3 酷炫的新特性? | 原力计划
- Vue3学习之第四节:setup()中使用watch、watchEffect 函数
- oracle获取用户名,Oracle 用户名详解
- 单点登录SSO的实现原理与方案详解
- webform(八)组合查询
- IntelliJ IDEA常用快捷键
- 新浪微博开放平台接口使用小结
- 保险业IT整体规划图
- wine android安装程序,Wine 4.0 发布- Ubuntu安装
- Java实现八皇后问题
- 网页游戏的项目设计方案分享
- 内存卡损坏怎么修复?分享实际经验
- 最优化路径和火车票退票
- 你究竟多想成功?(nba励志视频)
- python库——pandas