当前位置:网站首页>LeetCode - 8 - (三数之和、Z字形变换、两数之和<链表>、盛最多水的容器、电话号码的字母组合)
LeetCode - 8 - (三数之和、Z字形变换、两数之和<链表>、盛最多水的容器、电话号码的字母组合)
2022-04-22 06:16:00 【S atur】

class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
set<vector<int>> res; //去重
int n = nums.size();
for(int i = 0; i < n; i ++){
int l = i+1, r = n-1; //双指针
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://blog.csdn.net/Satur9/article/details/124039410
边栏推荐
- secureCRT无限循环脚本
- ERROR: [Hsi 55-1545] ,无法正常生成fsbl,Unable to read in MSS file,Failed to closesw system.mss
- Define the class shape as the parent class, and define the method to calculate the perimeter and area in the class; (2) Define the shape subclass circle, with radius attribute and constant PI, and ove
- 小题记录——
- Codeforces Round #610 (Div. 2)
- Vscode, this is enough
- 867 · 四键键盘
- 手撕算法---LRU缓存淘汰策略,问的这么频繁
- 指针 结构体 const 小结
- Host cannot Ping virtual machine in bridging mode
猜你喜欢

What is socket programming?

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

Choose any novel website, crawl any novel and save it in the form of Notepad.

桥接模式下主机ping不通虚拟机

C语言 | 指针

Virtual machine disk space shrinks

C语言 | 数组

Points for attention in Modelsim simulation acceleration

Anaconda installation and use

指针 结构体 const 小结
随机推荐
在类加载的过程中,类变量的分配区域和实例变量的分配区域不一样
小题记录——
Find a notepad file by yourself, find the data material by yourself, and count the times of three keywords or sentence entries in the whole text.
手撕算法---LRU缓存淘汰策略,问的这么频繁
C language | preprocessing
14 lines of code to complete arbitrary selection of image crawling
Vscode, this is enough
867 · 四键键盘
【数论】素数(二):素数筛法(埃式筛、欧拉筛、区间筛)
面试官常问的,对象分配的一般过程及特殊情况
C语言 | 指针
(5) Use Navicat to create database data table and set ID auto increment
顺序表之高速缓存命中率
[number theory] congruence (2): inverse element
Points for attention in Modelsim simulation acceleration
Leetcode - 6 - (chaîne multiplicatrice, prochain élément plus grand < Ⅰ Ⅱ Ⅲ >, K liste de chaînes inversées)
【数论】素数(三):素数判断法(朴素法、模6法、Rabin-Miller及改进)
(二)Sql Server的基本配置以及使用Navicat连接Sql Server
字节暑期实习一面——20220304
Process of stepping on the pit in the flutter environment