当前位置:网站首页>Leetcode - 8 - (sum of three numbers, zigzag transformation, sum of two numbers < linked list >, container with the most water, letter combination of telephone number)
Leetcode - 8 - (sum of three numbers, zigzag transformation, sum of two numbers < linked list >, container with the most water, letter combination of telephone number)
2022-04-22 07:26:00 【S atur】

class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
set<vector<int>> res; // duplicate removal
int n = nums.size();
for(int i = 0; i < n; i ++){
int l = i+1, r = n-1; // Double pointer
while(l<r){
if(nums[i]+nums[l]+nums[r]==0){
res.insert({nums[i], nums[l], nums[r]});
l ++;
r --;
}
else if(nums[i]+nums[l]+nums[r]<0) l ++;
else r --;
}
}
vector<vector<int>> ans;
for( auto x:res){
ans.push_back(x);
}
return ans;
}
};

class Solution {
public:
string convert(string s, int numRows) {
int n = numRows, sz = s.size();
if(n==1||n>=sz) return s;
vector<string> res(n);
for(int i = 0, x = 0, t = n*2-2; i < sz; i ++){
res[x] += s[i];
i%t<n-1 ? x++ : x--;
}
string ans;
for(auto x:res) ans += x;
return ans;
}
};

class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* head = nullptr, *ed = nullptr;
int sum = 0;
while(l1||l2){
int x = l1?l1->val:0;
int y = l2?l2->val:0;
int z = x+y+sum;
if(!head) head = ed = new ListNode(z%10);
else{
ed->next = new ListNode(z%10);
ed = ed->next;
}
sum = z/10;
if(l1) l1 = l1->next;
if(l2) l2 = l2->next;
}
if(sum>0){
ed->next = new ListNode(sum);
}
return head;
}
};

class Solution {
public:
int maxArea(vector<int>& height) {
int l = 0, r = height.size()-1;
int ans = 0;
while(l<r){
int sum = min(height[l], height[r])*(r-l);
ans = max(ans, sum);
if(height[l]<=height[r]) l ++;
else r --;
}
return ans;
}
};

class Solution {
public:
string tmp;
vector<string> ans;
vector<string> res = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
void dfs(int pos, string digits){
if(digits.size()==pos){
ans.push_back(tmp);
return;
}
int x = digits[pos]-'0';
for(int i = 0; i < res[x].size(); i ++){
tmp += res[x][i];
dfs(pos+1, digits);
tmp.erase(tmp.size()-1, 1);
}
}
vector<string> letterCombinations(string digits) {
if(digits.size()==0) return ans;
dfs(0, digits);
return ans;
}
};
版权声明
本文为[S atur]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220616059545.html
边栏推荐
- L2-001 紧急救援 (最短路Dijkstra的扩展 - 最短路径数&路径最大权值)
- 最强数组学习之路
- 296 · 数组去重
- [number theory] congruence (7): fast power, fast power of matrix
- 抽象类和抽象方法
- 队列(详解)——手撕队列习题
- Suppose there is such a relationship between the weight and height of adults: height (CM) - 100 = standard weight (kg). If the difference between a person's weight and its standard weight is between p
- 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
- 【数论】同余(七):快速幂、矩阵快速幂
- 顺序表 增删查(找)
猜你喜欢

LaTex用模板的时候图片的caption标题无法左对齐

内部类使用说明(静态、实例、局部)

LeetCode - 5 - (重复的子字符串<kmp>、最长回文子串、转置矩阵、二叉树的(左右)视图)
![Error: [HSI 55-1545], unable to generate fsbl normally, unable to read in MSS file, failed to close system mss](/img/4e/34e2820ff8579007b20b33b27d8f1d.png)
Error: [HSI 55-1545], unable to generate fsbl normally, unable to read in MSS file, failed to close system mss

顺序表之高速缓存命中率

Redis进阶

Points for attention in Modelsim simulation acceleration

顺序表 增删查(找)

Redis Advanced

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
随机推荐
在类加载的过程中,类变量的分配区域和实例变量的分配区域不一样
Add author photos and author profiles to latex
【数论】同余(一):同余的基本概念与性质
LeetCode - 8 - (三数之和、Z字形变换、两数之和<链表>、盛最多水的容器、电话号码的字母组合)
1420 · 最小覆盖子串II
下面这段SQL能否用索引优化查询性能
链表习题详解
【数论】同余(三):一元线性同余方程
843 · 数字翻转
SQL review, grammar notes, fresh out
instanceof的使用说明及实例讲解
使用Feign做远程调用的注意点
单例池、单例Bean、单例模式
递归找序列集合
牛客xb月赛45题解
Codeforces Round #780 (Div. 3)
【数论】素数(四):数的分解(Pollard-rho)
为什么非主键索引的叶子结点存放的数据是主键值
微软实习生面试的2道算法题目——20220119
Error: (vlog-2892) Net type of 'i_ yc422' must be explicitly declared.