当前位置:网站首页>2022/7/17
2022/7/17
2022-07-19 15:17:00 【anieoo】
Original link : The finger of the sword Offer 58 - II. Left rotation string

solution:
recursive
class Solution {
public:
string reverseLeftWords(string s, int n) {
reverse(s.begin(),s.begin() + n);
reverse(s.begin() + n,s.end());
reverse(s.begin(),s.end());
return s;
}
};
Original link : The finger of the sword Offer 58 - I. Flip word order

solution:
Split string , Back together .
class Solution {
public:
string reverseWords(string s) {
auto words = split(s, ' ');
int n = words.size();
string res;
for(int i = n - 1;i >= 0;i--) {
res += words[i];
if(i) res += " ";
}
return res;
}
vector<string> split(string s, char t) {
int n = s.size();
vector<string> res;
for(int i = 0;i < n;i++) {
int j = i;
string item;
while(j < n && s[j] != t) {
item += s[j];
j++;
}
if(item != "") res.push_back(item);
i = j;
}
return res;
}
};Every time you traverse a word, flip the word , Finally, turn it over
class Solution {
public:
string reverseWords(string s) {
int k = 0;
int n = s.size();
for(int i = 0;i < n;i++) {
if(s[i] == ' ') continue; // Skip leading spaces and skip
int j = i,t = k;
while(j < n && s[j] != ' ') s[t++] = s[j++]; // Save words
reverse(s.begin() + k, s.begin() + t); // Flip Words
s[t++] = ' '; // Add a space
k = t,i = j;
}
if(k) k--;
s.erase(s.begin() + k, s.end()); // Delete the last superfluous part
reverse(s.begin(), s.end());
return s;
}
};The finger of the sword Offer 59 - I. Maximum sliding window

solution:
Monotonic stack + The sliding window
class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
int n = nums.size();
deque<int> que; // Double ended queue used by monotonic stack
vector<int> res;
for(int i = 0;i < n;i++) {
// Over the window size
if(!que.empty() && i - que.front() + 1 > k) que.pop_front();
while(!que.empty() && nums[que.back()] <= nums[i]) que.pop_back();
que.push_back(i);
if(i + 1 >= k) res.push_back(nums[que.front()]);
}
return res;
}
};Priority queue
class Solution {
public:
typedef pair<int,int> PII;
struct cmp {
bool operator() (PII &a, PII &b) {
return a.first < b.first;
}
};
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
int n = nums.size();
if(!n) return {};
vector<int> res; // Define the return value
priority_queue<PII, vector<PII>, cmp> heap; // Construct priority queues
for(int i = 0;i < k;i++) heap.emplace(nums[i], i);
res.push_back(heap.top().first);
for(int i = k;i < n;i++) {
heap.emplace(nums[i], i);
while(heap.top().second < i - k + 1) {
heap.pop();
}
res.push_back(heap.top().first);
}
return res;
}
};The finger of the sword Offer 59 - II. The maximum value of the queue

solution:
A queue implementation push and pop operation , Another double ended queue maintains the maximum value in the queue
class MaxQueue {
public:
queue<int> que;
deque<int> deq;
int maxv = 0;
MaxQueue() {
}
int max_value() {
if(deq.empty()) return -1;
else return deq.front();
}
void push_back(int value) {
que.push(value);
while(!deq.empty() && deq.back() <= value) deq.pop_back();
deq.push_back(value);
}
int pop_front() {
if(que.empty())
return -1;
int t = que.front();
que.pop();
if(t == deq.front())
deq.pop_front();
return t;
}
};
/**
* Your MaxQueue object will be instantiated and called as such:
* MaxQueue* obj = new MaxQueue();
* int param_1 = obj->max_value();
* obj->push_back(value);
* int param_3 = obj->pop_front();
*/边栏推荐
猜你喜欢

ORA-08103

微信小程序6-云开发-云数据库

解决jupyter控制台出现中文乱码的问题

UCAS. Deep learning Final examination questions and brief thinking analysis

Maximum heap and heap sort and priority queue

Google Earth engine - Classification and processing of UAV images

How to bind process threads to CPU cores

B树

2022/7/17

Icml2022 | geometric multimodal comparative representation learning
随机推荐
P1004 [NOIP2000 提高组] 方格取数
初试Dart,笔记
CloudBees CI使用Velero进行灾备(DR)概念验证
GYM103660L. Monster tower overall dichotomy
买股票开户应该选哪个证券公司?什么证券公司是更安全的
E. Split Into Two Sets(种类并查集+染色法判二分图)
【微服务】 微服务学习笔记三:利用Feign替换RestTemplate完成远程调用
微信小程序7-云存储
FMC sub card: 4-channel 12bit 3.2g, 2-channel 12bit, 6.4g AD acquisition / 5G acquisition card /6g acquisition card
UCAS. Deep learning Final examination questions and brief thinking analysis
ARM未定义指令异常 汇编
您填写的保质期截止当前已经小于10天,不允许发布,若实际保质期大于10天,请如实填写生产日期与进货日期
Leetcode 1275. 找出井字棋的获胜者
第1章 预备知识
JVM common tuning configuration parameters
马走斜日(回溯法)
Leetcode 1296. Divide the array into a set of continuous numbers (provide an idea)
2021.07.13【B站】是这样崩的
【Renesas RA6M4开发板之I2C读取mpu6050】
009 execution sequence of SQL statement of interview questions