当前位置:网站首页>leetcode296场周赛
leetcode296场周赛
2022-06-09 23:18:00 【julia点c】
1.极大极小游戏
简单题,通过数组下标操作,可得出答案
class Solution {
public:
int minMaxGame(vector<int>& res) {
int n=res.size();
vector<int>nums=res;
n=n/2;
int a=2;
int k=1;
while(n)
{
int i=0;
for(int i=0;i<n;i++)
{
if(i%2==0)
{
nums[a*i]=min(nums[a*i],nums[a*i+k]);
}
else
{
nums[a*i]=max(nums[a*i],nums[a*i+k]);
}
}
n=n/2;
a=a*2;
k=k*2;
}
return nums[0];
}
};
2.划分数组使最大差为 K
排序+贪心
class Solution {
public:
int partitionArray(vector<int>& nums, int k) {
sort(nums.begin(),nums.end());
int n=nums.size();
int ans=1;
for(int i=0,j=0;i<n;i++)
{
if(nums[i]-nums[j]>k)
{
ans++;
j=i;
}
}
return ans;
}
};
3. 替换数组中的元素
hash+模拟
class Solution {
public:
vector<int> arrayChange(vector<int>& nums, vector<vector<int>>& s) {
unordered_map<int,int>hash;
for(int i=0;i<nums.size();i++)
{
hash[nums[i]]=i;
}
for(auto&x:s)
{
int index=hash[x[0]];
nums[hash[x[0]]]=x[1];
hash.erase(x[0]);
hash[x[1]]=index;
}
return nums;
}
};
4.设计一个文本编辑器
链表模拟,我只会暴力,我是five。。。。。
class TextEditor {
list<char>lst;
list<char>::iterator cur;
string print()
{
string ret;
auto it=cur;
for(int i=0;i<10;i++)
{
if(it==lst.begin()) break;
it =prev(it);
ret.push_back(*it);
}
reverse(ret.begin(),ret.end());
return ret;
}
public:
TextEditor() {
cur=lst.begin();
}
void addText(string text) {
for(char c:text) lst.insert(cur,c);
}
int deleteText(int k) {
int ret=0;
while(k&&cur!=lst.begin())
{
cur=prev(cur);
cur=lst.erase(cur);
k--,ret++;
}
return ret;
}
string cursorLeft(int k) {
while(k&&cur!=lst.begin())
{
cur=prev(cur);
k--;
}
return print();
}
string cursorRight(int k) {
while(k&&cur!=lst.end())
{
cur=next(cur);
k--;
}
return print();
}
};
边栏推荐
- YoutubeDNN召回的一些问题
- Chaîne - 4 - 242. Mots hétérotopiques alphabétiques valides
- 读书会招募 | 一起来读《蛤蟆先生去看心理医生》吧
- 香橙派H3烧录Uboot,远程加载zImage,dtb,rootfs
- Game installation, downloading and updating no unveils the mystery of future games
- 网络状态的起起伏伏
- 记一次应急排查“新“路历程
- 黄希庭:心理学的研究要走中国人自己的道路,要做中国化的心理学研究
- Autre contenu lourd | formes de produits et mesures de contrôle des risques pour les prêts en espèces à l'étranger
- 2022 practice questions and mock exam of quality controller municipal direction general basic (quality controller) examination
猜你喜欢

打破那面镜子,做回真正的自己(深度好文)

How does uni app build applets?

It only takes eight steps to package the applet to generate an app

The latest activity openharmony open source developer growth plan solution student challenge is about to start!

Exness: twitter said that it would continue to share data with musk and conduct shareholder voting at the end of July or early August

蓝桥杯_分割立方体_组合数学_加法原理

leetcode547. Number of provinces (medium, find the number of connected components)

"Leak detection and vacancy filling" Android internship interview knowledge points (II)

I've taken it. The MySQL table has 500W rows. Is there anyone who doesn't partition?

你了解单例模式吗?反正我不了解。
随机推荐
2022年金属非金属矿山爆破考试题库及答案
手推单精度浮点类型
Anatomy of illusory rendering system (15) - XR topic
2022 love analysis · privacy computing vendor panoramic report | love Analysis Report
SPSS主成分分析
emu8086的入门使用
设计千万级学生管理系统的考试试卷存储方案
What is a distributed software system
Py6S配置教程(win10 ×64)
使用打印机打印东西打印不全怎么办
Versionfilter version control
香橙派H3烧录Uboot,远程加载zImage,dtb,rootfs
Record the 'new' course of an emergency investigation
片上变化(on chip variation,OCV)概念学习
不能在此路径中使用此配置节,如果在父级别上锁定了该节,便会出现这种情况的解决办法
Manual single precision floating point type
Virtual machine environment configuration record 1
黄希庭:心理学的研究要走中国人自己的道路,要做中国化的心理学研究
Autre contenu lourd | formes de produits et mesures de contrôle des risques pour les prêts en espèces à l'étranger
微机原理与接口技术习题1