当前位置:网站首页>Leetcode - 6 - (chaîne multiplicatrice, prochain élément plus grand < Ⅰ Ⅱ Ⅲ >, K liste de chaînes inversées)
Leetcode - 6 - (chaîne multiplicatrice, prochain élément plus grand < Ⅰ Ⅱ Ⅲ >, K liste de chaînes inversées)
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--){ //Calcul de la multiplication
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 --){ //Traitement de la portance
tmp[i-1] += tmp[i]/10;
tmp[i] %= 10;
ans.push_back(tmp[i]+'0');
}
if(tmp[0]) ans.push_back(tmp[0]+'0'); // Déterminer les zéros de plomb
reverse(ans.begin(), ans.end()); // Les résultats doivent être inversés
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
边栏推荐
- win10 anaconda安装cocotb
- 【数论】欧拉函数(基本性质、递推法、公式法、线性筛法)
- Detailed tree array template -- Theory and code implementation
- [number theory] prime number (V): Mason prime number (lucas_lehmer decision method)
- 【数论】【不定方程】n元一次不定方程、佩尔方程、毕达哥拉斯定理、费马大定理
- C语言 | i++ 与 ++i
- 【数论】同余(五):多元线性同余方程
- (2) Basic configuration of SQL server and connection to SQL server using Navicat
- C语言 | 位运算符
- Unknown graphics extension:. 1 jpg. }
猜你喜欢

Binary tree chain structure operation leetcode + Niuke (detailed explanation)

Host cannot Ping virtual machine in bridging mode

1. Compile the following three modules of student information management system: and detect the implementation. 1. Add student information 4. Query student information 5. Query all student information

在类加载的过程中,类变量的分配区域和实例变量的分配区域不一样

C语言 | 数组

面试官常问的,对象分配的一般过程及特殊情况

Redis advanced

MySQL learning notes

Anaconda installation and use

a5 transceiver 信号vod和预加重调整关系
随机推荐
[number theory] prime number (I): basic concepts, properties, conjectures and theorems
Help documents on log4net and NLog usage
字节暑期实习一面——20220304
Vivado generates and invokes EDF netlist files
【数论】素数(三):素数判断法(朴素法、模6法、Rabin-Miller及改进)
【数论】同余(一):同余的基本概念与性质
Complete a student information management system and systematically practice object-oriented, function, string and other knowledge. Realize the comprehensive application of knowledge. Use classes, fun
[number theory] prime number (V): Mason prime number (lucas_lehmer decision method)
[number theory] [indefinite equation] n-ary primary indefinite equation, Pell equation, Pythagoras theorem, Fermat theorem
When latex uses the template, the caption title of the picture cannot be left aligned
【题解】洛谷P6186 [NOI Online #1 提高组] 冒泡排序:【冒泡排序】与【逆序对】问题
[number theory] prime number (III): prime number judgment method (simple method, modular 6 method, Rabin Miller and improvement)
Unknown graphics extension:. 1 jpg. }
[number theory] congruence (2): inverse element
C语言 | i++ 与 ++i
[DRC RTSTAT-1] Unrouted nets: 1 net(s) are unrouted
Redis進階
Design a circle class with private member radius representing radius and function get_ Radius () is used to obtain the radius and area () is used to calculate the area of the circle; (2) Define a tabl
单例池、单例Bean、单例模式
ERROR: [Hsi 55-1545] ,无法正常生成fsbl,Unable to read in MSS file,Failed to closesw system.mss