当前位置:网站首页>Leetcode - 6 - (string multiplication, next larger element < Ⅰ Ⅱ Ⅲ >, K sets of inverted linked list)
Leetcode - 6 - (string multiplication, next larger element < Ⅰ Ⅱ Ⅲ >, K sets of inverted linked list)
2022-04-22 07:26:00 【S atur】

class Solution {
public:
string multiply(string num1, string num2) {
if(num1=="0"||num2=="0") return "0";
int n = num1.size(), m = num2.size();
vector<int> tmp(n+m);
for(int i = n-1; ~i; i--){ // Multiply by
int x = num1[i]-'0';
for(int j = m-1; ~j; j--){
int y = num2[j]-'0';
tmp[i+j+1] += x*y;
}
}
string ans;
for(int i = n+m-1; i > 0; i --){ // Carry processing
tmp[i-1] += tmp[i]/10;
tmp[i] %= 10;
ans.push_back(tmp[i]+'0');
}
if(tmp[0]) ans.push_back(tmp[0]+'0'); // Judge leading zero
reverse(ans.begin(), ans.end()); // The result needs to be flipped
return ans;
}
};

class Solution {
public:
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
int n = nums2.size();
vector<int> res(n, -1);
stack<int> st;
map<int, int> mp;
for(int i = 0; i < n; i ++){
mp[nums2[i]] = i;
while(!st.empty()&&nums2[st.top()]<nums2[i]){
res[st.top()] = nums2[i];
st.pop();
}
st.push(i);
}
vector<int> ans;
for(int i = 0; i < nums1.size(); i ++){
ans.push_back(res[mp[nums1[i]]]);
}
return ans;
}
};

class Solution {
public:
vector<int> nextGreaterElements(vector<int>& nums) {
int n = nums.size();
vector<int> ans(n, -1);
stack<int> st;
for(int i = 0; i < n*2-1; i ++){
while(!st.empty()&&nums[st.top()]<nums[i%n]){
ans[st.top()] = nums[i%n];
st.pop();
}
st.push(i%n);
}
return ans;
}
};

class Solution {
public:
int nextGreaterElement(int n) {
string s = to_string(n);
if(next_permutation(s.begin(), s.end())&&stoll(s)<=INT_MAX) return stoi(s);
return -1;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
pair<ListNode*, ListNode*> Reverse(ListNode* bg, ListNode* ed){
ListNode* pre = ed->next;
ListNode* p = bg;
while(pre!=ed){
ListNode* nex = p->next;
p->next = pre;
pre = p;
p = nex;
}
return {ed, bg};
}
ListNode* reverseKGroup(ListNode* head, int k) {
ListNode* dummy = new ListNode(0);
dummy->next = head;
ListNode* pre = dummy;
while(head){
ListNode* ed = pre;
for(int i = 0; i < k; i ++){
ed = ed->next;
if(!ed) return dummy->next;
}
ListNode* nex = ed->next;
tie(head, ed) = Reverse(head, ed);
pre->next = head;
ed->next = nex;
pre = ed;
head = ed->next;
}
return dummy->next;
}
};
版权声明
本文为[S atur]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220616059617.html
边栏推荐
猜你喜欢

浅谈时间复杂度与空间复杂度

Idea does not display the run dashboard view window

L2-002 linked list weight removal (pit of test point 1)

Win10 modify command line default font

(2) Basic configuration of SQL server and connection to SQL server using Navicat

A. Alice and Bob (博弈?思维&暴力)(2021牛客暑期多校训练营1)

Why is the data stored in the leaf node of the non primary key index the primary key value

LeetCode - 6 - (字符串相乘、下一個更大元素<ⅠⅡⅢ>、k個一組翻轉鏈錶)
![[DRC RTSTAT-1] Unrouted nets: 1 net(s) are unrouted](/img/bf/0851c85f766432f6d6306d52e67188.png)
[DRC RTSTAT-1] Unrouted nets: 1 net(s) are unrouted

(4) Character set in SQL Server (collation)
随机推荐
Byte Summer Internship - 20220304
(2) Basic configuration of SQL server and connection to SQL server using Navicat
SecureCRT infinite loop script
Redis advanced
B. Ball Dropping (简单几何计算 / 相似三角形) (2021牛客暑期多校训练营1)
快排与归并排序
Codeforces Round #610 (Div. 2)
Why is the data stored in the leaf node of the non primary key index the primary key value
区间求和的问题——差分
最小圆覆盖(计算几何基础)
Codeforces Round #634 (Div. 3)
系统日志收集系列
CF1547E Air Conditioners
A. Weird Flecks, But OK (计算几何&三维最小圆覆盖)(2021年度训练联盟热身训练赛第一场)
Precautions for using feign to make remote calls
LeetCode - 6 - (字符串相乘、下一个更大元素<ⅠⅡⅢ>、k个一组翻转链表)
Solution to the 45 problem of Niuke XB monthly competition
Idea does not display the run dashboard view window
L1-071 前世档案 (20 分) (类似二分)
296 · 数组去重